paleolimbot / geos

Open Source Geometry Engine ('GEOS') R API
https://paleolimbot.github.io/geos/
Other
61 stars 8 forks source link

Reference geos_unnest() in geos_make_collection() man page #68

Closed JosiahParry closed 2 years ago

JosiahParry commented 2 years ago

Request: Please reference geos_unnest() in the man page for geos_make_collection()—perhaps under the details.

Context: After creating a geometry collection using geos::geos_make_collection() I found myself casting to an sf object to use sf::st_collection_extract(). After digging through the package documentation I finally found geos_unnest().

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source 
#>   `/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/sf/shape/nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27

geo <- sf::st_centroid(nc) |> 
  sf::st_geometry()
#> Warning in st_centroid.sf(nc): st_centroid assumes attributes are constant over
#> geometries of x

tris <- geo |> 
  geos::geos_make_collection() |> 
  geos::geos_delaunay_triangles()

# this is what i was doing 
sf::st_collection_extract(sf::st_as_sf(tris))
#> Simple feature collection with 186 features and 0 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.05986 ymin: 34.07671 xmax: -75.8095 ymax: 36.49111
#> Geodetic CRS:  NAD27
#> First 10 features:
#>                          geometry
#> 1  POLYGON ((-84.05986 35.1311...
#> 2  POLYGON ((-83.82962 35.3450...
#> 3  POLYGON ((-83.82962 35.3450...
#> 4  POLYGON ((-83.82962 35.3450...
#> 5  POLYGON ((-82.70541 35.8532...
#> 6  POLYGON ((-82.70541 35.8532...
#> 7  POLYGON ((-82.70541 35.8532...
#> 8  POLYGON ((-82.70541 35.8532...
#> 9  POLYGON ((-82.70541 35.8532...
#> 10 POLYGON ((-81.49823 36.4314...

# this is what i would've preferred (and now do)
geos::geos_unnest(tris) |> 
  head()
#> <geos_geometry[6] with CRS=NAD27>
#> [1] <POLYGON ((-84.05986 35.13111, -83.74725 35.05384, -83.82962 35.34507, -84.05986 35.13111))>
#> [2] <POLYGON ((-83.82962 35.34507, -83.74725 35.05384, -83.42226 35.14516, -83.82962 35.34507))>
#> [3] <POLYGON ((-83.82962 35.34507, -83.42226 35.14516, -83.48992 35.48069, -83.82962 35.34507))>
#> [4] <POLYGON ((-83.82962 35.34507, -83.48992 35.48069, -82.70541 35.85323, -83.82962 35.34507))>
#> [5] <POLYGON ((-82.70541 35.85323, -83.48992 35.48069, -82.98086 35.55136, -82.70541 35.85323))>
#> [6] <POLYGON ((-82.70541 35.85323, -82.98086 35.55136, -82.52831 35.60684, -82.70541 35.85323))>

Created on 2022-06-17 by the reprex package (v2.0.1)

paleolimbot commented 2 years ago

Good catch! See also wk::wk_flatten() and wk::wk_collection(), too, which work on a wider variety of objects (e.g., sf, sfc, geos).