Skip to contents

For each combination of point, raster layer, and buffer radius, computes a summary statistic over the cell values within the circular buffer. Vectorised over radii so all results are returned in a single data.frame. Output columns are named <layer>_r<radius>_<fun_name> (where radius is in CRS units). Cells are included when their centre falls inside the buffer; a buffer smaller than one cell falls back to the cells it touches, so every point always gets a value.

Usage

extract_buffer(x, points, radii, fun = mean, fun_name = NULL, bind = TRUE, ...)

Arguments

x

A SpatRaster with one or more layers.

points

An sf or SpatVector of point locations.

radii

Numeric vector of buffer radii in CRS units.

fun

Summary function; passed to terra::extract() via the fun argument. Default mean.

fun_name

Character label appended to column names. If NULL (default), the name of fun is used when fun is a named function, or "stat" otherwise.

bind

Logical; if TRUE (default), columns are bound to the attributes of points.

...

Additional arguments passed to terra::extract(), and on to funna.rm = TRUE is the usual one, needed whenever a buffer can overlap NA cells.

Value

A data.frame with one row per point. Extracted columns are named <layer>_r<radius>_<fun_name>.

Examples

if (FALSE) { # \dontrun{
library(terra)
library(sf)
r   <- rast(nrows = 100, ncols = 100, crs = "EPSG:3400",
            xmin = 0, xmax = 10000,
            ymin = 0, ymax = 10000)
r[] <- runif(ncell(r))
pts <- st_as_sf(
  data.frame(id = 1:2, x = c(2500, 7500), y = c(5000, 5000)),
  coords = c("x", "y"), crs = 3400
)
extract_buffer(r, pts, radii = c(250, 500, 1000))
} # }