paleolimbot / geos

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

Crash when converting USA states/counties #56

Closed paleolimbot closed 2 years ago

paleolimbot commented 2 years ago
library(sf)
library(geos)

# USA states
# https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_1_sf.rds

states <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_USA_1_sf.rds"))
# fix old-style CRS object
attr(states$geometry, "crs") <- st_crs("OGC:CRS84")

# in various formats
states_sf_geod <- states$geometry
states_sf_cart <- st_set_crs(states_sf_geod, st_crs(NA))
states_s2 <- as_s2_geography(states_sf_geod)
states_geos <- as_geos_geometry(states_sf_cart)
christopherkenny commented 2 years ago

If another data point is helpful, I am finding a similar issue where R crashes completely on running the following:

al <- tigris::voting_districts('AL')
algeos <- geos::as_geos_geometry(al)

(Can't submit a reprex because R crashes.)

paleolimbot commented 2 years ago

Thanks for adding this! Until I fix this, the workaround is to go through st_as_binary() and geos_read_wkb() like this:

al <- tigris::voting_districts('AL')
algeos <- geos::geos_read_wkb(sf::st_as_binary(tigris::voting_districts('AL')$geometry))
head(algeos)
#> <geos_geometry[6]>
#> [1] <MULTIPOLYGON [-86.909 31.870...-86.840 31.962]>
#> [2] <MULTIPOLYGON [-87.927 32.038...-87.745 32.191]>
#> [3] <MULTIPOLYGON [-88.052 32.030...-87.890 32.174]>
#> [4] <MULTIPOLYGON [-88.020 32.139...-87.854 32.308]>
#> [5] <MULTIPOLYGON [-87.877 32.343...-87.827 32.460]>
#> [6] <MULTIPOLYGON [-88.118 31.990...-87.950 32.129]>

Created on 2021-10-24 by the reprex package (v2.0.1)

christopherkenny commented 2 years ago

Thanks for the workaround! The package looks great. Planning on using this and your libgeos. After some testing, it seems like everything is extremely fast.

paleolimbot commented 2 years ago

Good to know! There's a few things that are slower (geos_intersects_matrix() is the one I know about)...let me know if you find anything else or find any problems!

paleolimbot commented 2 years ago

This crash stopped happening on my computer, so I'm going to fall back to using geos_read_wkb() for the next release and re-evaluate this later. This will slow down sf -> geos by about 4x (you can do wk::wk_handle(sf_object, geos_geometry_writer()) if you want the fast version that possibly crashes)

christopherkenny commented 2 years ago

Also working for me using the most recent version on GitHub. Thanks!