Summarise mean environmental conditions per quadrat
Source:R/matrices.R
calculate_quadrat_environment.Rd
Converts a gridded environmental table (env_grid
) into an sf
point layer, joins it to quadrat polygons, and computes per‑quadrat mean
values (temperature, elevation, rainfall). 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
,y
giving point coordinates, and environmental columnstemperature_C
,elevation_m
,rainfall_mm
. Coordinates are assumed to be in the CRS specified bydomain_crs
.- quadrats
An
sf
POLYGON (or MULTIPOLYGON) layer with aquadrat_id
column. Must be in the same CRS asdomain_crs
.- domain_crs
A coordinate reference system for
env_grid
points. Can be an integer EPSG code, a PROJ4string/WKT, or ansf
crs
object. This CRS should match that ofquadrats
.
Value
A data.frame
with one row per quadrat and columns:
site
, temperature_C
, elevation_m
, rainfall_mm
.
Means are numeric; sites with no overlapping points will have NA
in
the environmental columns.
Details
The function:
converts
env_grid
tosf
points viast_as_sf
usingcoords = c("x","y")
,performs a spatial join
st_join
of points into quadrats (default predicate:st_intersects
),groups by
quadrat_id
and returns the mean of each 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)
} # }