Calculate Area Proportions for Categorical Raster Classes (Generic)
Source:R/extract_cat_raster.R
extract_cat_raster.RdExtracts and calculates the **area proportion** of each categorical class (e.g., LULC) found within each input polygon. This function uses area-weighting to ensure highly accurate, sub-pixel zonal statistics.
Arguments
- spat_raster_cat
A single-layer
SpatRasterobject containing categorical values.- sf_hex_grid
An
sfobject containing polygonal geometries. The function will useh3_addressif present, otherwise it creates and uses a temporaryIDcolumn for joining.- proportion
Logical. If
TRUE(default), the output values are the proportion of the polygon area covered by each category (summing to 1 for the covered area). IfFALSE, the output is the raw sum of the coverage fraction (area).
Value
An sf object identical to sf_hex_grid, but with new columns
appended for each categorical value found in the raster. Column names follow the
pattern <layer_name>_prop_<category_value>. Columns are **numerically ordered**
by the category value.
Details
This function replaces the simplistic, non-area-weighted table() counting
method with a robust custom function utilizing dplyr and the coverage_fraction
column from exactextractr. Key features include:
**Area-Weighted Accuracy:** Uses
coverage_fractionfor precise results.**NA Filtering:** Excludes
NAraster values to prevent aprop_NaNcolumn.**Numerical Ordering:** Sorts the final output columns by category number (e.g., 70 before 80).