Debian Codegraph @

tests/pass1.R

raw
1 options("rgdal_show_exportToProj4_warnings"="none")
2 source("utils/print.R")
3
4 library(sp)
5 data(meuse)
6 x = meuse
7 coordinates(x) = cbind(rnorm(155), rnorm(155))
8 # should pass:
9 names(x@data)
10 names(as.data.frame(x))
11 class(as(x, "data.frame"))
12 x = meuse
13 # coordinates defined as data:
14 coordinates(x) = cbind(xcoord = rnorm(155), ycoord = rnorm(155))
15 # should pass:
16 names(x@data)
17 names(as.data.frame(x))
18 is.projected(x)
19 proj4string(x)
20
21 set.seed(13131) # make sample reproducable:
22 x = meuse[, sample(ncol(meuse))] # 'randomly' shuffle columns
23 # coordinates defined as variable names:
24 coordinates(x) = c("x", "y") # no matter their position
25 #plot(x, cex=.05 * sqrt(x@data[,"zinc"]),
26 plot(x, cex=.05 * sqrt(as.data.frame(x)[["zinc"]]),pch=1)
27 title("Meuse: zinc bubble plot")
28 print(summary(x))
29
30 # coordinates defined as formula:
31 x = meuse[, 1:5]
32 coordinates(x) = ~x+y
33 print(summary(x))
34
35 # a = NULL
36 # cc = cbind(sample(1:10), sample(1:10), sample(1:10))
37 # coordinates(a) = cc
38 # summary(a)
39
40 xx = SpatialPointsDataFrame(matrix(1:10,5,2),data.frame(f = 1:5))
41 rbind(xx,xx,xx,xx)
42
43 grd <- GridTopology(c(1,1), c(1,1), c(10,10))
44 polys <- as.SpatialPolygons.GridTopology(grd)
45 summary(rbind(polys[1:10], polys[11:20], polys[21:30]))
46 plot(rbind(polys[1:10],polys[21:30]))
47 title("2 x 10 blocks -- test rbind on SpatialPolygons")
48
49 l1 = cbind(c(1,2,3),c(3,2,2))
50 l1a = cbind(l1[,1]+.05,l1[,2]+.05)
51 l2 = cbind(c(1,2,3),c(1,1.5,1))
52 Sl1 = Line(l1)
53 Sl1a = Line(l1a)
54 Sl2 = Line(l2)
55 S1 = Lines(list(Sl1, Sl1a), ID="a")
56 S2 = Lines(list(Sl2), ID="b")
57 Sl = SpatialLines(list(S1,S2))
58
59 summary(as(polys, "SpatialLines"))
60 summary(as(Sl, "SpatialPoints"))
61 summary(as(Sl, "SpatialPointsDataFrame"))
62 SlDf = SpatialLinesDataFrame(Sl, data.frame(xx = factor(c("foo", "bar"))), match.ID = FALSE)
63 summary(as(SlDf, "SpatialPointsDataFrame"))
64
65 meuse[["xxx"]] = log(meuse$zinc)
66 print(summary(meuse))
67 meuse$xxy = log(meuse[["zinc"]])
68 print(summary(meuse))
69
70 # test behaviour on zero-length objects:
71 demo(meuse, ask = FALSE, echo = FALSE)
72 p = as(meuse, "SpatialPoints")
73
74 p[0]
75 dim(p[0])
76
77 meuse[0,]
78 dim(meuse[1:3,0])
79 dim(subset(meuse, zinc < 100, 0))
80 dim(subset(meuse, , 0))
81 dim(subset(meuse, F, 0))
82
83 dim(meuse.grid[0,])
84 fullgrid(meuse.grid) = TRUE
85 dim(meuse.grid[0,])
86 print(summary(meuse.grid[0,]))
87 dim(meuse.grid[,0])
88 print(summary(meuse.grid[,0]))
89 dim(meuse.grid[0,,0])
90 print(summary(meuse.grid[0,,0]))
91
92 # SpatialPolygons:
93 L = as(meuse.riv, "SpatialPolygons")
94 length(L[1])
95 length(L[0])
96 summary(L[0])
97
98 # SpatialPolygonsDataFrame:
99 L$something = factor("bla")
100 class(L)
101 length(L[1])
102 summary(L[1])
103 length(L[0])
104 summary(L[0])
105
106 # SpatialLines
107 L = as(meuse.riv, "SpatialLines")
108 length(L[1])
109 length(L[0])
110 summary(L[0])
111
112 # SpatialLinesDataFrame
113 L$something = factor("bla")
114 class(L)
115 length(L[1])
116 summary(L[1])
117 length(L[0])
118 summary(L[0])
119
120 m = meuse
121 all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,]))
122 all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[0,], m[1:3,], m[0,], m[5:7,]))
123 all.equal(rbind(m[1:3,], m[5:7,]), rbind(m[1:3,], m[0,], m[5:7,], m[0,]))
124
125 # match.ID settings for SpatialPointsDataFrame():
126 set.seed(1331)
127 pts = cbind(1:5, 1:5)
128 dimnames(pts)[[1]] = letters[1:5]
129 df = data.frame(a = 1:5)
130 row.names(df) = letters[5:1]
131
132 library(sp)
133 options(warn=1) # show warnings where they occur
134 SpatialPointsDataFrame(pts, df) # warn
135 SpatialPointsDataFrame(pts, df, match.ID = TRUE) # don't warn
136 SpatialPointsDataFrame(pts, df, match.ID = FALSE) # don't warn
137 df$m = letters[5:1]
138 SpatialPointsDataFrame(pts, df, match.ID = "m") # don't warn
139
140 dimnames(pts)[[1]] = letters[5:1]
141 pts
142 SpatialPointsDataFrame(pts, df) # don't warn: match doesn't reorder
143
144 # duplicated row name:
145 dimnames(pts)[[1]] = letters[c(1:4,1)]
146 pts
147 xx = try(x <- SpatialPointsDataFrame(pts, df))
148 class(xx)
149 xx
150 SpatialPointsDataFrame(pts, df, match.ID = FALSE)
151 try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail
152
153 dimnames(pts)[[1]] = letters[5:1]
154 row.names(df) = letters[6:2] # non-matching row.names
155 SpatialPointsDataFrame(pts, df) # do not match.ID
156 SpatialPointsDataFrame(pts, df, match.ID = FALSE)
157 try(x <- SpatialPointsDataFrame(pts, df, match.ID = TRUE)) # fail