This function allows cropping of a raster mosaic interactively or programmatically:
Interactive Mode: If neither
shapefile
normosaic2
is provided, an interactive map is shown viamosaic_view()
, allowing users to draw a rectangle to define the cropping area.Shapefile Mode: If a
SpatVector
is provided inshapefile
, cropping or masking is performed based on its extent or exact shape, optionally with a buffer.Raster Mode: If
mosaic2
is provided,mosaic
will be cropped to match the extent ofmosaic2
.
For disk-based mosaics, cropping with shapefiles uses GDAL (sf::gdal_utils()
) to improve efficiency.
Arguments
- mosaic
A
SpatRaster
object to be cropped.- r, g, b, re, nir
Integer indices representing the red, green, blue, red-edge, and near-infrared bands of the input mosaic. Default assumes BGR format (b = 1, g = 2, r = 3).
- shapefile
An optional
SpatVector
(orsf
object) to use as cropping/masking geometry. Can be created interactively withshapefile_input()
.- in_memory
Logical. If
TRUE
, raster processing will occur entirely in memory usingterra
. IfFALSE
(default), disk-based processing with GDAL will be used when appropriate.- mosaic2
A second
SpatRaster
whose extent will be used to cropmosaic
.- buffer
A numeric value indicating a buffer (in CRS units) to apply around the shapefile geometry.
- show
A character value indicating what to display in the interactive viewer. Either
"rgb"
or"index"
.- index
The index to show if
show = "index"
. Default is"R"
.- max_pixels
Maximum number of pixels to render in the interactive viewer.
- downsample
Optional downsampling factor for display purposes.
- type
Either
"crop"
(default) or"mask"
:"crop"
crops the mosaic to the bounding box of the shapefile."mask"
sets pixels outside the shapefile geometry toNA
(recommended when using exact shapes).
- ...
Additional arguments passed to
mosaic_view()
.
Details
Crop or mask a SpatRaster
object (mosaic
) based on user input from an
interactive map or by using a provided shapefile or another raster.
Examples
if (interactive() && requireNamespace("EBImage")) {
library(pliman)
# Load a sample raster
mosaic <- mosaic_input(system.file("ex/elev.tif", package = "terra"))
# Interactive cropping with drawn rectangle
cropped <- mosaic_crop(mosaic)
# View result
mosaic_view(cropped)
}