Skip to contents

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

Usage

extract_proportion(x, points, radius, bind = TRUE, ...)

Arguments

x

A categorical SpatRaster (single layer).

points

An sf or SpatVector of 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 of points.

...

Additional arguments passed to terra::extract().

Value

A data.frame with one row per point and one proportion column per class.

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