Skip to content

Conversation

@JosiahParry
Copy link
Contributor

This PR adds st_sample.bbox() to provide an S3 method to st_sample() for bbox class object.

Very often I find myself wanting sample random points over the extent of an sfc object. This requires me to write code such as

st_bbox(nc) |> 
  st_as_sfc() |> 
  st_sample(42)

It would be quite nice to simplify this to st_sample(st_bbox(nc), 42). This PR makes it possible.

Again, sorry for not updating man/st_sample.Rd. I used to be able to compile sf. I'll figure it out again at some point.

@edzer
Copy link
Member

edzer commented Dec 8, 2023

I can see that this simplifies life in R2 cases, my reservation is the user expectation when doing this on the sphere:

library(sf)
# Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
set.seed(13331)
st_bbox(c(xmin = 0, xmax = 45, ymin = 0, ymax = 45), crs = st_crs('OGC:CRS84')) |>
  st_as_sfc() |>
  st_sample(42) |>
  st_bbox()
#       xmin       ymin       xmax       ymax 
#  1.3055001  0.4876549 44.2484992 46.2575499 

and looking at ymax. This is expected, but can be better understood, I believe, when we force the user to leave the st_as_sfc() in the pipeline, and when wanting to do this on a box formed by parallels and meridians, to force them to add a st_segmentize() in it while s2 is switched off (sampling with s2 switched off does not give a uniform sampling over the sphere). Maybe we need examples of this in documentation / vignettes / books?

@edzer
Copy link
Member

edzer commented Dec 8, 2023

Or we add a useful stop("") message in case coordinates are geographic, pointing the user to the problem?

The deeper problem is obviously the meaning / interpretation of a bounding box on the sphere.

@JosiahParry
Copy link
Contributor Author

As always, I greatly appreciate your great consideration to detail! Very great points which I clearly failed to consider by mindlessly stealing the example from st_bbox() (perhaps that ought to be modified too!).

I will concede, though, that most users of sf will not be too aware of when s2 is on or off (and frankly many users aren't even aware of what s2 is or that it's used!!) which includes myself.

I think a section in the book on this very topic could be very helpful and can be linked to in the sf docs for st_bbox().

For the practical purposes, I would be content with adding stop("sampling from a bounding box using spherical geometries is not supported"). But I think a more user friendly alternative would be to:

  1. segmentize the bounding box when using s2
  2. provide a message() (not a warning because it's not necessarily a negative consequence of inaction) describing the action taken on the user's behalf
  3. sample from the now curved shape

@edzer
Copy link
Member

edzer commented Dec 8, 2023

I agree, but would maybe go one step further, and add an option, only working for geographic coordinates, great_circles = FALSE which would sample the "graticule" bounding box, and if TRUE would sample the S2 / great circle polygon formed by the four corners.

@edzer edzer merged commit b070c93 into r-spatial:main Dec 10, 2023
@edzer
Copy link
Member

edzer commented Dec 10, 2023

I'll take it from here.

@JosiahParry
Copy link
Contributor Author

Ah, thank you! Can't wait to see how you implement it.

edzer added a commit that referenced this pull request Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants