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 s2_cell <-> integer64 support #231

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Imports:
Rcpp,
wk (>= 0.6.0)
Suggests:
bit64,
testthat (>= 3.0.0),
vctrs
URL: https://r-spatial.github.io/s2/, https://github.com/r-spatial/s2, https://s2geometry.io/
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(as.character,s2_cell_union)
S3method(as.character,s2_geography)
S3method(as.list,s2_cell)
S3method(as_s2_cell,character)
S3method(as_s2_cell,integer64)
S3method(as_s2_cell,s2_cell)
S3method(as_s2_cell,s2_geography)
S3method(as_s2_cell,wk_xy)
Expand Down
18 changes: 18 additions & 0 deletions R/s2-cell.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,31 @@ as_s2_cell.wk_xy <- function(x, ...) {
cpp_s2_cell_from_lnglat(as_s2_lnglat(x))
}

#' @rdname s2_cell
#' @export
as_s2_cell.integer64 <- function(x, ...) {
storage <- unclass(x)
storage[is.na(x)] <- NA_real_
new_s2_cell(storage)
}

#' @rdname s2_cell
#' @export
new_s2_cell <- function(x) {
stopifnot(is.double(x))
structure(x, class = c("s2_cell", "wk_vctr"))
}

# registered in zzz.R
as.integer64.s2_cell <- function(x, ...) {
# We store 64-bit integegers the same way bit64 does so we can just set the
# class attribute and propagate NA values in the way that bit64 expects them.
x_is_na <- is.na(x)
class(x) <- "integer64"
x[x_is_na] <- bit64::NA_integer64_
x
}

#' @export
as.character.s2_cell <- function(x, ...) {
cpp_s2_cell_to_string(x)
Expand Down
2 changes: 2 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
s3_register("vctrs::vec_restore", cls)
s3_register("vctrs::vec_ptype_abbr", cls)
}

s3_register("bit64::as.integer64", "s2_cell")
}

s3_register <- function(generic, class, method = NULL) {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-s2-cell.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ test_that("s2_cell class works", {
)
})

test_that("s2_cell bit64::integer64 support works", {
cells <- c(as_s2_cell(NA_character_), s2_cell_sentinel())
int64s <- bit64::as.integer64(cells)
expect_identical(int64s, bit64::as.integer64(c(NA, -1)))
expect_identical(as_s2_cell(int64s), cells)
})

test_that("invalid and sentinel values work as expected", {
expect_false(s2_cell_is_valid(s2_cell_sentinel()))
expect_false(s2_cell_is_valid(s2_cell_invalid()))
Expand Down