Skip to contents

Quickly partition a large raster mosaic into individual tiles using a polygon layer. Each tile is clipped by either the polygon's bounding box or (optionally) the exact feature geometry, and written to disk as a separate GeoTIFF named by the feature's unique_id.

Usage

mosaic_clip(
  mosaic,
  shapefile,
  unique_id = "unique_id",
  out_dir = NULL,
  overwrite = TRUE,
  verbose = TRUE,
  exact = FALSE,
  parallel = FALSE,
  workers = NULL
)

Arguments

mosaic

A terra::SpatRaster object or a file path pointing to a raster. In-memory rasters are first written to a temporary GeoTIFF.

shapefile

An sf::sf, terra::SpatVector, or path to a vector file. Must contain a column named unique_id for naming each output tile.

unique_id

A column present in shapefile that uniquely identifies the plots to be clipped.

out_dir

Directory where clipped rasters will be saved. Defaults to the current working directory. Created recursively if it does not exist.

overwrite

Logical; if TRUE (the default), existing files in out_dir with the same name will be overwritten.

verbose

Logical; if TRUE (default), progress bars and status messages will be shown.

exact

Logical; if FALSE (default), tiles are cropped by each feature's bounding box. If TRUE, the function extracts each polygon as a cutline for an exact crop (slower, but shape-accurate).

parallel

Logical; if TRUE (default), processing is parallelized using mirai. Set to FALSE for purely sequential execution.

workers

Integer; number of parallel daemons to launch when parallel = TRUE. Defaults to 70% of available cores.

Value

Invisibly returns a character vector of file paths to all clipped GeoTIFFs.

Details

Clip a Raster Mosaic by Polygons

This function wraps GDAL's warp utility for efficient raster clipping. When parallel = TRUE, it will spawn multiple workers via mirai and process tiles in batches. Use exact = TRUE to clip to the true polygon shape (at some extra cost), or leave exact = FALSE for a faster bounding-box crop.