Skip to contents

Computes fine-cell weights, covariate averages, coordinates, and metadata for each areal prediction polygon.

Usage

cos_make_areal_blocks(U_sf, X_rast, id_col = NULL,
  missing = c("error", "drop", "renormalize"), row_sum_tol = 1e-5,
  verbose = TRUE)

Arguments

U_sf

An sf object containing areal prediction polygons in the same projected CRS as X_rast.

X_rast

A raster object with prepared fine-support covariates.

id_col

Optional column in U_sf used as prediction-unit label.

missing

How to handle incomplete raster covariate cells that overlap prediction supports. "error" stops if retained support weights fall below one. "drop" keeps the covered-cell weights without rescaling. "renormalize" rescales retained weights to sum to one within each prediction support.

row_sum_tol

Tolerance used when warning about support-weight row sums.

verbose

Logical; if TRUE, print exact extraction progress.

Details

By default, incomplete raster covariate cells are treated as an error when they would change the support average. Use missing = "drop" or missing = "renormalize" only when the prediction target should be based on covered cells rather than the full prediction polygon.

Value

A list with class "cos_areal_blocks" containing areal support blocks and timing, a named numeric vector with user, system, and elapsed seconds.

Examples

if (FALSE) { # \dontrun{
library(sf)
library(raster)
library(ggplot2)

chm <- raster(system.file("extdata", "example_chm.tif", package = "cosTapered"))
names(chm) <- "chm"
U_sf <- st_read(system.file("extdata", "example_U.gpkg",
                            package = "cosTapered"), quiet = TRUE)
forest_sf <- st_read(system.file("extdata", "example_forest.gpkg",
                                 package = "cosTapered"), quiet = TRUE)

intercept <- chm
intercept[] <- ifelse(is.na(getValues(chm)), NA_real_, 1)
names(intercept) <- "intercept"
X_rast <- stack(intercept, chm)
names(X_rast) <- c("intercept", "chm")

U_blocks <- cos_make_areal_blocks(U_sf, X_rast, id_col = "U_id",
                                  verbose = FALSE)
summary(U_blocks)

chm_df <- as.data.frame(chm, xy = TRUE, na.rm = TRUE)
names(chm_df) <- c("x", "y", "chm")

ggplot() +
  geom_tile(data = chm_df, aes(x = x, y = y, fill = chm),
            width = xres(chm), height = yres(chm)) +
  geom_sf(data = U_sf, fill = NA, color = "white", linewidth = 0.35) +
  geom_sf(data = forest_sf, fill = NA, color = "black", linewidth = 0.5) +
  scale_fill_viridis_c(option = "cividis") +
  coord_sf(expand = FALSE) +
  labs(title = "Areal prediction supports over fine-support covariate",
       fill = "CHM", x = NULL, y = NULL) +
  theme_bw()
} # }