Skip to contents

Draws k seed points uniformly at random inside boundary, ignoring where the observations are. Reach for this when the cells should cover the study area evenly (so that sparsely sampled ground still gets its own cells and is visibly under-sampled in the results) instead of concentrating resolution where the data already are, which is what voronoi_seeds_kmeans() does. It is also the honest choice for a null or sensitivity comparison: re-running an analysis over several random seedings shows how much of a result depends on one particular tessellation.

Usage

voronoi_seeds_random(boundary, k, set_seed = NULL)

Arguments

boundary

An sf or sfc polygonal object.

k

Integer; number of random seeds.

set_seed

Optional integer RNG seed. Default NULL: the seeds are drawn from the session's random-number stream, so consecutive calls give different seedings and set.seed() before a call makes it reproducible. Pass a number to get the same seeds whatever that stream holds; the caller's stream is then left as it was. The default used to be 456, which made every call return the same "random" seeding, even inside a loop over set.seed().

Value

An sf object of k random POINTs (fewer only in the degenerate case above), with seed_id and method = "random" columns matching get_voronoi_seeds().

Details

Seeds are drawn uniformly inside the polygon. When a draw falls short of k it is topped up with further uniform draws from the same polygon, so the result has exactly k seeds; only a geometry that still yields too few after ten top-ups returns fewer, and that shortfall is logged.

Examples

library(sf)
bnd <- st_sf(geometry = st_sfc(st_polygon(list(rbind(
  c(0, 0), c(100, 0), c(100, 100), c(0, 100), c(0, 0)
))), crs = 32632))
set.seed(1)
seeds <- voronoi_seeds_random(bnd, k = 10)
nrow(seeds)   # 10
#> [1] 10
seeds
#> Simple feature collection with 10 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 6.178627 ymin: 17.65568 xmax: 94.46753 ymax: 99.19061
#> Projected CRS: WGS 84 / UTM zone 32N
#>                     geometry seed_id method
#> 1  POINT (26.55087 20.59746)       1 random
#> 2  POINT (37.21239 17.65568)       2 random
#> 3  POINT (57.28534 68.70228)       3 random
#> 4  POINT (90.82078 38.41037)       4 random
#> 5  POINT (20.16819 76.98414)       5 random
#> 6  POINT (89.83897 49.76992)       6 random
#> 7  POINT (94.46753 71.76185)       7 random
#> 8  POINT (66.07978 99.19061)       8 random
#> 9   POINT (62.9114 38.00352)       9 random
#> 10 POINT (6.178627 77.74452)      10 random
# Another call is another seeding; set_seed pins one.
identical(st_coordinates(voronoi_seeds_random(bnd, k = 10, set_seed = 7)),
          st_coordinates(voronoi_seeds_random(bnd, k = 10, set_seed = 7)))
#> [1] TRUE