Draws a simple random sample of the rows whose coordinates fall inside a
region. It decides where sampling happens, not how evenly it covers the
ground: for a sample spread evenly across space — usually far more precise
when what you measure varies smoothly — use design_spread(), on the frame
restricted to the region if need be.
Arguments
- coords
Two column names,
c(x, y)— longitude then latitude.- region
An
sforsfcgeometry, or a list of them, which is unioned. Reprojected tocrswhen it differs.- n
Number of rows to draw from inside the region.
- crs
Coordinate reference system of the coordinate columns. Defaults to EPSG:4326.
- na_rm
Drop rows with missing coordinates instead of raising an error.
Value
A design object, for use with draw().
Coordinate order
coords is c(x, y) — longitude first, then latitude — matching
sf::st_as_sf(). Getting this backwards puts your points somewhere else
entirely, so it is worth checking against a known landmark once.
Regions that cross the antimeridian
With spherical geometry enabled (sf::sf_use_s2(), the default), consecutive
polygon vertices are joined by the shortest great-circle path. An edge from
longitude -179 to +179 therefore spans the 2 degrees across the antimeridian,
not the 358 degrees the coordinates suggest. A "whole world" rectangle
collapses into a 2-degree-wide pole-to-pole strip of 2.8 million km2 –
against the roughly 510 million km2 of the globe – and contains almost
nothing.
Ring orientation is not the cause and reversing it does not help: sf
normalises winding, so both directions give the same strip. Adding
intermediate vertices does not reliably help either, because the intermediate
edges still bow along geodesics.
draw() warns whenever any edge of region spans more than 180 degrees of
longitude. If you hit it, either split the region at the antimeridian into two
polygons, or switch to planar interpretation with sf::sf_use_s2(FALSE).
Examples
box <- sf::st_sfc(
sf::st_polygon(list(cbind(c(0, 10, 10, 0, 0), c(0, 0, 10, 10, 0)))),
crs = 4326
)
df <- data.frame(id = 1:20,
lon = seq(1, 9, length.out = 20),
lat = seq(1, 9, length.out = 20))
draw(df, design_spatial(c("lon", "lat"), region = box, n = 5), seed = 1)
#> id lon lat
#> 1 1 1.000000 1.000000
#> 2 2 1.421053 1.421053
#> 3 4 2.263158 2.263158
#> 4 7 3.526316 3.526316
#> 5 13 6.052632 6.052632