Extract a summary statistic within one or more buffer radii
Source:R/extract_buffer.R
extract_buffer.RdFor 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.
Arguments
- x
A
SpatRasterwith one or more layers.- points
An
sforSpatVectorof point locations.- radii
Numeric vector of buffer radii in CRS units.
- fun
Summary function; passed to
terra::extract()via thefunargument. Defaultmean.- fun_name
Character label appended to column names. If
NULL(default), the name offunis used whenfunis a named function, or"stat"otherwise.- bind
Logical; if
TRUE(default), columns are bound to the attributes ofpoints.- ...
Additional arguments passed to
terra::extract(), and on tofun—na.rm = TRUEis the usual one, needed whenever a buffer can overlapNAcells.
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))
} # }