Summarise mean environmental conditions per quadrat
Source:R/matrices.R
calculate_quadrat_environment.RdConverts a gridded environmental table (env_grid) into an sf
point layer, joins it to quadrat polygons, and computes per‑quadrat mean
values for all numeric environmental columns. Quadrats with no overlapping
grid points are retained and returned with NA means.
Arguments
- env_grid
A regular (or irregular) data frame with numeric columns
x,ygiving point coordinates, plus one or more numeric environmental columns. Coordinates are assumed to be in the CRS specified bydomain_crs.- quadrats
An
sfPOLYGON (or MULTIPOLYGON) layer with aquadrat_idcolumn. Must be in the same CRS asdomain_crs.- domain_crs
A coordinate reference system for
env_gridpoints. Can be an integer EPSG code, a PROJ4string/WKT, or ansfcrsobject. This CRS should match that ofquadrats.
Value
A data.frame with one row per quadrat and columns:
site plus one column per summarised numeric environmental variable.
Means are numeric; sites with no overlapping points will have NA.
Details
The function:
converts
env_gridtosfpoints viast_as_sfusingcoords = c("x","y"),performs a spatial join
st_joinof points into quadrats (default predicate:st_intersects),groups by
quadrat_idand returns the mean of each numeric environmental variable (withna.rm = TRUE),left‑joins back to the full set of quadrats to keep empty sites.
If you prefer a different join logic (e.g., nearest neighbour), adapt the
join call to specify a different predicate or use st_nearest_feature.
CRS
env_grid is interpreted in domain_crs; quadrats must
already be in the same CRS. Reproject beforehand as needed.
Examples
if (FALSE) { # \dontrun{
env <- create_environmental_gradients(domain, resolution = 50, noise_level = 0.05)
E <- calculate_quadrat_environment(env, quadrats, domain_crs = sf::st_crs(domain))
head(E)
} # }