Debian Codegraph @

inst/doc/intro_sp.R

raw
1 ### R code from vignette source 'intro_sp.Rnw'
2
3 ###################################################
4 ### code chunk number 1: intro_sp.Rnw:74-75
5 ###################################################
6 library(sp)
7
8
9 ###################################################
10 ### code chunk number 2: intro_sp.Rnw:78-80
11 ###################################################
12 set.seed(13331)
13 # library(lattice)
14
15
16 ###################################################
17 ### code chunk number 3: intro_sp.Rnw:184-188
18 ###################################################
19 xc = round(runif(10), 2)
20 yc = round(runif(10), 2)
21 xy = cbind(xc, yc)
22 xy
23
24
25 ###################################################
26 ### code chunk number 4: intro_sp.Rnw:193-196
27 ###################################################
28 xy.sp = SpatialPoints(xy)
29 xy.sp
30 plot(xy.sp, pch = 2)
31
32
33 ###################################################
34 ### code chunk number 5: intro_sp.Rnw:202-203
35 ###################################################
36 plot(xy.sp, pch = 2)
37
38
39 ###################################################
40 ### code chunk number 6: intro_sp.Rnw:211-214
41 ###################################################
42 xy.cc = coordinates(xy.sp)
43 class(xy.cc)
44 dim(xy.cc)
45
46
47 ###################################################
48 ### code chunk number 7: intro_sp.Rnw:218-225
49 ###################################################
50 bbox(xy.sp)
51 dimensions(xy.sp)
52 xy.sp[1:2]
53 xy.df = as.data.frame(xy.sp)
54 class(xy.df)
55 dim(xy.df)
56 summary(xy.sp)
57
58
59 ###################################################
60 ### code chunk number 8: intro_sp.Rnw:233-247
61 ###################################################
62 df = data.frame(z1 = round(5 + rnorm(10), 2), z2 = 20:29)
63 df
64 xy.spdf = SpatialPointsDataFrame(xy.sp, df)
65 xy.spdf
66 summary(xy.spdf)
67 dimensions(xy.spdf)
68 xy.spdf[1:2, ] # selects row 1 and 2
69 xy.spdf[1] # selects attribute column 1, along with the coordinates
70 xy.spdf[1:2, "z2"] # select row 1,2 and attribute "z2"
71 xy.df = as.data.frame(xy.spdf)
72 xy.df[1:2,]
73 xy.cc = coordinates(xy.spdf)
74 class(xy.cc)
75 dim(xy.cc)
76
77
78 ###################################################
79 ### code chunk number 9: intro_sp.Rnw:258-261
80 ###################################################
81 df1 = data.frame(xy, df)
82 coordinates(df1) = c("xc", "yc")
83 df1
84
85
86 ###################################################
87 ### code chunk number 10: intro_sp.Rnw:264-268
88 ###################################################
89 df2 = data.frame(xy, df)
90 coordinates(df2) = ~xc+yc
91 df2[1:2,]
92 as.data.frame(df2)[1:2,]
93
94
95 ###################################################
96 ### code chunk number 11: intro_sp.Rnw:276-277
97 ###################################################
98 coordinates(df2)[1:2,]
99
100
101 ###################################################
102 ### code chunk number 12: intro_sp.Rnw:281-285
103 ###################################################
104 df2[["z2"]]
105 df2[["z2"]][10] = 20
106 df2[["z3"]] = 1:10
107 summary(df2)
108
109
110 ###################################################
111 ### code chunk number 13: intro_sp.Rnw:289-291 (eval = FALSE)
112 ###################################################
113 ## bubble(df2, "z1", key.space = "bottom")
114 ## spplot(df2, "z1", key.space = "bottom")
115
116
117 ###################################################
118 ### code chunk number 14: intro_sp.Rnw:297-299
119 ###################################################
120 print(bubble(df2, "z1", key.space = "bottom"), split = c(1,1,2,1), more=TRUE)
121 print(spplot(df2, "z1", key.space = "bottom"), split = c(2,1,2,1), more=FALSE)
122
123
124 ###################################################
125 ### code chunk number 15: intro_sp.Rnw:318-321
126 ###################################################
127 gt = GridTopology(cellcentre.offset = c(1,1,2), cellsize=c(1,1,1), cells.dim = c(3,4,6))
128 grd = SpatialGrid(gt)
129 summary(grd)
130
131
132 ###################################################
133 ### code chunk number 16: intro_sp.Rnw:325-326
134 ###################################################
135 gridparameters(grd)
136
137
138 ###################################################
139 ### code chunk number 17: intro_sp.Rnw:332-337
140 ###################################################
141 pts = expand.grid(x = 1:3, y = 1:4, z=2:7)
142 grd.pts = SpatialPixels(SpatialPoints(pts))
143 summary(grd.pts)
144 grd = as(grd.pts, "SpatialGrid")
145 summary(grd)
146
147
148 ###################################################
149 ### code chunk number 18: intro_sp.Rnw:349-356
150 ###################################################
151 attr = expand.grid(xc = 1:3, yc = 1:3)
152 grd.attr = data.frame(attr, z1 = 1:9, z2 = 9:1)
153 coordinates(grd.attr) = ~xc+yc
154 gridded(grd.attr)
155 gridded(grd.attr) = TRUE
156 gridded(grd.attr)
157 summary(grd.attr)
158
159
160 ###################################################
161 ### code chunk number 19: intro_sp.Rnw:368-375
162 ###################################################
163 fullgrid(grd)
164 fullgrid(grd.pts)
165 fullgrid(grd.attr)
166 fullgrid(grd.pts) = TRUE
167 fullgrid(grd.attr) = TRUE
168 fullgrid(grd.pts)
169 fullgrid(grd.attr)
170
171
172 ###################################################
173 ### code chunk number 20: intro_sp.Rnw:409-413
174 ###################################################
175 fullgrid(grd.attr) = FALSE
176 grd.attr[1:5, "z1"]
177 fullgrid(grd.attr) = TRUE
178 grd.attr[1:2,-2, c("z2","z1")]
179
180
181 ###################################################
182 ### code chunk number 21: intro_sp.Rnw:424-435
183 ###################################################
184 l1 = cbind(c(1,2,3),c(3,2,2))
185 l1a = cbind(l1[,1]+.05,l1[,2]+.05)
186 l2 = cbind(c(1,2,3),c(1,1.5,1))
187 Sl1 = Line(l1)
188 Sl1a = Line(l1a)
189 Sl2 = Line(l2)
190 S1 = Lines(list(Sl1, Sl1a), ID="a")
191 S2 = Lines(list(Sl2), ID="b")
192 Sl = SpatialLines(list(S1,S2))
193 summary(Sl)
194 plot(Sl, col = c("red", "blue"))
195
196
197 ###################################################
198 ### code chunk number 22: intro_sp.Rnw:443-446
199 ###################################################
200 df = data.frame(z = c(1,2), row.names=sapply(slot(Sl, "lines"), function(x) slot(x, "ID")))
201 Sldf = SpatialLinesDataFrame(Sl, data = df)
202 summary(Sldf)
203
204
205 ###################################################
206 ### code chunk number 23: intro_sp.Rnw:458-469
207 ###################################################
208 Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
209 Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
210 Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
211 Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
212
213 Srs1 = Polygons(list(Sr1), "s1")
214 Srs2 = Polygons(list(Sr2), "s2")
215 Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
216 SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
217 plot(SpP, col = 1:3, pbg="white")
218 # plot(SpP)
219
220
221 ###################################################
222 ### code chunk number 24: intro_sp.Rnw:476-480
223 ###################################################
224 attr = data.frame(a=1:3, b=3:1, row.names=c("s3/4", "s2", "s1"))
225 SrDf = SpatialPolygonsDataFrame(SpP, attr)
226 as(SrDf, "data.frame")
227 spplot(SrDf)
228
229
230 ###################################################
231 ### code chunk number 25: intro_sp.Rnw:483-484
232 ###################################################
233 print(spplot(SrDf))
234
235
236 ###################################################
237 ### code chunk number 26: intro_sp.Rnw:488-490
238 ###################################################
239 SrDf = attr
240 polygons(SrDf) = SpP
241
242