Skip to content

Commit

Permalink
st_sample with type="hexagon" and n=1; fixes #1945
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jun 14, 2022
1 parent 1b77b9c commit 6d3e931
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Suggests:
Matrix,
microbenchmark,
odbc,
pbapply,
pillar,
pool,
raster,
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# version 1.0-8

* fix `st_sample()` with `type = "hexagonal"` for corner case (n=1), add a `progress` argument for a progress bar; #1945

* add package `pbapply` to Suggests; #1945

* add pdf driver to windows build; #1942

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

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

* `pivot_wider()` method added, by Henning Teickner; #1915
* add `pivot_wider()` method, by Henning Teickner; #1915

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

Expand Down
12 changes: 9 additions & 3 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ st_sample = function(x, size, ...) UseMethod("st_sample")
#' @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
#' the same as specified by \code{size}? \code{TRUE} by default. Only applies to polygons, and
#' when \code{type = "random"}.
#' @param progress logical; if \code{TRUE} show progress bar (only if \code{size} is a vector).
#' @return an \code{sfc} object containing the sampled \code{POINT} geometries
#' @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}
#'
Expand Down Expand Up @@ -93,13 +94,18 @@ st_sample.sf = function(x, size, ...) st_sample(st_geometry(x), size, ...)
#' @export
#' @name st_sample
st_sample.sfc = function(x, size, ..., type = "random", exact = TRUE, warn_if_not_integer = TRUE,
by_polygon = FALSE) {
by_polygon = FALSE, progress = FALSE) {

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

y = rep(y, each = length(x))
x = rep(c(x, x + dx / 2), length.out = length(y))
xy = cbind(x, y)[x >= xlim[1] & x <= xlim[2] & y >= ylim[1] & y <= ylim[2], ]
xy = cbind(x, y)[x >= xlim[1] & x <= xlim[2] & y >= ylim[1] & y <= ylim[2], , drop = FALSE]
colnames(xy) = NULL
st_sfc(lapply(seq_len(nrow(xy)), function(i) st_point(xy[i,])), crs = st_crs(bb))
}
Expand Down
5 changes: 4 additions & 1 deletion man/st_sample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d3e931

Please sign in to comment.