| 1 |
|
| 2 |
R version 4.3.1 (2023-06-16) -- "Beagle Scouts" |
| 3 |
Copyright (C) 2023 The R Foundation for Statistical Computing |
| 4 |
Platform: x86_64-pc-linux-gnu (64-bit) |
| 5 |
|
| 6 |
R is free software and comes with ABSOLUTELY NO WARRANTY. |
| 7 |
You are welcome to redistribute it under certain conditions. |
| 8 |
Type 'license()' or 'licence()' for distribution details. |
| 9 |
|
| 10 |
R is a collaborative project with many contributors. |
| 11 |
Type 'contributors()' for more information and |
| 12 |
'citation()' on how to cite R or R packages in publications. |
| 13 |
|
| 14 |
Type 'demo()' for some demos, 'help()' for on-line help, or |
| 15 |
'help.start()' for an HTML browser interface to help. |
| 16 |
Type 'q()' to quit R. |
| 17 |
|
| 18 |
> options("rgdal_show_exportToProj4_warnings"="none") |
| 19 |
> library(sp) |
| 20 |
> x = c(0.5, 1.5, 0.5, 1.5, 1.6) |
| 21 |
> y = c(1.5, 1.5, 0.5, 0.5, 0.5) |
| 22 |
> xy = cbind(x,y) |
| 23 |
> dimnames(xy)[[1]] = c("a", "b", "c", "d", "e") |
| 24 |
> pts = SpatialPoints(xy) |
| 25 |
> z = data.frame(z1 = 1:5, z2=5:1, f = c("a", "a", "b", "b", "b")) |
| 26 |
> row.names(z) = c("a", "b", "c", "d", "e") |
| 27 |
> ptsdf = SpatialPointsDataFrame(pts, z) |
| 28 |
> |
| 29 |
> xpol = c(0,1,1,0,0) |
| 30 |
> ypol = c(0,0,1,1,0) |
| 31 |
> pol = SpatialPolygons(list( |
| 32 |
+ Polygons(list(Polygon(cbind(xpol-1,ypol))), ID="x9"), |
| 33 |
+ Polygons(list(Polygon(cbind(xpol,ypol))), ID="x2"), |
| 34 |
+ Polygons(list(Polygon(cbind(xpol,ypol-1))), ID="x3"), |
| 35 |
+ Polygons(list(Polygon(cbind(xpol+1,ypol))), ID="x4") |
| 36 |
+ )) |
| 37 |
> z = data.frame(z = c(10, 15, 25, 3), zz = 1:4, f = c("z", "q", "r", "z"), |
| 38 |
+ row.names = c("x9", "x2", "x3", "x4")) |
| 39 |
> poldf = SpatialPolygonsDataFrame(pol, z) |
| 40 |
> plot(pol, xlim = c(-1.5, 2)) |
| 41 |
> points(pts, col='red') |
| 42 |
> |
| 43 |
> over(pts, pol) |
| 44 |
a b c d e |
| 45 |
NA NA 2 4 4 |
| 46 |
> over(pts, poldf) |
| 47 |
z zz f |
| 48 |
a NA NA <NA> |
| 49 |
b NA NA <NA> |
| 50 |
c 15 2 q |
| 51 |
d 3 4 z |
| 52 |
e 3 4 z |
| 53 |
> over(pts, poldf[1:2], fn = mean) |
| 54 |
z zz |
| 55 |
a NA NA |
| 56 |
b NA NA |
| 57 |
c 15 2 |
| 58 |
d 3 4 |
| 59 |
e 3 4 |
| 60 |
> |
| 61 |
> #rbind(poldf, over(pts, poldf[1:2], fn = mean)) |
| 62 |
> |
| 63 |
> over(pol, pts) |
| 64 |
x9 x2 x3 x4 |
| 65 |
NA 3 NA 4 |
| 66 |
> over(pol, ptsdf) |
| 67 |
z1 z2 f |
| 68 |
x9 NA NA <NA> |
| 69 |
x2 3 3 b |
| 70 |
x3 NA NA <NA> |
| 71 |
x4 4 2 b |
| 72 |
> over(pol, ptsdf[1:2], fn = mean) |
| 73 |
z1 z2 |
| 74 |
x9 NA NA |
| 75 |
x2 3.0 3.0 |
| 76 |
x3 NA NA |
| 77 |
x4 4.5 1.5 |
| 78 |
> |
| 79 |
> pts[pol] |
| 80 |
SpatialPoints: |
| 81 |
x y |
| 82 |
c 0.5 0.5 |
| 83 |
d 1.5 0.5 |
| 84 |
e 1.6 0.5 |
| 85 |
Coordinate Reference System (CRS) arguments: NA |
| 86 |
> |
| 87 |
> points(pts[pol], col='green', pch=16, cex=.8) |
| 88 |
> summary(pol[pts]) |
| 89 |
Object of class SpatialPolygons |
| 90 |
Coordinates: |
| 91 |
min max |
| 92 |
x 0 2 |
| 93 |
y 0 1 |
| 94 |
Is projected: NA |
| 95 |
proj4string : [NA] |
| 96 |
> plot(pol[pts], border='blue', add=TRUE) |
| 97 |
> |
| 98 |
> gt = GridTopology(c(.5,.5), c(1,1), c(3,2)) |
| 99 |
> sg = SpatialGrid(gt) |
| 100 |
> df6 = data.frame(z = 6:1, f = c("a", "a", "b", "b", "c", "c")) |
| 101 |
> sgdf = SpatialGridDataFrame(gt, df6) |
| 102 |
> over(sg, pol) |
| 103 |
1 2 3 4 5 6 |
| 104 |
NA NA NA 2 4 NA |
| 105 |
> over(sg, poldf) |
| 106 |
z zz f |
| 107 |
1 NA NA <NA> |
| 108 |
2 NA NA <NA> |
| 109 |
3 NA NA <NA> |
| 110 |
4 15 2 q |
| 111 |
5 3 4 z |
| 112 |
6 NA NA <NA> |
| 113 |
> over(sg, poldf[1:2]) |
| 114 |
z zz |
| 115 |
1 NA NA |
| 116 |
2 NA NA |
| 117 |
3 NA NA |
| 118 |
4 15 2 |
| 119 |
5 3 4 |
| 120 |
6 NA NA |
| 121 |
> |
| 122 |
> spix = as(sg, "SpatialPixels") |
| 123 |
> spixdf = as(sgdf, "SpatialPixelsDataFrame") |
| 124 |
> over(spix, pol) |
| 125 |
1 2 3 4 5 6 |
| 126 |
NA NA NA 2 4 NA |
| 127 |
> over(spix, poldf) |
| 128 |
z zz f |
| 129 |
1 NA NA <NA> |
| 130 |
2 NA NA <NA> |
| 131 |
3 NA NA <NA> |
| 132 |
4 15 2 q |
| 133 |
5 3 4 z |
| 134 |
6 NA NA <NA> |
| 135 |
> over(spix, poldf[1:2]) |
| 136 |
z zz |
| 137 |
1 NA NA |
| 138 |
2 NA NA |
| 139 |
3 NA NA |
| 140 |
4 15 2 |
| 141 |
5 3 4 |
| 142 |
6 NA NA |
| 143 |
> |
| 144 |
> over(pol, sg) |
| 145 |
x9 x2 x3 x4 |
| 146 |
NA 4 NA 5 |
| 147 |
> over(pol, sgdf) |
| 148 |
z f |
| 149 |
x9 NA <NA> |
| 150 |
x2 3 b |
| 151 |
x3 NA <NA> |
| 152 |
x4 2 c |
| 153 |
> over(pol, sgdf[1], fn = mean) |
| 154 |
z |
| 155 |
x9 NA |
| 156 |
x2 3 |
| 157 |
x3 NA |
| 158 |
x4 2 |
| 159 |
> |
| 160 |
> over(pol, spix) |
| 161 |
x9 x2 x3 x4 |
| 162 |
NA 4 NA 5 |
| 163 |
> over(pol, spixdf) |
| 164 |
z f |
| 165 |
x9 NA <NA> |
| 166 |
x2 3 b |
| 167 |
x3 NA <NA> |
| 168 |
x4 2 c |
| 169 |
> over(pol, spixdf[1], fn = mean) |
| 170 |
z |
| 171 |
x9 NA |
| 172 |
x2 3 |
| 173 |
x3 NA |
| 174 |
x4 2 |
| 175 |
> |
| 176 |
> over(pts, sg) |
| 177 |
a b c d e |
| 178 |
1 2 4 5 5 |
| 179 |
> over(pts, spix) |
| 180 |
a b c d e |
| 181 |
1 2 4 5 5 |
| 182 |
> over(pts, sgdf) |
| 183 |
z f |
| 184 |
a 6 a |
| 185 |
b 5 a |
| 186 |
c 3 b |
| 187 |
d 2 c |
| 188 |
e 2 c |
| 189 |
> over(pts, spixdf) |
| 190 |
z f |
| 191 |
a 6 a |
| 192 |
b 5 a |
| 193 |
c 3 b |
| 194 |
d 2 c |
| 195 |
e 2 c |
| 196 |
> |
| 197 |
> #over(pts, sg, returnList=TRUE) |
| 198 |
> #over(pts, spix, returnList=TRUE) |
| 199 |
> #over(pts, sgdf, returnList=TRUE) |
| 200 |
> #over(pts, spixdf, returnList=TRUE) |
| 201 |
> |
| 202 |
> proc.time() |
| 203 |
user system elapsed |
| 204 |
0.718 0.040 0.754 |