Skip to content

Commit

Permalink
bugfix on empty data. closes #112
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Jan 31, 2024
1 parent 47b7fdc commit d416b33
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 6 additions & 3 deletions R/geometry.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ interleave_xy <- function(xy, dims = "xy") {
stack_xy <- function(xy, dims = "xy") {
xy_dims <- unclass(xy)
# add / remove z
xy_dims$z <- if (dims == "xyz" || dims == "XYZ") xy_dims$z %??% 0

cbind(xy_dims$x, xy_dims$y, xy_dims$z)
# NOTE: length-0 z isn't dropped when x and y are length-0
if (dims == "xyz" || dims == "XYZ") {
cbind(xy_dims$x, xy_dims$y, xy_dims$z %??% 0)
} else {
cbind(xy_dims$x, xy_dims$y)
}
}


Expand Down
4 changes: 2 additions & 2 deletions R/layer_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ deckgl_table <- function(object, dims = "xy", ...) {
other_cols <- vctrs::vec_slice(other_cols, feature_sizes$feature_id)
}

n_features <- sum(feature_sizes$n_geom %??% nrow(data))
n_features <- sum(feature_sizes$n_geom %||% nrow(data))
list(
# number of features
length = jsonlite::unbox(n_features),
Expand Down Expand Up @@ -106,7 +106,7 @@ deckgl_geom <- function(coords, dims = "xy", ...) {
# optimised path for all length = 1 geometries
# row-major matrix is equivalent to, but
# simpler and faster to serialise than a list of coords
if (max(geom_runs$size) <= 1L) {
if (max(geom_runs$size, 0L) <= 1L) {
return(stack_xy(coords$xy, dims))
}

Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ n_unique <- function(x, na_rm = FALSE) {

vec_runs <- function(x) {
size <- vctrs::vec_run_sizes(x)
if (vctrs::vec_is_empty(size)) {
return (vctrs::new_data_frame(list(loc = integer(), size = integer())))
}

len <- length(size)
loc <- cumsum(vctrs::vec_c(1L, size[seq_len(len - 1L)]))
vctrs::new_data_frame(list(loc = loc, size = size))
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-layer_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,25 @@ test_that("deckgl_table works for multipolygons", {
)
)
})

test_that("deckgl_table works for empty data frames", {
expect_identical(
deckgl_table(vctrs::data_frame()),
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list())
)

expect_identical(
deckgl_table(vctrs::data_frame(foo = integer())),
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list(foo = integer()))
)

expect_equal(
deckgl_table(vctrs::data_frame(point = wk::wkt("POINT EMPTY"))),
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list(point = cbind(double(), double())))
)

expect_identical(
deckgl_table(vctrs::data_frame(point = wk::wkt("POINT EMPTY"), foo = integer())),
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list(point = cbind(double(), double()), foo = integer()))
)
})

0 comments on commit d416b33

Please sign in to comment.