partitionSites: Random Stratified Site Selection

View source: R/partitionSites.R

partitionSitesR Documentation

Random Stratified Site Selection

Description

Randomly assign locations across a waterbody using Halton Iterative Partitioning (HIP; Robertson et al. 2018, via the spbal package), stratified by depth. Depth bins are defined from an interpolated DEM (e.g. the output of interpBathy()), and a customizable number of spatially determined locations is drawn within each bin - so, for example, you can request more locations in a shallow littoral bin and fewer in a deep pelagic bin.

Usage

partitionSites(
  dem,
  depth_bins,
  n_per_bin,
  water_level_drop = 0,
  min_spacing = NULL,
  iterations = 7,
  plot = TRUE,
  seed = NULL
)

Arguments

dem

a SpatRaster of interpolated bathymetry (e.g. from interpBathy()), used both to define depth bins and as the pool of candidate locations. Assumed to represent the waterbody at full pool/elevation unless 'water_level_drop' is used.

depth_bins

numeric vector of depth bin edges, e.g. c(0, 5, 15, 30, Inf) for three bins: 0-5 m, 5-15 m, and 15-30+ m. Must have at least 2 values (i.e. at least 1 bin). Bins are half-open, [lower, upper).

n_per_bin

numeric vector giving how many locations to draw from each bin, in the same order as 'depth_bins' implies (length must be length(depth_bins) - 1). A single value is also accepted and recycled across all bins (e.g. n_per_bin = 5 draws 5 locations from every bin).

water_level_drop

optional single non-negative numeric value giving how far the water level has dropped, in the same depth units as 'dem', relative to the full-pool elevation 'dem' is assumed to represent. If greater than 0, the DEM is rebuilt before sampling: every cell's depth is reduced by this amount, and any cell whose adjusted depth is at or below zero (i.e., now exposed/dry) is excluded from sampling. Default = 0 (no water level adjustment; 'dem' is used as-is).

min_spacing

optional numeric value giving the minimum allowed distance, in meters, between selected locations within a bin (passed to spbal::HIP()'s minRadius argument). Default = NULL (no minimum spacing enforced).

iterations

numeric value giving the number of Halton partition levels used by spbal::HIP() (see spbal::HIP() for details), default = 7. If HIP fails for a given bin (e.g. too few candidate cells), the function automatically retries with fewer partition levels before giving up on that bin with a warning.

plot

logical: should a map of the resulting locations, colored by depth bin and shown over the (adjusted) DEM, be drawn? Default = TRUE.

seed

optional numeric value used to seed the random number generator, for reproducible site selection across runs. Default = NULL (not seeded).

Details

This function requires the 'spbal' and 'sf' packages to be installed (not hard dependencies of this package, since they are only needed for this function). The function automatically detects whether 'dem' is in a geographic (decimal degree) or projected (meters) coordinate system; if geographic, sampling and 'min_spacing' are carried out in the waterbody's best-fit UTM zone (determined from 'dem's own extent) so that distances are measured in meters, and returned locations are projected back to the original CRS of 'dem'. If a depth bin contains fewer candidate DEM cells than the number of locations requested for it, all available cells are used and a warning is issued. No outline shapefile is required - a boundary for the plot (if requested) is derived automatically from the non-NA footprint of 'dem' (or the water-level-adjusted DEM, if 'water_level_drop' > 0).

Value

a list with:

locations

a data frame of selected locations, with columns 'x', 'y' (in the original CRS of 'dem'), 'depth_bin' (the bin label), and 'bin_index' (the bin's position in 'depth_bins'). Depth bins/labels reflect the water-level-adjusted DEM if 'water_level_drop' > 0.

map

a recorded base R plot (see grDevices::recordPlot()) of the (adjusted) DEM and selected locations by depth bin, or NULL if plot = FALSE.

Author(s)

Tristan Blechinger, Department of Zoology & Physiology, University of Wyoming

Examples


outline <- terra::vect(system.file("extdata", "example_outline.shp", package = 'rLakeHabitat'))
data <- read.csv(system.file("extdata", "example_depths.csv", package = 'rLakeHabitat'))
dem <- interpBathy(outline, data, "x", "y", "z", zeros = FALSE,
separation = 10, res = 10, nmax = 8, method = "IDW")
partitionSites(dem, depth_bins = c(0, 5, 15, 30, Inf),
n_per_bin = c(8, 6, 4, 2), seed = 123)

# with a 2 m water level drop from full pool
partitionSites(dem, depth_bins = c(0, 5, 15, 30, Inf),
n_per_bin = c(8, 6, 4, 2), water_level_drop = 2, seed = 123)

rLakeHabitat documentation built on July 30, 2026, 5:11 p.m.