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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> suppressPackageStartupMessages(library(spatstat))
> suppressPackageStartupMessages(library(sf))
>
> data(chicago)
> st_as_sf(chicago)
Simple feature collection with 620 features and 4 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 0.3893523 ymin: 153.1034 xmax: 1281.986 ymax: 1276.56
CRS: NA
First 10 features:
label seg tp marks geom
1 window NA NA <NA> POLYGON ((0.3893523 153.103...
2 segment NA NA <NA> LINESTRING (0.3894739 1253....
3 segment NA NA <NA> LINESTRING (109.683 1251.77...
4 segment NA NA <NA> LINESTRING (109.683 1251.77...
5 segment NA NA <NA> LINESTRING (198.1486 1276.5...
6 segment NA NA <NA> LINESTRING (197.9988 1251.1...
7 segment NA NA <NA> LINESTRING (290.4787 1276.5...
8 segment NA NA <NA> LINESTRING (288.9907 1250.5...
9 segment NA NA <NA> LINESTRING (380.1326 1276.5...
10 segment NA NA <NA> LINESTRING (379.9827 1249.8...
> # ppp:
> g = gorillas
> st_as_sf(g)
Simple feature collection with 648 features and 4 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 580457.9 ymin: 674172.8 xmax: 585934 ymax: 678739.2
CRS: NA
First 10 features:
label group season date geom
NA window <NA> <NA> <NA> POLYGON ((584712 674237.1, ...
1 point major dry 2006-01-06 POINT (582518.4 676886.2)
2 point major dry 2006-01-10 POINT (581823 677422.7)
3 point major dry 2006-01-15 POINT (582131 676937.9)
4 point major dry 2006-01-24 POINT (582111.9 677420)
5 point minor dry 2006-01-27 POINT (582585.1 677509.7)
6 point major dry 2006-01-28 POINT (582302.3 677521.6)
7 point major dry 2006-02-01 POINT (583167.2 676730.5)
8 point major dry 2006-02-03 POINT (583584.5 677207.1)
9 point major dry 2006-02-13 POINT (583117.8 676850.3)
> marks(g) = NULL
> st_as_sf(g)
Simple feature collection with 648 features and 1 field
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 580457.9 ymin: 674172.8 xmax: 585934 ymax: 678739.2
CRS: NA
First 10 features:
label geom
1 window POLYGON ((584712 674237.1, ...
2 point POINT (582518.4 676886.2)
3 point POINT (581823 677422.7)
4 point POINT (582131 676937.9)
5 point POINT (582111.9 677420)
6 point POINT (582585.1 677509.7)
7 point POINT (582302.3 677521.6)
8 point POINT (583167.2 676730.5)
9 point POINT (583584.5 677207.1)
10 point POINT (583117.8 676850.3)
>
> # multipolygon: https://github.com/r-spatial/sf/issues/1161
> window = read_sf(system.file("shape/nc.shp", package = "sf")) %>%
+ st_transform(32119)
>
> win = spatstat::as.owin(window)
>
> set.seed(1331)
> pp2a = runifpoint(n = 50, win = win)
> print(st_as_sf(pp2a))
Simple feature collection with 51 features and 1 field
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9
CRS: NA
First 10 features:
label geom
1 window MULTIPOLYGON (((886136 3141...
2 point POINT (339121.3 257815.9)
3 point POINT (827443.1 246572.7)
4 point POINT (451340 207948)
5 point POINT (268749.6 203327.8)
6 point POINT (516677.7 198560.6)
7 point POINT (692368 238647.5)
8 point POINT (843281.2 287246)
9 point POINT (648479.4 235471)
10 point POINT (852595.8 267252.6)
>
> # st_sample going the spatstat way
> x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0)))))
> try(pts <- st_sample(x, type = "thomas"))
Error in st_poly_sample(x, size = size, ..., type = type) :
rthomas is not an exported function from spatstat.
> try(pts <- st_sample(x, kappa = 1, mu = 10, type = "Thomas"))
Error in st_poly_sample(x, size = size, ..., type = type) :
The spatstat function rThomas did not return a valid result. Consult the help file.
Error message from spatstat:
Error : 'scale' should be a single number
> # points expected
> set.seed(1331)
> pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas")
> #plot(x)
> #plot(pts, add = TRUE)
> pts
Simple feature collection with 597 features and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: 1.213108 ymin: 0.02200954 xmax: 9.994943 ymax: 8.82534
CRS: NA
First 10 features:
label geom
2 point POINT (9.076646 8.661168)
3 point POINT (9.347089 8.800523)
4 point POINT (9.207624 8.82534)
5 point POINT (9.403886 8.666932)
6 point POINT (9.437082 8.63911)
7 point POINT (9.254756 8.573871)
8 point POINT (9.29172 8.678031)
9 point POINT (9.735812 1.621866)
10 point POINT (9.853825 1.616409)
11 point POINT (9.665138 1.593111)
>
> # see https://github.com/r-spatial/sf/issues/1233
> # png("/tmp/spa%03d.png")
>
> p1 = st_point(0:1)
> p2 = st_point(1:2)
> p3 = st_point(c(-1,2))
> p = st_sfc(p1, p2, p3)
> as.ppp(p)
Planar point pattern: 3 points
window: rectangle = [-1, 1] x [1, 2] units
> try(as.ppp(st_set_crs(p, 4326)))
Error in as.ppp.sfc(st_set_crs(p, 4326)) :
Only projected coordinates may be converted to spatstat class objects
>
> sf = st_sf(geom = p)
> try(as.ppp(sf))
Planar point pattern: 3 points
window: rectangle = [-1, 1] x [1, 2] units
> sf = st_sf(a = 1:3, geom = p)
> as.ppp(sf)
Marked planar point pattern: 3 points
marks are numeric, of storage type 'integer'
window: rectangle = [-1, 1] x [1, 2] units
> sf = st_sf(a = 1:3, b=3:1, geom = p)
> as.ppp(sf) # warns
Marked planar point pattern: 3 points
marks are numeric, of storage type 'integer'
window: rectangle = [-1, 1] x [1, 2] units
Warning message:
In as.ppp.sf(sf) : only first attribute column is used for marks
>
> w = st_as_sfc(st_bbox(st_sfc(p1, p2)))
> sf = st_sf(a = 1:3, geom = p)
> (p0 = rbind(st_sf(a = 0, geom = w), sf))
Simple feature collection with 4 features and 1 field
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -1 ymin: 1 xmax: 1 ymax: 2
CRS: NA
a geom
1 0 POLYGON ((0 1, 1 1, 1 2, 0 ...
2 1 POINT (0 1)
3 2 POINT (1 2)
4 3 POINT (-1 2)
> try(as.ppp(p0)) # errors: one point outside window
Error in `marks<-.ppp`(`*tmp*`, value = value) :
number of rows of data frame != number of points
In addition: Warning message:
1 point was rejected as lying outside the specified window
>
> w = st_as_sfc(st_bbox(p))
> sf = st_sf(a = 1:3, geom = p)
> (p0 = rbind(st_sf(a = 0, geom = w), sf))
Simple feature collection with 4 features and 1 field
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -1 ymin: 1 xmax: 1 ymax: 2
CRS: NA
a geom
1 0 POLYGON ((-1 1, 1 1, 1 2, -...
2 1 POINT (0 1)
3 2 POINT (1 2)
4 3 POINT (-1 2)
> as.ppp(p0)
Marked planar point pattern: 3 points
marks are numeric, of storage type 'double'
window: polygonal boundary
enclosing rectangle: [-1, 1] x [1, 2] units
>
> # as.owin.sf, as.owin.sfc_*
> nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), check_ring_dir = TRUE)
Reading layer `nc.gpkg' from data source `/home/edzer/git/sf.Rcheck/sf/gpkg/nc.gpkg' using driver `GPKG'
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
> try(as.owin(nc)) # should be projected
Error in as.owin.sfc_MULTIPOLYGON(st_cast(W, "MULTIPOLYGON"), ...) :
Only projected coordinates may be converted to spatstat class objects
> nc = st_transform(nc, 32119)
> plot(as.owin(nc), col = 'grey')
> plot(as.owin(st_geometry(nc)), col = 'grey')
>
> sq = rbind(c(-1,-1), c(1, -1), c(1,1), c(-1,1), c(-1,-1))
> pol = st_polygon(list(0.5 * sq, sq[5:1,] * 0.45)) # w hole
> plot(as.owin(pol), col = 'grey')
> plot(as.owin(st_sfc(pol)), col = 'grey')
> mpol = st_multipolygon(list(
+ list(sq, sq[5:1,] * 0.9),
+ list(sq * 2, sq[5:1,] * 1.8)))
> plot(as.owin(mpol), col = 'grey')
> plot(as.owin(st_sfc(mpol)), col = 'grey')
> plot(as.owin(st_sfc(pol, mpol)), col = 'grey')
> plot(as.owin(st_sf(a=1:2, st_sfc(pol, mpol))), col = 'grey')
> (o = as.owin(st_sf(a=1:2, st_sfc(pol, mpol))))
window: polygonal boundary
enclosing rectangle: [-2, 2] x [-2, 2] units
> st_as_sfc(o)
Geometry set for 1 feature
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -2 ymin: -2 xmax: 2 ymax: 2
CRS: NA
MULTIPOLYGON (((2 2, -2 2, -2 -2, 2 -2, 2 2), (...
>
> plot(st_as_sfc(o), col = 'blue', main = 'st_as_sfc(o)')
> plot(st_as_sf(o), col = 'blue', main = 'st_as_sf(o)')
>
> data(nztrees)
> qNZ <- quadratcount(nztrees, nx=4, ny=3)
> ts = as.tess(qNZ)
> plot(st_as_sfc(ts))
>
> ls = st_linestring(rbind(c(0,0), c(1,1), c(2,0)))
> plot(as.psp(ls))
> mls = st_multilinestring(list(rbind(c(0,0), c(1,1), c(2,0)), rbind(c(3,3), c(4,2))))
> plot(as.psp(mls))
>
> plot(as.psp(st_sfc(ls)))
> plot(as.psp(st_sfc(mls)))
> plot(as.psp(st_sfc(ls, mls)))
>
> sf = st_sf(st_cast(st_sfc(ls, mls), "MULTILINESTRING"), marks = 1:2, foo = 2:1)
> as.psp(sf) # picks marks itself
marked planar line segment pattern: 5 line segments
Mark variables: marks, foo
window: rectangle = [0, 4] x [0, 3] units
> as.psp(sf, marks = 5:1)
marked planar line segment pattern: 5 line segments
marks are numeric, of type 'integer'
window: rectangle = [0, 4] x [0, 3] units
>
> (x = st_as_sf(as.psp(sf)))
Simple feature collection with 6 features and 3 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 4 ymax: 3
CRS: NA
label marks foo geom
1 window NA NA POLYGON ((0 0, 4 0, 4 3, 0 ...
2 segment 1 2 LINESTRING (0 0, 1 1)
3 segment 1 2 LINESTRING (1 1, 2 0)
4 segment 2 1 LINESTRING (0 0, 1 1)
5 segment 2 1 LINESTRING (1 1, 2 0)
6 segment 2 1 LINESTRING (3 3, 4 2)
> (y = st_as_sfc(as.psp(sf)))
Geometry set for 6 features
geometry type: GEOMETRY
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 4 ymax: 3
CRS: NA
First 5 geometries:
POLYGON ((0 0, 4 0, 4 3, 0 3, 0 0))
LINESTRING (0 0, 1 1)
LINESTRING (1 1, 2 0)
LINESTRING (0 0, 1 1)
LINESTRING (1 1, 2 0)
> all.equal(st_geometry(x), y)
[1] TRUE
>
> proc.time()
user system elapsed
2.022 0.126 2.145
|