if (FALSE) {
polygons <- sf::st_read(system.file('extdata/Benin_communes.gpkg', package = 'mbg'))
id_raster <- build_id_raster(polygons)
n_data_pixels <- sum(!is.na(terra::values(id_raster)))
# Example ID raster filled with data
# This is an example of pixel-level covariate data or model estimates
data_raster <- mbg::values_to_raster(stats::rnorm(n_data_pixels), id_raster)
# Build aggregation table, which can be used across many aggregations
aggregation_table <- build_aggregation_table(
polygons, id_raster, polygon_id_field = 'commune_code'
)
# Aggregate the raster to the polygons
aggregated <- aggregate_raster_to_polygons(
data_raster = data_raster,
aggregation_table = aggregation_table,
aggregation_cols = 'commune_code',
method = 'mean'
)
head(aggregated)
}
Run the code above in your browser using DataLab