Skip to content

Commit 1e44daf

Browse files
committed
add spherical options for st_sample.bbox(); #2283
1 parent b070c93 commit 1e44daf

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ S3method(st_read,default)
287287
S3method(st_reverse,sf)
288288
S3method(st_reverse,sfc)
289289
S3method(st_reverse,sfg)
290+
S3method(st_sample,bbox)
290291
S3method(st_sample,sf)
291292
S3method(st_sample,sfc)
292293
S3method(st_sample,sfg)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 1.0-15
22

3+
* add `st_sample()` method for `bbox`, with special provisions for ellipsoidal coordinates; #2283
4+
35
* documentation clean-up by @olivroy; #2266, #2285
46

57
* `st_convex_hull()` uses `s2::s2_convex_hull()` for geodetic coordinates; #2250

R/sample.R

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,27 @@ st_sample.sfg = function(x, size, ...) {
141141

142142
#' @export
143143
#' @name st_sample
144+
#' @param great_circles logical; if `TRUE`, great circle arcs are used to connect the bounding box vertices, if `FALSE` parallels (graticules)
145+
#' @param segments units, or numeric (degrees); segment sizes for segmenting a bounding box polygon if `great_circles` is `FALSE`
144146
#' @examples
145147
#' bbox = st_bbox(
146-
#' c(xmin = 16.1, xmax = 16.6, ymax = 48.6, ymin = 47.9),
147-
#' crs = st_crs(4326)
148+
#' c(xmin = 0, xmax = 40, ymax = 70, ymin = 60),
149+
#' crs = st_crs('OGC:CRS84')
148150
#' )
149-
#'
150-
#' st_sample(bbox, 5)
151-
st_sample.bbox = function(x, size, ...) {
152-
st_sample(st_as_sfc(x), size, ...)
151+
#' set.seed(13531)
152+
#' s1 = st_sample(bbox, 400)
153+
#' st_bbox(s1) # within bbox
154+
#' s2 = st_sample(bbox, 400, great_circles = TRUE)
155+
#' st_bbox(s2) # outside bbox
156+
st_sample.bbox = function(x, size, ..., great_circles = FALSE, segments = units::set_units(2, degrees)) {
157+
polygon = st_as_sfc(x)
158+
crs = st_crs(x)
159+
if (isTRUE(st_is_longlat(x)) && !great_circles) {
160+
st_crs(polygon) = NA_crs_ # to fool segmentize that we're on R2:
161+
segments = units::drop_units(units::set_units(segments, degrees))
162+
polygon = st_set_crs(st_segmentize(polygon, segments), crs)
163+
}
164+
st_sample(polygon, size, ...)
153165
}
154166

155167
st_poly_sample = function(x, size, ..., type = "random",

man/st_sample.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)