Skip to contents

Converts 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.

Usage

calculate_quadrat_environment(env_grid, quadrats, domain_crs)

Arguments

env_grid

A regular (or irregular) data frame with numeric columns x, y giving point coordinates, plus one or more numeric environmental columns. Coordinates are assumed to be in the CRS specified by domain_crs.

quadrats

An sf POLYGON (or MULTIPOLYGON) layer with a quadrat_id column. Must be in the same CRS as domain_crs.

domain_crs

A coordinate reference system for env_grid points. Can be an integer EPSG code, a PROJ4string/WKT, or an sf crs object. This CRS should match that of quadrats.

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:

  1. converts env_grid to sf points via st_as_sf using coords = c("x","y"),

  2. performs a spatial join st_join of points into quadrats (default predicate: st_intersects),

  3. groups by quadrat_id and returns the mean of each numeric environmental variable (with na.rm = TRUE),

  4. 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)
} # }