Skip to contents

Aligns a SpatRaster or stars raster to a raster template, or clips it to a vector template. Vector geometries are rasterized on the target grid and used as a mask.

Usage

spl_clipMask(x, template, method = c("bilinear", "near"), touches = FALSE)

Arguments

x

A SpatRaster or stars object to clip and mask.

template

A SpatRaster or stars raster template, an sf or sfc object, or a path to a raster or vector file.

method

Resampling method: "bilinear" for continuous data or "near" for categorical data (Default: "bilinear").

touches

If TRUE, rasterize all cells touched by vector geometries; otherwise rasterize cells whose centers fall within them (Default: FALSE).

Value

An object of the same class as x, with its layer count and names preserved. Raster templates set the output grid and, when they contain values, mask cells where the first layer is NA. Vector templates crop the output to their extent and mask it to the rasterized geometry.

Author

Martin Jung

Examples

target <- terra::rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10,
                      ymin = 0, ymax = 10, crs = "EPSG:4326", vals = 1:100)
boundary <- sf::st_sf(
  id = 1,
  geometry = sf::st_sfc(sf::st_polygon(list(rbind(
    c(2, 2), c(8, 2), c(8, 8), c(2, 8), c(2, 2)
  ))), crs = 4326)
)
clipped <- spl_clipMask(target, boundary)
#> Clipping and masking raster to template.