Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wk_is_empty() handler / utility? #197

Closed
anthonynorth opened this issue Oct 3, 2023 · 0 comments · Fixed by #199
Closed

Add wk_is_empty() handler / utility? #197

anthonynorth opened this issue Oct 3, 2023 · 0 comments · Fixed by #199

Comments

@anthonynorth
Copy link
Contributor

anthonynorth commented Oct 3, 2023

Could we add an "is empty" handler or utility wrapper over wk_count()? The following works, but may be inefficient, particularly for WKT.

wk_is_empty <- function(handleable) {
  wk::wk_count(handleable)$n_coord == 0L
}

AFAIK, wk_meta() (which should generally be faster?) cannot currently be used for this purpose, since it doesn't read any coordinates. wk_meta() gives a correct size for POINT EMPTY, MULTIPOINT EMPTY etc, but not for geometry collections.

# if wk_meta is modified to read at least one non-empty coord
wk_is_empty2 <- function(handleable) {
  size <- wk::wk_meta(handleable)$size
  !is.na(size) & size == 0L
}
# empty
wk::wk_meta(wk::wkt("GEOMETRYCOLLECTION (POINT EMPTY)"))
#>   geometry_type size has_z has_m srid precision
#> 1             7   NA FALSE FALSE   NA         0
wk::wk_count(wk::wkt("GEOMETRYCOLLECTION (POINT EMPTY)"))
#>   n_geom n_ring n_coord
#> 1      2      0       0
# not empty
wk::wk_meta(wk::wkt("GEOMETRYCOLLECTION (POINT (1 1))"))
#>   geometry_type size has_z has_m srid precision
#> 1             7   NA FALSE FALSE   NA         0
wk::wk_count(wk::wkt("GEOMETRYCOLLECTION (POINT (1 1))"))
#>   n_geom n_ring n_coord
#> 1      2      0       1

wk::wk_meta(wk::wkt("POINT EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             1    0 FALSE FALSE   NA         0
wk::wk_meta(wk::wkt("MULTIPOINT EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             4    0 FALSE FALSE   NA         0
wk::wk_meta(wk::wkt("LINESTRING EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             2    0 FALSE FALSE   NA         0
wk::wk_meta(wk::wkt("MULTILINESTRING EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             5    0 FALSE FALSE   NA         0
wk::wk_meta(wk::wkt("POLYGON EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             3    0 FALSE FALSE   NA         0
wk::wk_meta(wk::wkt("MULTIPOLYGON EMPTY"))
#>   geometry_type size has_z has_m srid precision
#> 1             6    0 FALSE FALSE   NA         0

Created on 2023-10-03 with reprex v2.0.2

edit: clarity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant