Skip to content

Commit 6d3e931

Browse files
committed
st_sample with type="hexagon" and n=1; fixes #1945
1 parent 1b77b9c commit 6d3e931

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Suggests:
8383
Matrix,
8484
microbenchmark,
8585
odbc,
86+
pbapply,
8687
pillar,
8788
pool,
8889
raster,

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# version 1.0-8
22

3+
* fix `st_sample()` with `type = "hexagonal"` for corner case (n=1), add a `progress` argument for a progress bar; #1945
4+
5+
* add package `pbapply` to Suggests; #1945
6+
37
* add pdf driver to windows build; #1942
48

59
* clarify `pipeline` argument in `st_transform()` when axis order is ambiguous; #1934
610

711
* handle argument `xpd` in calls to `plot.sfc_POLYGON()` and `plot.sfc_MULTIPOLYGON()`
812

9-
* `pivot_wider()` method added, by Henning Teickner; #1915
13+
* add `pivot_wider()` method, by Henning Teickner; #1915
1014

1115
* add `gdal_addo()` to add or remove overviews from raster images; #1921
1216

R/sample.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ st_sample = function(x, size, ...) UseMethod("st_sample")
2525
#' @param by_polygon logical; for \code{MULTIPOLYGON} geometries, should the effort be split by \code{POLYGON}? See https://github.com/r-spatial/sf/issues/1480
2626
#' the same as specified by \code{size}? \code{TRUE} by default. Only applies to polygons, and
2727
#' when \code{type = "random"}.
28+
#' @param progress logical; if \code{TRUE} show progress bar (only if \code{size} is a vector).
2829
#' @return an \code{sfc} object containing the sampled \code{POINT} geometries
2930
#' @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{http://mathworld.wolfram.com/SpherePointPicking.html}
3031
#'
@@ -93,13 +94,18 @@ st_sample.sf = function(x, size, ...) st_sample(st_geometry(x), size, ...)
9394
#' @export
9495
#' @name st_sample
9596
st_sample.sfc = function(x, size, ..., type = "random", exact = TRUE, warn_if_not_integer = TRUE,
96-
by_polygon = FALSE) {
97+
by_polygon = FALSE, progress = FALSE) {
9798

9899
if (!missing(size) && warn_if_not_integer && any(size %% 1 != 0))
99100
warning("size is not an integer")
100101
if (!missing(size) && length(size) > 1) { # recurse:
101102
size = rep(size, length.out = length(x))
102-
ret = lapply(1:length(x), function(i) st_sample(x[i], size[i], type = type, exact = exact, ...))
103+
ret = if (progress) {
104+
if (!requireNamespace("pbapply", quietly = TRUE))
105+
stop("package pbapply required, please install it first")
106+
pbapply::pblapply(1:length(x), function(i) st_sample(x[i], size[i], type = type, exact = exact, ...))
107+
} else
108+
lapply(1:length(x), function(i) st_sample(x[i], size[i], type = type, exact = exact, ...))
103109
st_set_crs(do.call(c, ret), st_crs(x))
104110
} else {
105111
res = switch(max(st_dimension(x)) + 1,
@@ -249,7 +255,7 @@ hex_grid_points = function(obj, pt, dx) {
249255

250256
y = rep(y, each = length(x))
251257
x = rep(c(x, x + dx / 2), length.out = length(y))
252-
xy = cbind(x, y)[x >= xlim[1] & x <= xlim[2] & y >= ylim[1] & y <= ylim[2], ]
258+
xy = cbind(x, y)[x >= xlim[1] & x <= xlim[2] & y >= ylim[1] & y <= ylim[2], , drop = FALSE]
253259
colnames(xy) = NULL
254260
st_sfc(lapply(seq_len(nrow(xy)), function(i) st_point(xy[i,])), crs = st_crs(bb))
255261
}

man/st_sample.Rd

Lines changed: 4 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)