Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds runifpoint #1204

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@
#' @export
st_sample = function(x, size, ..., type = "random", exact = TRUE) {
x = st_geometry(x)
if (any(size %% 1 != 0))
if (!missing(size) && any(size %% 1 != 0))
stop("size should be an integer")
if (length(size) > 1) { # recurse:
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, ...))
res = st_set_crs(do.call(c, ret), st_crs(x))
} else {
res = switch(max(st_dimension(x)) + 1,
st_multipoints_sample(do.call(c, x), size, ..., type = type),
st_ll_sample(st_cast(x, "LINESTRING"), size, ..., type = type),
st_poly_sample(x, size, ..., type = type))
if (exact & type == "random" & all(st_geometry_type(res) == "POINT")) {
st_poly_sample(x, ..., size, type = type))
if (!missing(size) && exact && type == "random" && all(st_geometry_type(res) == "POINT")) {
diff = size - length(res)
if(diff > 0) { # too few points
res_additional = st_sample_exact(x = x, size = diff, ..., type = type)
Expand All @@ -95,14 +95,14 @@ st_sample = function(x, size, ..., type = "random", exact = TRUE) {
res
}

st_poly_sample = function(x, size, ..., type = "random",
st_poly_sample = function(x, ..., size, type = "random",
offset = st_sample(st_as_sfc(st_bbox(x)), 1)[[1]]) {

a0 = as.numeric(st_area(st_make_grid(x, n = c(1,1))))
a1 = as.numeric(sum(st_area(x)))
# st_polygon(list(rbind(c(-180,-90),c(180,-90),c(180,90),c(-180,90),c(-180,-90))))
# for instance has 0 st_area
if (is.finite(a0) && is.finite(a1) && a0 > a0 * 0.0 && a1 > a1 * 0.0)
if (is.finite(a0) && is.finite(a1) && a0 > a0 * 0.0 && a1 > a1 * 0.0 && !missing(size))
size = round(size * a0 / a1)
bb = st_bbox(x)

Expand Down Expand Up @@ -130,8 +130,28 @@ st_poly_sample = function(x, size, ..., type = "random",
runif(size, bb[2], bb[4])
m = cbind(lon, lat)
st_sfc(lapply(seq_len(nrow(m)), function(i) st_point(m[i,])), crs = st_crs(x))
} else
stop(paste("sampling type", type, "not implemented for polygons"))
} else {
if (!requireNamespace("spatstat", quietly = TRUE)){
stop("package spatstat required, please install it first")
}
spatstat_fun = try(get(paste0("r", type), asNamespace("spatstat")), silent = TRUE)
if(inherits(spatstat_fun, "try-error")){
# stop(paste0("r", type), " is not an exported function from spatstat.")
stop(paste("sampling type", type, "not implemented for polygons"))
}
if (!requireNamespace("maptools", quietly = TRUE)){
stop("package maptools required, please install it first")
}
rslt = try(spatstat_fun(..., win = maptools::as.owin.SpatialPolygons(as(x, "Spatial"))), silent = TRUE)
if(inherits(rslt, "try-error")){
stop("The spatstat function ", paste0("r", type),
" did not return a valid result. Consult the help file.\n",
"Error message from spatstat:\n", rslt)
}
rslt = st_as_sf(rslt)
rslt = st_collection_extract(rslt, "POINT")
st_geometry(rslt)
}
pts[lengths(st_intersects(pts, x)) > 0]
}

Expand Down