From 3ec76b872491b7ee1e89f47c2da06f9a88dacd4c Mon Sep 17 00:00:00 2001 From: Edzer Pebesma Date: Thu, 29 Jun 2023 20:51:55 +0200 Subject: [PATCH] modifications handling c() and st_distance --- R/bbox.R | 4 ++-- R/geom-measures.R | 1 + R/sfc.R | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/R/bbox.R b/R/bbox.R index e65a48073..dcc9e4e8f 100644 --- a/R/bbox.R +++ b/R/bbox.R @@ -10,10 +10,10 @@ bb_wrap = function(bb) { } bbox.pointmatrix = function(obj, ...) { - if (nrow(obj) == 0) + if (nrow(obj) == 0 || all(is.na(obj))) bb_wrap(rep(NA_real_, 4)) else - bb_wrap(as.vector(t(apply(obj[,1:2,drop=FALSE], 2, range)))) + bb_wrap(as.vector(t(apply(obj[,1:2,drop=FALSE], 2, range, na.rm = TRUE)))) } bbox.Set = function(obj, ...) { diff --git a/R/geom-measures.R b/R/geom-measures.R index 2851f0c7d..90852c7ec 100644 --- a/R/geom-measures.R +++ b/R/geom-measures.R @@ -181,6 +181,7 @@ st_distance = function(x, y, ..., dist_fun, by_element = FALSE, else CPL_geos_dist(x, y, which, par) } + d[is.nan(d)] = NA_real_ if (!is.null(u <- st_crs(x)$ud_unit)) units(d) = u d diff --git a/R/sfc.R b/R/sfc.R index 3b94911b0..2973e7092 100644 --- a/R/sfc.R +++ b/R/sfc.R @@ -186,8 +186,17 @@ c.sfc = function(..., recursive = FALSE) { else c(ucls, "sfc") + points_attr = sapply(lst, function(x) !is.null(attr(x, "points"))) + if (any(points_attr) && !all(points_attr)) { + for (i in seq_along(lst)) + lst[[i]] = lst[[i]][] # realize + points_attr = FALSE + } + ret = unlist(lapply(lst, unclass), recursive = FALSE) attributes(ret) = attributes(lst[[1]]) # crs + if (all(points_attr)) + attr(ret, "points") = do.call(rbind, lapply(lst, attr, "points")) class(ret) = cls attr(ret, "bbox") = compute_bbox(ret) # dispatch on class attr(ret, "n_empty") = sum(sapply(lst, attr, which = "n_empty"))