Skip to content

Commit

Permalink
st_sample won't break if lwgeom is not present, but warns
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Sep 11, 2023
1 parent 1569aa7 commit 8ea9051
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ importFrom(units,drop_units)
importFrom(units,make_unit_label)
importFrom(units,set_units)
importFrom(utils,compareVersion)
importFrom(utils,globalVariables)
importFrom(utils,head)
importFrom(utils,object.size)
importFrom(utils,packageVersion)
Expand Down
2 changes: 1 addition & 1 deletion R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,5 @@ CPL_get_m_range <- function(sf, depth) {

# Register entry points for exported C++ functions
methods::setLoadAction(function(ns) {
.Call('_sf_RcppExport_registerCCallable', PACKAGE = 'sf')
.Call(`_sf_RcppExport_registerCCallable`)
})
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @importFrom utils head object.size str tail packageVersion compareVersion
#' @importFrom utils head object.size str tail packageVersion compareVersion globalVariables
#' @importFrom stats aggregate dist na.omit rbinom runif setNames
#' @importFrom tools file_ext file_path_sans_ext
#' @importFrom methods as new slot slotNames "slot<-"
Expand Down
28 changes: 16 additions & 12 deletions R/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ st_sample = function(x, size, ...) UseMethod("st_sample")
#' if (sf_extSoftVersion()["proj.4"] >= "4.9.0")
#' plot(p <- st_sample(x, 1000), add = TRUE)
#' if (require(lwgeom, quietly = TRUE)) { # for st_segmentize()
#' x2 = st_transform(st_segmentize(x, 1e4), st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' g = st_transform(st_graticule(), st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' plot(x2, graticule = g)
#' if (sf_extSoftVersion()["proj.4"] >= "4.9.0") {
#' p2 = st_transform(p, st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' plot(p2, add = TRUE)
#' }
#' x2 = st_transform(st_segmentize(x, 1e4), st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' g = st_transform(st_graticule(), st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' plot(x2, graticule = g)
#' if (sf_extSoftVersion()["proj.4"] >= "4.9.0") {
#' p2 = st_transform(p, st_crs("+proj=ortho +lat_0=30 +lon_0=45"))
#' plot(p2, add = TRUE)
#' }
#' }
#' x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,10),c(0,90),c(0,0))))) # NOT long/lat:
#' plot(x)
Expand Down Expand Up @@ -90,9 +90,9 @@ st_sample = function(x, size, ...) UseMethod("st_sample")
#' plot(st_sample(ls, 80))
#' # spatstat example:
#' if (require(spatstat.random)) {
#' x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0)))))
#' # for spatstat.random::rThomas(), set type = "Thomas":
#' pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas")
#' x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0)))))
#' # for spatstat.random::rThomas(), set type = "Thomas":
#' pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas")
#' }
#' @export
#' @name st_sample
Expand Down Expand Up @@ -164,9 +164,13 @@ st_poly_sample = function(x, size, ..., type = "random",
global = FALSE
bb = st_bbox(x)
if (isTRUE(st_is_longlat(x))) {
if (sf_use_s2())
bb = st_bbox(st_segmentize(st_as_sfc(bb),
if (sf_use_s2()) { # if FALSE, the user wants the coord ranges to be the bbox
if (!requireNamespace("lwgeom", quietly = TRUE))
warning("coordinate ranges not computed along great circles; install package lwgeom to get rid of this warning")
else
bb = st_bbox(st_segmentize(st_as_sfc(bb),
units::set_units(1, "degree", mode = "standard"))) # get coordinate range on S2
}
R = s2::s2_earth_radius_meters()
toRad = pi / 180
h1 = sin(bb["ymax"] * toRad)
Expand Down
1 change: 1 addition & 0 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ pivot_longer.sf <- function (data, cols, names_to = "name", names_prefix = NULL,
st_as_sf(out, sf_column_name = sf_column_name)
}

globalVariables(c("name", "value"))
# https://github.com/r-spatial/sf/issues/1915
#' @name tidyverse
#' @export
Expand Down
20 changes: 10 additions & 10 deletions 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 8ea9051

Please sign in to comment.