Skip to contents

Clips a raster using a polygon, preserving the coordinate reference system (CRS) of the raster.

Usage

clip_raster(raster, polygon, keep_crs = TRUE)

Arguments

raster

A `terra` raster to be clipped.

polygon

A `sf` polygon layer used for clipping.

keep_crs

Logical. If `TRUE`, retains the original CRS of the raster. If `FALSE`, transforms the raster to the polygon CRS. Default is `TRUE`.

Value

A `terra` raster clipped to the extent of the polygon.

See also

Other clip functions: clip_layer(), clip_multipoligon(), generate_bbox()

Examples

source_gpkg <- system.file("extdata", "sigugr.gpkg", package = "sigugr")
p <-sf::st_read(source_gpkg, layer = 'lanjaron', quiet = TRUE)

source_tif <- system.file("extdata", "sat.tif", package = "sigugr")
r <- terra::rast(source_tif)

result <- clip_raster(r, p)