From 13a42be1914e2cca4c460c3cadf528d8cd9abca0 Mon Sep 17 00:00:00 2001 From: edzer Date: Tue, 29 Oct 2024 13:57:35 +0100 Subject: [PATCH] MultiPolygon; closes #2463 --- src/sfc-sfg.cpp | 12 ++++++++++-- tests/sfc.R | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/sfc-sfg.cpp b/src/sfc-sfg.cpp index 5619aa469..ef0fb5619 100644 --- a/src/sfc-sfg.cpp +++ b/src/sfc-sfg.cpp @@ -119,8 +119,16 @@ LogicalVector sfc_is_empty(List sfc) { } } } - } else - is_empty = (item_len == 0) || (TYPEOF(item) == VECSXP && Rf_length(VECTOR_ELT(item, 0)) == 0); // #2463 + } else { + if (item_len == 0) + is_empty = true; + else if (TYPEOF(item) == VECSXP) { // #2463 + item = VECTOR_ELT(item, 0); + is_empty = Rf_length(item) == 0 || // e.g. POLYGON with 1 ring without coordinates + (TYPEOF(item) == VECSXP && Rf_length(VECTOR_ELT(item, 0)) == 0); // same for one level deeper, e.g. MULTIPOLYGON: + } else + is_empty = false; + } out[i] = is_empty; } diff --git a/tests/sfc.R b/tests/sfc.R index 855160f8f..7c56baa5e 100644 --- a/tests/sfc.R +++ b/tests/sfc.R @@ -360,3 +360,8 @@ st_exterior_ring(st_sf(a = 1, geom = spl1)) st_exterior_ring(smpl1[[1]]) st_exterior_ring(st_sfc(smpl1)) st_exterior_ring(st_sf(a = 1, geom = st_sfc(smpl1))) + +'{"type":"Polygon","coordinates":[[]]}' |> read_sf() |> st_is_empty() +# '{"type":"Polygon","coordinates":[]}' |> read_sf() |> st_is_empty() # breaks on GDAL < 3.9 or so +'{"type":"MultiPolygon","coordinates":[[[]]]}' |> read_sf() |> st_is_empty() +'{"type":"MultiPolygon","coordinates":[[]]}' |> read_sf() |> st_is_empty()