6.1 Raster cropping/masking

One (very) common problem: the extent of input raster is larger than the area of interest.

Solution: Cropping and masking!

# a raster:  SpatRaster
srtm = terra::rast(system.file("raster/srtm.tif", package = "spDataLarge"))
zion = sf::read_sf(system.file("vector/zion.gpkg", package = "spDataLarge"))
# same CRS for both of this object
zion = st_transform(zion, crs(srtm))
# always do a quick viz
plot(srtm, col = grDevices::terrain.colors(50))
plot(zion$geom, add = TRUE)