# \donttest{
library(terra)
# 1) Primary raster (integer categories)
land_cover <- rast(ncol = 30, nrow = 30,
xmin = -50, xmax = -49,
ymin = -15, ymax = -14)
values(land_cover) <- sample(1:3, ncell(land_cover), replace = TRUE)
crs(land_cover) <- "+proj=longlat +datum=WGS84 +no_defs"
# Basic: total area by category
area.calc.flex(land_cover, unit = "km")
# 2) Zonal polygons (two regions)
region1 <- vect("POLYGON ((-50 -15, -49.5 -15, -49.5 -14, -50 -14, -50 -15))")
region2 <- vect("POLYGON ((-49.5 -15, -49 -15, -49 -14, -49.5 -14, -49.5 -15))")
regions <- rbind(region1, region2)
crs(regions) <- crs(land_cover)
regions$region_id <- c("A", "B")
area.calc.flex(
land_cover,
zonal_polys = regions,
id_col = "region_id",
unit = "km"
)
# 3) Overlay raster (binary mask)
protected <- rast(land_cover)
values(protected) <- sample(0:1, ncell(protected), replace = TRUE)
area.calc.flex(
land_cover,
r2_raster = protected,
unit = "km"
)
# }
Run the code above in your browser using DataLab