Derives aligned categorical raster layers from an integer ID raster and its Reference Attribute Table (RAT), with support for categorical columns and binned numeric columns. Attaches coverage statistics describing valid cell proportions and class counts.
Arguments
- x
A single-layer categorical or integer
SpatRaster.- attrs
Optional
data.framecontaining the attribute table. IfNULL(default), the table is extracted fromterra::cats(x)[[1]].- id_col
Column index or column name in
attrscontaining the keys that match raster values inx. Default is1.- vars
Character vector of column names in
attrsto convert into target raster layers. Default is all columns exceptid_col.- breaks
Named list of numeric break vectors for binning continuous attribute columns using
base::cut().- labels
Named list of character vectors of category labels corresponding to
breaks.- right
Logical. Indicating if the intervals should be closed on the right (and open on the left) or vice versa. Default is
TRUE. Passed tobase::cut().
Value
A multi-layer SpatRaster where each layer corresponds to a derived
target variable with categories set via terra::levels(). The returned raster
has an attached attribute "coverage", a tidy data.frame containing:
- target
Target layer name.
- n_total
Total number of cells.
- n_valid
Number of valid non-NA cells.
- n_na
Number of missing (NA) cells.
- prop_valid
Proportion of valid cells (
n_valid / n_total).- n_classes
Number of factor levels / distinct categories.
Examples
library(terra)
r <- rast(matrix(c(1, 2, 1, 3), nrow = 2, ncol = 2))
rat <- data.frame(id = 1:3, landuse = c("Forest", "Water", "Forest"), slope = c(12, 1, 25))
targets <- geomorphon_targets(r, attrs = rat, id_col = "id", vars = "landuse")
attr(targets, "coverage")
#> target n_total n_valid n_na prop_valid n_classes
#> 1 landuse 4 4 0 1 2