-
Notifications
You must be signed in to change notification settings - Fork 7
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
Handle xy(NA, NA)
as null instead of empty
#205
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #205 +/- ##
=======================================
Coverage 98.85% 98.85%
=======================================
Files 85 85
Lines 6197 6207 +10
=======================================
+ Hits 6126 6136 +10
Misses 71 71
☔ View full report in Codecov by Sentry. |
Do we need an We'll need to check over the codebase for consistency issues with empty vs null features. I've found some bugs which are hopefully easily fixed. See reprex. # pak::pkg_install("paleolimbot/wk#205")
# debug shows both NaN and NA are null feature?
wk::wk_debug(wk::xy(c(NA, NaN), c(NA, NaN)))
#> initialize (dirty = 0 -> 1)
#> vector_start: POINT[2] <0xffffe4850230> => WK_CONTINUE
#> feature_start (1): <0xffffe4850230> => WK_CONTINUE
#> null_feature => WK_CONTINUE
#> feature_end (1): <0xffffe4850230> => WK_CONTINUE
#> feature_start (2): <0xffffe4850230> => WK_CONTINUE
#> null_feature => WK_CONTINUE
#> feature_end (2): <0xffffe4850230> => WK_CONTINUE
#> vector_end: <0xffffe4850230>
#> deinitialize
#> NULL
# xy meta
testthat::expect_identical(
wk::xy(NA, NA) |>
wk::wk_meta(),
wk::wkt(NA_character_) |>
wk::wk_meta()
)
# xy meta
testthat::expect_identical(
wk::xy(NaN, NaN) |>
wk::wk_meta(),
wk::wkt("POINT EMPTY") |>
wk::wk_meta()
)
#> Error: wk::wk_meta(wk::xy(NaN, NaN)) (`actual`) not identical to wk::wk_meta(wk::wkt("POINT EMPTY")) (`expected`).
#>
#> actual vs expected
#> geometry_type size has_z has_m precision is_empty
#> - actual[1, ] NA NA NA NA NA NA
#> + expected[1, ] 1 0 FALSE FALSE 0 TRUE
#>
#> `actual$geometry_type`: NA
#> `expected$geometry_type`: 1
#>
#> `actual$size`: NA
#> `expected$size`: 0
#>
#> `actual$has_z`: <NA>
#> `expected$has_z`: FALSE
#>
#> `actual$has_m`: <NA>
#> `expected$has_m`: FALSE
#>
#> `actual$precision`: NA
#> `expected$precision`: 0
#>
#> `actual$is_empty`: <NA>
#> `expected$is_empty`: TRUE
# xy -> sfc
testthat::expect_identical(
wk::xy(NaN, NaN) |>
sf::st_as_sfc(),
wk::wkt("POINT EMPTY") |>
sf::st_as_sfc()
)
#> Error: sf::st_as_sfc(wk::xy(NaN, NaN)) (`actual`) not identical to sf::st_as_sfc(wk::wkt("POINT EMPTY")) (`expected`).
#>
#> `class(actual)`: "sfc_GEOMETRYCOLLECTION" "sfc"
#> `class(expected)`: "sfc_POINT" "sfc"
#>
#> `actual[[1]]` is an S3 object of class <XY/GEOMETRYCOLLECTION/sfg>, a list
#> `expected[[1]]` is an S3 object of class <XY/POINT/sfg>, a double vector
# xy -> sfc
# should xy <null feature> be known to be an empty point?
testthat::expect_identical(
# should this be point empty?
wk::xy(NA, NA) |>
sf::st_as_sfc(),
wk::wkt(NA_character_) |>
sf::st_as_sfc()
)
# round trip
testthat::expect_identical(
wk::xy(NA, NA) |>
sf::st_as_sfc() |>
wk::as_xy() |>
wk::wk_set_crs(NULL),
wk::xy(NA, NA)
)
# impossible -> cannot succeed
testthat::expect_identical(
wk::xy(NaN, NaN) |>
sf::st_as_sfc() |>
wk::as_xy() |>
wk::wk_set_crs(NULL),
wk::xy(NaN, NaN)
)
#> Error: wk::wk_set_crs(wk::as_xy(sf::st_as_sfc(wk::xy(NaN, NaN))), NULL) (`actual`) not identical to wk::xy(NaN, NaN) (`expected`).
#>
#> `actual$x`: NA
#> `expected$x`: NaN
#>
#> `actual$y`: NA
#> `expected$y`: NaN
# sfc -> xy. should this work?
testthat::expect_identical(
sf::st_sfc(sf::st_point(c(NaN, NaN))) |>
wk::as_xy(),
wk::xy(NaN, NaN)
)
# coords
testthat::expect_equal(
wk::xy(NA, NA) |>
wk::wk_coords(),
wk::wkt(NA_character_) |>
wk::wk_coords()
)
# coords
testthat::expect_equal(
wk::xy(NaN, NaN) |>
wk::wk_coords(),
wk::wkt("POINT EMPTY") |>
wk::wk_coords()
)
# count
testthat::expect_equal(
wk::wk_count(wk::xy(NA, NA)),
wk::wk_count(wk::wkt(NA_character_))
)
# count. xy(NaN, NaN) inconsistent with other handlers
testthat::expect_equal(
wk::wk_count(wk::xy(NaN, NaN)),
wk::wk_count(wk::wkt("POINT EMPTY"))
)
#> Error: wk::wk_count(wk::xy(NaN, NaN)) (`actual`) not equal to wk::wk_count(wk::wkt("POINT EMPTY")) (`expected`).
#>
#> actual vs expected
#> n_geom
#> - actual[1, ] 0
#> + expected[1, ] 1
#>
#> `actual$n_geom`: 0
#> `expected$n_geom`: 1 Created on 2023-10-16 with reprex v2.0.2 |
I think we should expect # pak::pkg_install("paleolimbot/wk#205")
# sf POINT EMPTY
testthat::expect_identical(
sf::st_sfc(sf::st_point(c(NA_real_, NA_real_))) |>
wk::as_xy(),
wk::xy(NaN, NaN)
)
#> Error: wk::as_xy(sf::st_sfc(sf::st_point(c(NA_real_, NA_real_)))) (`actual`) not identical to wk::xy(NaN, NaN) (`expected`).
#>
#> `actual$x`: NA
#> `expected$x`: NaN
#>
#> `actual$y`: NA
#> `expected$y`: NaN
# already works
testthat::expect_identical(
sf::st_sfc(sf::st_point(c(NaN, NaN))) |>
wk::as_xy(),
wk::xy(NaN, NaN)
)
testthat::expect_identical(
wk::wk_handle(
sf::st_sfc(sf::st_point(c(NA_real_, NA_real_))),
wk::xy_writer()
),
wk::xy(NaN, NaN)
)
#> Error: wk::wk_handle(...) (`actual`) not identical to wk::xy(NaN, NaN) (`expected`).
#>
#> `actual$x`: NA
#> `expected$x`: NaN
#>
#> `actual$y`: NA
#> `expected$y`: NaN
testthat::expect_identical(
wk::wk_handle(
sf::st_sfc(sf::st_point(c(NaN, NaN))),
wk::xy_writer()
),
wk::xy(NaN, NaN)
)
#> Error: wk::wk_handle(sf::st_sfc(sf::st_point(c(NaN, NaN))), wk::xy_writer()) (`actual`) not identical to wk::xy(NaN, NaN) (`expected`).
#>
#> `actual$x`: NA
#> `expected$x`: NaN
#>
#> `actual$y`: NA
#> `expected$y`: NaN Created on 2023-10-16 with reprex v2.0.2 |
Thank you for taking a look! I think the main thing here is that
I don't think so...while a point can be missing, I'm not sure that it can be NaN in the same way that a number can be NaN.
I may fix that one separately...I think that happens because when all the geometries are empty, the sfc writer doesn't know what geometry to pick. In this case, we also have geometry type at the vector level and I think I can rig something to use that as a fallback. |
Ok, that turned into a bit of a rabbit hole, but I think it's much better. One consequence of this was that I noticed |
Looking good. I've noticed that sf::st_as_sfc(wk::xy(NaN, NaN)) fails. This is because the default behaviour of sf:::st_as_sf.data.frame() fails if na values are present. This isn't consistent with other formats, so perhaps it's worth patching? # pak::pkg_install("paleolimbot/wk#205")
testthat::local_edition(3)
testthat::expect_identical(
wk::wk_handle(
wk::xy(NaN, NaN),
wk::sfc_writer()
),
sf::st_sfc(sf::st_point()),
ignore_attr = c("crs", "bbox")
)
# should we default na.fail = FALSE for consistency?
testthat::expect_identical(
wk::xy(NaN, NaN) |>
sf::st_as_sfc(),
sf::st_sfc(sf::st_point()),
ignore_attr = c("crs", "bbox")
)
#> Error in st_as_sf.data.frame(as.data.frame(x), coords = xy_dims(x), crs = sf_crs_from_wk(x)): missing values in coordinates not allowed
testthat::expect_identical(
{
xys <- wk::xy(NaN, NaN)
coords <- as.data.frame(xys)
# also need to convert NaN to NA?
coords <- as.data.frame(lapply(coords, \(x) replace(x, is.nan(x), NA_real_)))
sf::st_as_sf(coords, coords = wk::xy_dims(xys), crs = sf::st_crs(wk::wk_crs(xys)), na.fail = FALSE) |>
sf::st_geometry()
},
sf::st_sfc(sf::st_point()),
# these appear to be inconsistencies in sf itself?
ignore_attr = c("bbox", "n_empty")
)
#> Warning in min(cc[[1]], na.rm = TRUE): no non-missing arguments to min;
#> returning Inf
#> Warning in min(cc[[2]], na.rm = TRUE): no non-missing arguments to min;
#> returning Inf
#> Warning in max(cc[[1]], na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
#> Warning in max(cc[[2]], na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
testthat::expect_identical(
wk::xy(NaN, NaN) |>
wk::as_wkb() |>
sf::st_as_sfc(),
sf::st_sfc(sf::st_point()),
ignore_attr = c("crs", "bbox")
) Created on 2023-10-18 with reprex v2.0.2 Edit: fix reprex |
I've found an inconsistency between wk_xy.sfc and using a handler. # pak::pkg_install("paleolimbot/wk#205")
testthat::expect_identical(
wk::as_xy(sf::st_sfc(sf::st_point())),
wk::xy(NaN, NaN)
)
#> Error: wk::as_xy(sf::st_sfc(sf::st_point())) not identical to wk::xy(NaN, NaN).
#> Objects equal but not identical
testthat::expect_identical(
wk::wk_handle(
sf::st_sfc(sf::st_point()),
wk::xy_writer()
),
wk::xy(NaN, NaN)
) Created on 2023-10-18 with reprex v2.0.2 |
Thanks again! I still have to add a few tests and fix the last issue you identified. I don't think I ever would have spotted those! |
@anthonynorth do you forsee problems with this? I would like to preserve the invariant that a "null feature" lines up exactly with
is.na(x)
.Before:
Created on 2023-10-15 with reprex v2.0.2
After:
Created on 2023-10-15 with reprex v2.0.2