diff --git a/NAMESPACE b/NAMESPACE index 0fedfbcbd..5941b4654 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -257,6 +257,7 @@ S3method(st_reverse,sfc) S3method(st_reverse,sfg) S3method(st_sample,sf) S3method(st_sample,sfc) +S3method(st_sample,sfg) S3method(st_segmentize,sf) S3method(st_segmentize,sfc) S3method(st_segmentize,sfg) diff --git a/R/sample.R b/R/sample.R index 947c2f340..2137de35e 100644 --- a/R/sample.R +++ b/R/sample.R @@ -115,6 +115,12 @@ st_sample.sfc = function(x, size, ..., type = "random", exact = TRUE) { res } +#' @export +#' @name st_sample +st_sample.sfg = function(x, size, ...) { + st_sample(st_geometry(x), size, ...) +} + st_poly_sample = function(x, size, ..., type = "random", offset = st_sample(st_as_sfc(st_bbox(x)), 1)[[1]]) { diff --git a/R/valid.R b/R/valid.R index 330f37a8e..e794c56a7 100644 --- a/R/valid.R +++ b/R/valid.R @@ -53,13 +53,14 @@ st_make_valid.sfg = function(x) { #' @export st_make_valid.sfc = function(x) { + crs = st_crs(x) x = if (sf_extSoftVersion()["GEOS"] < "3.8.0") { if (!requireNamespace("lwgeom", quietly = TRUE)) stop("lwgeom required: install that first") # nocov lwgeom::lwgeom_make_valid(x) } else CPL_geos_make_valid(x) # nocov - st_sfc(x, crs = st_crs(x)) + st_sfc(x, crs = crs) } #' @export diff --git a/man/st_sample.Rd b/man/st_sample.Rd index c36146f73..d1ae8a5d7 100644 --- a/man/st_sample.Rd +++ b/man/st_sample.Rd @@ -4,6 +4,7 @@ \alias{st_sample} \alias{st_sample.sf} \alias{st_sample.sfc} +\alias{st_sample.sfg} \title{sample points on or in (sets of) spatial features} \usage{ st_sample(x, size, ...) @@ -11,6 +12,8 @@ st_sample(x, size, ...) \method{st_sample}{sf}(x, size, ...) \method{st_sample}{sfc}(x, size, ..., type = "random", exact = TRUE) + +\method{st_sample}{sfg}(x, size, ...) } \arguments{ \item{x}{object of class \code{sf} or \code{sfc}} diff --git a/tests/sfc.R b/tests/sfc.R index a80f1ab36..86356031d 100644 --- a/tests/sfc.R +++ b/tests/sfc.R @@ -194,6 +194,7 @@ options(digits=6) x = st_sfc(st_polygon(list(rbind(c(0,1),c(90,1),c(90,90),c(0,90),c(0,1)))), crs = st_crs(4326)) #if (sf_extSoftVersion()["proj.4"] >= "4.9.0") (p <- st_sample(x, 10)) + p <- st_sample(x[[1]], 10) # sfg method x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,90),c(0,90),c(0,0))))) # NOT long/lat: p <- st_sample(x, 10) x = st_sfc(st_polygon(list(rbind(c(-180,-90),c(180,-90),c(180,90),c(-180,90),c(-180,-90)))),