From b154f8114b07ff94037999eb35e8ee34b77e280d Mon Sep 17 00:00:00 2001 From: Anthony North Date: Wed, 31 Jan 2024 02:02:11 +0000 Subject: [PATCH] refactor stack empty handling --- R/geometry.R | 13 +++++++------ tests/testthat/test-layer_table.R | 7 ++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/R/geometry.R b/R/geometry.R index 906fcd0..20e04f6 100644 --- a/R/geometry.R +++ b/R/geometry.R @@ -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) } diff --git a/tests/testthat/test-layer_table.R b/tests/testthat/test-layer_table.R index 94bee74..03c15eb 100644 --- a/tests/testthat/test-layer_table.R +++ b/tests/testthat/test-layer_table.R @@ -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())) ) })