r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.34k stars 297 forks source link

st_is_empty() does not correctly identify empty geometries with sf>=1.0-18 #2463

Closed goergen95 closed 1 day ago

goergen95 commented 2 days ago

When reading a GeoJSON string like the one below st_is_empty() returned TRUE with sf <= 1.0-17. The example JSON is not entirely constructed, since the output of gdalinfo in some cases might return such a string which we are relying on correctly identifying as empty in mapme.biodiversity. I am not at all sure what is causing this, so I'd appreciate any help on this.

library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.9.2, PROJ 9.5.0; sf_use_s2() is TRUE
packageVersion("sf")
#> [1] '1.0.18'
json <- '{"type":"Polygon","coordinates":[[]]}'
(x <- read_sf(json, quiet = TRUE))
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS:  WGS 84
#> # A tibble: 1 × 1
#>        geometry
#>   <POLYGON [°]>
#> 1       (EMPTY)
st_is_empty(x)
#> [1] FALSE

Created on 2024-10-28 with reprex v2.1.1

goergen95 commented 2 days ago

Removing those extra [] we correctly identify the geometry as empty with sf=1.0-18. Now I am just trying to figure out what changed between releases? sfc_is_empty seems to be untouched since apprx. two years.

library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.9.2, PROJ 9.5.0; sf_use_s2() is TRUE
packageVersion("sf")
#> [1] '1.0.18'
json <- '{"type":"Polygon","coordinates":[]}'
(x <- read_sf(json, quiet = TRUE))
#> Simple feature collection with 1 feature and 0 fields (with 1 geometry empty)
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
#> Geodetic CRS:  WGS 84
#> # A tibble: 1 × 1
#>        geometry
#>   <POLYGON [°]>
#> 1         EMPTY
st_is_empty(x)
#> [1] TRUE

Created on 2024-10-29 with reprex v2.1.1

edzer commented 1 day ago

Also note the difference in WKT: (EMPTY) vs EMPTY (the former being no WKT). The change was here: https://github.com/r-spatial/sf/commit/436c2938eefac117013b446af97cb7e09488af50 in geom-predicates.R: we stop using the GEOS function but use the built-in, which is more strict.

edzer commented 1 day ago

However,

'{"type":"MultiPolygon","coordinates":[[[]]]}' |> read_sf() |> st_is_empty()
## [1] FALSE
goergen95 commented 1 day ago

Cool, thank you! Any plans on when 1.0-19 will land on CRAN?

edzer commented 20 hours ago

CRAN likes updates to occur less than bimonthly; I don't consider this a bug fix but rather a "grey zone" fix, so would like to wait a while.