Extract class proportions within a buffer for categorical layers
Source:R/extract_proportion.R
extract_proportion.RdFor each point location, draws a circular buffer of radius
(in CRS units) and computes the proportion of each class in a
categorical SpatRaster. All points share the same set of
class columns; classes absent within a buffer are filled with
zero. Proportions are of the non-NA cells in the buffer, so
they sum to one even where part of the buffer is masked, and the
columns are the classes observed across all buffers — a class
present in the layer but in none of them gets no column.
Arguments
- x
A categorical
SpatRaster(single layer).- points
An
sforSpatVectorof point locations.- radius
Numeric; buffer radius in the CRS units of
x.- bind
Logical; if
TRUE(default), the proportions are column-bound to the attributes ofpoints.- ...
Additional arguments passed to
terra::extract().
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[] <- sample(1:3, ncell(r), replace = TRUE)
pts <- st_as_sf(
data.frame(id = 1:2, x = c(2500, 7500), y = c(5000, 5000)),
coords = c("x", "y"), crs = 3400
)
extract_proportion(r, pts, radius = 500)
} # }