Skip to content

Commit 9ce3f25

Browse files
authored
Add bit64 support (#231)
1 parent 528d50e commit 9ce3f25

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Imports:
3434
Rcpp,
3535
wk (>= 0.6.0)
3636
Suggests:
37+
bit64,
3738
testthat (>= 3.0.0),
3839
vctrs
3940
URL: https://r-spatial.github.io/s2/, https://github.com/r-spatial/s2, https://s2geometry.io/

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ S3method(as.character,s2_cell_union)
1212
S3method(as.character,s2_geography)
1313
S3method(as.list,s2_cell)
1414
S3method(as_s2_cell,character)
15+
S3method(as_s2_cell,integer64)
1516
S3method(as_s2_cell,s2_cell)
1617
S3method(as_s2_cell,s2_geography)
1718
S3method(as_s2_cell,wk_xy)

R/s2-cell.R

+18
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,31 @@ as_s2_cell.wk_xy <- function(x, ...) {
8888
cpp_s2_cell_from_lnglat(as_s2_lnglat(x))
8989
}
9090

91+
#' @rdname s2_cell
92+
#' @export
93+
as_s2_cell.integer64 <- function(x, ...) {
94+
storage <- unclass(x)
95+
storage[is.na(x)] <- NA_real_
96+
new_s2_cell(storage)
97+
}
98+
9199
#' @rdname s2_cell
92100
#' @export
93101
new_s2_cell <- function(x) {
94102
stopifnot(is.double(x))
95103
structure(x, class = c("s2_cell", "wk_vctr"))
96104
}
97105

106+
# registered in zzz.R
107+
as.integer64.s2_cell <- function(x, ...) {
108+
# We store 64-bit integegers the same way bit64 does so we can just set the
109+
# class attribute and propagate NA values in the way that bit64 expects them.
110+
x_is_na <- is.na(x)
111+
class(x) <- "integer64"
112+
x[x_is_na] <- bit64::NA_integer64_
113+
x
114+
}
115+
98116
#' @export
99117
as.character.s2_cell <- function(x, ...) {
100118
cpp_s2_cell_to_string(x)

R/zzz.R

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
s3_register("vctrs::vec_restore", cls)
1111
s3_register("vctrs::vec_ptype_abbr", cls)
1212
}
13+
14+
s3_register("bit64::as.integer64", "s2_cell")
1315
}
1416

1517
s3_register <- function(generic, class, method = NULL) {

tests/testthat/test-s2-cell.R

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ test_that("s2_cell class works", {
1010
)
1111
})
1212

13+
test_that("s2_cell bit64::integer64 support works", {
14+
cells <- c(as_s2_cell(NA_character_), s2_cell_sentinel())
15+
int64s <- bit64::as.integer64(cells)
16+
expect_identical(int64s, bit64::as.integer64(c(NA, -1)))
17+
expect_identical(as_s2_cell(int64s), cells)
18+
})
19+
1320
test_that("invalid and sentinel values work as expected", {
1421
expect_false(s2_cell_is_valid(s2_cell_sentinel()))
1522
expect_false(s2_cell_is_valid(s2_cell_invalid()))

0 commit comments

Comments
 (0)