Skip to content

Commit

Permalink
refactor stack empty handling
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Jan 31, 2024
1 parent d416b33 commit b154f81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 7 additions & 6 deletions R/geometry.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ interleave_xy <- function(xy, dims = "xy") {

# stack xy[z] coordinates
stack_xy <- function(xy, dims = "xy") {
use_z <- dims == "xyz" || dims == "XYZ"
if (vctrs::vec_is_empty(xy)) {
return(matrix(double(), ncol = 2L + use_z))
}

xy_dims <- unclass(xy)
# add / remove 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)
}
xy_dims$z <- if (use_z) xy_dims$z %??% 0
cbind(xy_dims$x, xy_dims$y, xy_dims$z)
}


Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-layer_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,14 @@ test_that("deckgl_table works for empty data frames", {
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list(foo = integer()))
)

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

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()))
list(length = jsonlite::unbox(0L), lengths = NULL, columns = list(point = matrix(double(), ncol = 2L), foo = integer()))
)
})

0 comments on commit b154f81

Please sign in to comment.