Sample points on or in (sets of) spatial features. Returns a pre-specified number of points that is equal to size if type = "random" or an approximation of size for other sampling types or if exact = FALSE.

st_sample(x, size, ..., type = "random", exact = TRUE)

Arguments

x

object of class sf or sfc

size

sample size(s) requested; either total size, or a numeric vector with sample sizes for each feature geometry. When sampling polygons, the returned sampling size may differ from the requested size, as the bounding box is sampled, and sampled points intersecting the polygon are returned.

...

ignored, or passed on to sample for multipoint sampling

type

character; indicates the spatial sampling type; only random is implemented right now

exact

logical; should the length of output be exactly the same as specified by size? TRUE by default. Only applies if type = "random".

Value

an sfc object containing the sampled POINT geometries

Details

if x has dimension 2 (polygons) and geographical coordinates (long/lat), uniform random sampling on the sphere is applied, see e.g. http://mathworld.wolfram.com/SpherePointPicking.html

For regular or hexagonal sampling of polygons, the resulting size is only an approximation.

As parameter called offset can be passed to control ("fix") regular or hexagonal sampling: for polygons a length 2 numeric vector (by default: a random point from st_bbox(x)); for lines use a number like runif(1).

Examples

x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,90),c(0,90),c(0,0)))), crs = st_crs(4326)) plot(x, axes = TRUE, graticule = TRUE)
if (sf_extSoftVersion()["proj.4"] >= "4.9.0") plot(p <- st_sample(x, 1000), 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)
p_exact = st_sample(x, 1000) p_not_exact = st_sample(x, 1000, exact = FALSE) length(p_exact); length(p_not_exact)
#> [1] 1000
#> [1] 976
plot(st_sample(x, 1000), add = TRUE)
x = st_sfc(st_polygon(list(rbind(c(-180,-90),c(180,-90),c(180,90),c(-180,90),c(-180,-90)))), crs=st_crs(4326)) if (sf_extSoftVersion()["proj.4"] >= "4.9.0") { p = st_sample(x, 1000) st_sample(p, 3) } # hexagonal: sfc = st_sfc(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,0))))) plot(sfc)
h = st_sample(sfc, 100, type = "hexagonal") h1 = st_sample(sfc, 100, type = "hexagonal") plot(h, add = TRUE)
plot(h1, col = 'red', add = TRUE)
c(length(h), length(h1)) # approximate!
#> [1] 97 104
pt = st_multipoint(matrix(1:20,,2)) ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))), st_linestring(rbind(c(0,0),c(.1,0))), st_linestring(rbind(c(0,1),c(.1,1))), st_linestring(rbind(c(2,2),c(2,2.00001)))) st_sample(ls, 80)
#> Warning: the condition has length > 1 and only the first element will be used
#> Geometry set for 4 features (with 1 geometry empty) #> geometry type: MULTIPOINT #> dimension: XY #> bbox: xmin: 0 ymin: 0 xmax: 0.09592193 ymax: 1 #> epsg (SRID): NA #> proj4string: NA
#> MULTIPOINT (0 0.8016789, 0 0.1279624, 0 0.81180...
#> MULTIPOINT (0.09384245 0, 0.05768073 0, 0.01859...
#> MULTIPOINT (0.0524577 1, 0.06497263 1, 0.072786...
#> MULTIPOINT EMPTY