| 1 |
options("rgdal_show_exportToProj4_warnings"="none") |
| 2 |
library(sp) |
| 3 |
x = c(0.5, 1.5, 0.5, 1.5, 1.6) |
| 4 |
y = c(1.5, 1.5, 0.5, 0.5, 0.5) |
| 5 |
xy = cbind(x,y) |
| 6 |
dimnames(xy)[[1]] = c("a", "b", "c", "d", "e") |
| 7 |
pts = SpatialPoints(xy) |
| 8 |
z = data.frame(z1 = 1:5, z2=5:1, f = c("a", "a", "b", "b", "b")) |
| 9 |
row.names(z) = c("a", "b", "c", "d", "e") |
| 10 |
ptsdf = SpatialPointsDataFrame(pts, z) |
| 11 |
|
| 12 |
xpol = c(0,1,1,0,0) |
| 13 |
ypol = c(0,0,1,1,0) |
| 14 |
pol = SpatialPolygons(list( |
| 15 |
Polygons(list(Polygon(cbind(xpol-1,ypol))), ID="x9"), |
| 16 |
Polygons(list(Polygon(cbind(xpol,ypol))), ID="x2"), |
| 17 |
Polygons(list(Polygon(cbind(xpol,ypol-1))), ID="x3"), |
| 18 |
Polygons(list(Polygon(cbind(xpol+1,ypol))), ID="x4") |
| 19 |
)) |
| 20 |
z = data.frame(z = c(10, 15, 25, 3), zz = 1:4, f = c("z", "q", "r", "z"), |
| 21 |
row.names = c("x9", "x2", "x3", "x4")) |
| 22 |
poldf = SpatialPolygonsDataFrame(pol, z) |
| 23 |
plot(pol, xlim = c(-1.5, 2)) |
| 24 |
points(pts, col='red') |
| 25 |
|
| 26 |
over(pts, pol) |
| 27 |
over(pts, poldf) |
| 28 |
over(pts, poldf[1:2], fn = mean) |
| 29 |
|
| 30 |
#rbind(poldf, over(pts, poldf[1:2], fn = mean)) |
| 31 |
|
| 32 |
over(pol, pts) |
| 33 |
over(pol, ptsdf) |
| 34 |
over(pol, ptsdf[1:2], fn = mean) |
| 35 |
|
| 36 |
pts[pol] |
| 37 |
|
| 38 |
points(pts[pol], col='green', pch=16, cex=.8) |
| 39 |
summary(pol[pts]) |
| 40 |
plot(pol[pts], border='blue', add=TRUE) |
| 41 |
|
| 42 |
gt = GridTopology(c(.5,.5), c(1,1), c(3,2)) |
| 43 |
sg = SpatialGrid(gt) |
| 44 |
df6 = data.frame(z = 6:1, f = c("a", "a", "b", "b", "c", "c")) |
| 45 |
sgdf = SpatialGridDataFrame(gt, df6) |
| 46 |
over(sg, pol) |
| 47 |
over(sg, poldf) |
| 48 |
over(sg, poldf[1:2]) |
| 49 |
|
| 50 |
spix = as(sg, "SpatialPixels") |
| 51 |
spixdf = as(sgdf, "SpatialPixelsDataFrame") |
| 52 |
over(spix, pol) |
| 53 |
over(spix, poldf) |
| 54 |
over(spix, poldf[1:2]) |
| 55 |
|
| 56 |
over(pol, sg) |
| 57 |
over(pol, sgdf) |
| 58 |
over(pol, sgdf[1], fn = mean) |
| 59 |
|
| 60 |
over(pol, spix) |
| 61 |
over(pol, spixdf) |
| 62 |
over(pol, spixdf[1], fn = mean) |
| 63 |
|
| 64 |
over(pts, sg) |
| 65 |
over(pts, spix) |
| 66 |
over(pts, sgdf) |
| 67 |
over(pts, spixdf) |
| 68 |
|
| 69 |
#over(pts, sg, returnList=TRUE) |
| 70 |
#over(pts, spix, returnList=TRUE) |
| 71 |
#over(pts, sgdf, returnList=TRUE) |
| 72 |
#over(pts, spixdf, returnList=TRUE) |