| 1 |
|
| 2 |
R version 3.2.2 (2015-08-14) -- "Fire Safety" |
| 3 |
Copyright (C) 2015 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 |
> library(sp) |
| 19 |
> x = c(0.5, 0.5, 1.2, 1.5) |
| 20 |
> y = c(1.5, 0.5, 0.5, 0.5) |
| 21 |
> xy = cbind(x,y) |
| 22 |
> dimnames(xy)[[1]] = c("a", "b", "c", "d") |
| 23 |
> pts = SpatialPoints(xy) |
| 24 |
> |
| 25 |
> xpol = c(0,1,1,0,0) |
| 26 |
> ypol = c(0,0,1,1,0) |
| 27 |
> pol = SpatialPolygons(list( |
| 28 |
+ Polygons(list(Polygon(cbind(xpol-1.05,ypol))), ID="x1"), |
| 29 |
+ Polygons(list(Polygon(cbind(xpol,ypol))), ID="x2"), |
| 30 |
+ Polygons(list(Polygon(cbind(xpol,ypol-1.05))), ID="x3"), |
| 31 |
+ Polygons(list(Polygon(cbind(xpol+1.05,ypol))), ID="x4"), |
| 32 |
+ Polygons(list(Polygon(cbind(xpol+.4, ypol+.1))), ID="x5") |
| 33 |
+ )) |
| 34 |
> |
| 35 |
> zdf = data.frame(z1 = 1:4, z2=4:1, f = c("a", "a", "b", "b"), |
| 36 |
+ row.names = c("a", "b", "c", "d")) |
| 37 |
> zdf |
| 38 |
z1 z2 f |
| 39 |
a 1 4 a |
| 40 |
b 2 3 a |
| 41 |
c 3 2 b |
| 42 |
d 4 1 b |
| 43 |
> ptsdf = SpatialPointsDataFrame(pts, zdf) |
| 44 |
> |
| 45 |
> zpl = data.frame(z = c(10, 15, 25, 3, 0), zz=1:5, |
| 46 |
+ f = c("z", "q", "r", "z", "q"), row.names = c("x1", "x2", "x3", "x4", "x5")) |
| 47 |
> zpl |
| 48 |
z zz f |
| 49 |
x1 10 1 z |
| 50 |
x2 15 2 q |
| 51 |
x3 25 3 r |
| 52 |
x4 3 4 z |
| 53 |
x5 0 5 q |
| 54 |
> poldf = SpatialPolygonsDataFrame(pol, zpl) |
| 55 |
> |
| 56 |
> gt = GridTopology(c(.5,.5), c(1,1), c(3,2)) |
| 57 |
> sg = SpatialGrid(gt) |
| 58 |
> df6 = data.frame(z = 6:1, f = c("a", "a", "b", "b", "c", "c")) |
| 59 |
> sgdf = SpatialGridDataFrame(gt, df6) |
| 60 |
> over(sg, pol) |
| 61 |
1 2 3 4 5 6 |
| 62 |
NA NA NA 5 4 NA |
| 63 |
> over(sg, poldf) |
| 64 |
z zz f |
| 65 |
1 NA NA <NA> |
| 66 |
2 NA NA <NA> |
| 67 |
3 NA NA <NA> |
| 68 |
4 15 2 q |
| 69 |
5 3 4 z |
| 70 |
6 NA NA <NA> |
| 71 |
> over(sg, poldf[1:2]) |
| 72 |
z zz |
| 73 |
1 NA NA |
| 74 |
2 NA NA |
| 75 |
3 NA NA |
| 76 |
4 15 2 |
| 77 |
5 3 4 |
| 78 |
6 NA NA |
| 79 |
> |
| 80 |
> spix = as(sg, "SpatialPixels") |
| 81 |
> spixdf = as(sgdf, "SpatialPixelsDataFrame") |
| 82 |
> over(spix, pol) |
| 83 |
1 2 3 4 5 6 |
| 84 |
NA NA NA 5 4 NA |
| 85 |
> over(spix, poldf) |
| 86 |
z zz f |
| 87 |
1 NA NA <NA> |
| 88 |
2 NA NA <NA> |
| 89 |
3 NA NA <NA> |
| 90 |
4 15 2 q |
| 91 |
5 3 4 z |
| 92 |
6 NA NA <NA> |
| 93 |
> over(spix, poldf[1:2]) |
| 94 |
z zz |
| 95 |
1 NA NA |
| 96 |
2 NA NA |
| 97 |
3 NA NA |
| 98 |
4 15 2 |
| 99 |
5 3 4 |
| 100 |
6 NA NA |
| 101 |
> |
| 102 |
> over(pol, sg) |
| 103 |
x1 x2 x3 x4 x5 |
| 104 |
NA 4 NA 5 4 |
| 105 |
> over(pol, sgdf) |
| 106 |
z f |
| 107 |
x1 NA <NA> |
| 108 |
x2 3 b |
| 109 |
x3 NA <NA> |
| 110 |
x4 2 c |
| 111 |
x5 3 b |
| 112 |
> over(pol, sgdf[1], fn = mean) |
| 113 |
z |
| 114 |
x1 NA |
| 115 |
x2 3 |
| 116 |
x3 NA |
| 117 |
x4 2 |
| 118 |
x5 3 |
| 119 |
> |
| 120 |
> over(pol, spix) |
| 121 |
x1 x2 x3 x4 x5 |
| 122 |
NA 4 NA 5 4 |
| 123 |
> over(pol, spixdf) |
| 124 |
z f |
| 125 |
x1 NA <NA> |
| 126 |
x2 3 b |
| 127 |
x3 NA <NA> |
| 128 |
x4 2 c |
| 129 |
x5 3 b |
| 130 |
> over(pol, spixdf[1], fn = mean) |
| 131 |
z |
| 132 |
x1 NA |
| 133 |
x2 3 |
| 134 |
x3 NA |
| 135 |
x4 2 |
| 136 |
x5 3 |
| 137 |
> |
| 138 |
> over(pts, sg) |
| 139 |
a b c d |
| 140 |
1 4 5 5 |
| 141 |
> over(pts, spix) |
| 142 |
a b c d |
| 143 |
1 4 5 5 |
| 144 |
> over(pts, sgdf) |
| 145 |
z f |
| 146 |
a 6 a |
| 147 |
b 3 b |
| 148 |
c 2 c |
| 149 |
d 2 c |
| 150 |
> over(pts, spixdf) |
| 151 |
z f |
| 152 |
a 6 a |
| 153 |
b 3 b |
| 154 |
c 2 c |
| 155 |
d 2 c |
| 156 |
> |
| 157 |
> over(sg, sg) |
| 158 |
1 2 3 4 5 6 |
| 159 |
1 2 3 4 5 6 |
| 160 |
> over(sg, spix) |
| 161 |
1 2 3 4 5 6 |
| 162 |
1 2 3 4 5 6 |
| 163 |
> over(sg, sgdf) |
| 164 |
z f |
| 165 |
1 6 a |
| 166 |
2 5 a |
| 167 |
3 4 b |
| 168 |
4 3 b |
| 169 |
5 2 c |
| 170 |
6 1 c |
| 171 |
> over(sg, spixdf) |
| 172 |
z f |
| 173 |
1 6 a |
| 174 |
2 5 a |
| 175 |
3 4 b |
| 176 |
4 3 b |
| 177 |
5 2 c |
| 178 |
6 1 c |
| 179 |
> |
| 180 |
> over(spix, sg) |
| 181 |
1 2 3 4 5 6 |
| 182 |
1 2 3 4 5 6 |
| 183 |
> over(spix, spix) |
| 184 |
1 2 3 4 5 6 |
| 185 |
1 2 3 4 5 6 |
| 186 |
> over(spix, sgdf) |
| 187 |
z f |
| 188 |
1 6 a |
| 189 |
2 5 a |
| 190 |
3 4 b |
| 191 |
4 3 b |
| 192 |
5 2 c |
| 193 |
6 1 c |
| 194 |
> over(spix, spixdf) |
| 195 |
z f |
| 196 |
1 6 a |
| 197 |
2 5 a |
| 198 |
3 4 b |
| 199 |
4 3 b |
| 200 |
5 2 c |
| 201 |
6 1 c |
| 202 |
> |
| 203 |
> proc.time() |
| 204 |
user system elapsed |
| 205 |
0.536 0.281 0.506 |