Skip to content

Commit 48aea57

Browse files
committed
make the #2308 problem user friendly
1 parent fe44040 commit 48aea57

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

R/sample.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ st_sample = function(x, size, ...) UseMethod("st_sample")
2626
#' the same as specified by \code{size}? \code{TRUE} by default. Only applies to polygons, and
2727
#' when \code{type = "random"}.
2828
#' @param progress logical; if \code{TRUE} show progress bar (only if \code{size} is a vector).
29+
#' @force logical; if `TRUE` continue when the sampled bounding box area is more than 1e4 times the area of interest, else (default) stop with an error. If this error is not justified, try setting `oriented=TRUE`, see details.
2930
#' @return an \code{sfc} object containing the sampled \code{POINT} geometries
3031
#' @details if \code{x} has dimension 2 (polygons) and geographical coordinates (long/lat), uniform random sampling on the sphere is applied, see e.g. \url{https://mathworld.wolfram.com/SpherePointPicking.html}.
3132
#'
@@ -103,7 +104,7 @@ st_sample.sf = function(x, size, ...) st_sample(st_geometry(x), size, ...)
103104
#' @export
104105
#' @name st_sample
105106
st_sample.sfc = function(x, size, ..., type = "random", exact = TRUE, warn_if_not_integer = TRUE,
106-
by_polygon = FALSE, progress = FALSE) {
107+
by_polygon = FALSE, progress = FALSE, force = FALSE) {
107108

108109
if (!missing(size) && warn_if_not_integer && any(size %% 1 != 0))
109110
warning("size is not an integer")
@@ -120,7 +121,7 @@ st_sample.sfc = function(x, size, ..., type = "random", exact = TRUE, warn_if_no
120121
res = switch(max(st_dimension(x)) + 1,
121122
st_multipoints_sample(do.call(c, x), size = size, ..., type = type),
122123
st_ll_sample(st_cast(x, "LINESTRING"), size = size, ..., type = type),
123-
st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon))
124+
st_poly_sample(x, size = size, ..., type = type, by_polygon = by_polygon, force = force))
124125
if (exact && type == "random" && all(st_geometry_type(res) == "POINT")) {
125126
diff = size - length(res)
126127
if (diff > 0) { # too few points
@@ -168,7 +169,7 @@ st_sample.bbox = function(x, size, ..., great_circles = FALSE, segments = units:
168169

169170
st_poly_sample = function(x, size, ..., type = "random",
170171
offset = st_sample(st_as_sfc(st_bbox(x)), 1)[[1]],
171-
by_polygon = FALSE, oriented = FALSE) {
172+
by_polygon = FALSE, oriented = FALSE, force = FALSE) {
172173

173174
if (by_polygon && inherits(x, "sfc_MULTIPOLYGON")) { # recurse into polygons:
174175
sum_a = units::drop_units(sum(st_area(x)))
@@ -207,6 +208,8 @@ st_poly_sample = function(x, size, ..., type = "random",
207208
if (!is.finite(a1))
208209
stop("One or more geometries have a non-finite area")
209210
global = (a1 / a0) > .9999
211+
if (a0 / a1 > 1e4 && !force)
212+
stop(paste0("sampling box is ", format(a0/a1), " times larger than sampling region;\nuse force=TRUE if you really want this, or try setting oriented=TRUE\n(after reading the documentation)"), call. = FALSE)
210213
size = round(size * a0 / a1)
211214
} else {
212215
a0 = as.numeric(st_area(st_as_sfc(bb)))

man/st_sample.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)