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