From 683a548be4b70fb3bff55b6997dcd985c08ccdd4 Mon Sep 17 00:00:00 2001 From: edzer Date: Sun, 10 Dec 2023 11:39:36 +0100 Subject: [PATCH] use s2 for convex hull; #2250 --- NEWS.md | 2 ++ R/geom-transformers.R | 11 +++++++++-- man/geos_unary.Rd | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8a864328f..b316026da 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # version 1.0-15 +* `st_convex_hull()` uses `s2::s2_convex_hull()` for geodetic coordinates; #2250 + * add `directed` argument to `st_line_merge()`; #2264 * `st_union.sfc()` given `x` and `y` works consistently across geodetic and projected objects; #2262 diff --git a/R/geom-transformers.R b/R/geom-transformers.R index 2f6a76c24..e06d34f8a 100644 --- a/R/geom-transformers.R +++ b/R/geom-transformers.R @@ -182,6 +182,9 @@ st_boundary.sf = function(x) { #' nc_g = st_geometry(nc) #' plot(st_convex_hull(nc_g)) #' plot(nc_g, border = grey(.5), add = TRUE) +#' pt = st_combine(st_sfc(st_point(c(0,80)), st_point(c(120,80)), st_point(c(240,80)))) +#' st_convex_hull(pt) # R2 +#' st_convex_hull(st_set_crs(pt, 'OGC:CRS84')) # S2 st_convex_hull = function(x) UseMethod("st_convex_hull") @@ -190,8 +193,12 @@ st_convex_hull.sfg = function(x) get_first_sfg(st_convex_hull(st_sfc(x))) #' @export -st_convex_hull.sfc = function(x) - st_sfc(CPL_geos_op("convex_hull", x, numeric(0), integer(0), numeric(0), logical(0))) +st_convex_hull.sfc = function(x) { + if (isTRUE(st_is_longlat(x)) && sf_use_s2()) + st_as_sfc(s2::s2_convex_hull(x), crs = st_crs(x)) + else + st_sfc(CPL_geos_op("convex_hull", x, numeric(0), integer(0), numeric(0), logical(0))) +} #' @export st_convex_hull.sf = function(x) { diff --git a/man/geos_unary.Rd b/man/geos_unary.Rd index d73f8c10d..3e1de3ed0 100644 --- a/man/geos_unary.Rd +++ b/man/geos_unary.Rd @@ -213,6 +213,9 @@ nc = st_read(system.file("shape/nc.shp", package="sf")) nc_g = st_geometry(nc) plot(st_convex_hull(nc_g)) plot(nc_g, border = grey(.5), add = TRUE) +pt = st_combine(st_sfc(st_point(c(0,80)), st_point(c(120,80)), st_point(c(240,80)))) +st_convex_hull(pt) # R2 +st_convex_hull(st_set_crs(pt, 'OGC:CRS84')) # S2 set.seed(131) if (compareVersion(sf_extSoftVersion()[["GEOS"]], "3.11.0") > -1) { pts = cbind(runif(100), runif(100))