
Crop and mask many raster files to M (or any polygon), safely and on disk
Source:R/crop.R
crop_rasters.RdCuts every raster file in a set of folders (e.g. all CHELSA or CHELSA-TraCE21k time slices) to the accessible area or another polygon and writes the results to a new folder. Designed for large collections: the work is done by GDAL directly from file to file, so memory use stays low however large the rasters are, finished files are skipped when the run is repeated, and one failing file does not stop the others.
Usage
crop_rasters(
files,
m,
out_dir,
variables = NULL,
pattern = "\\.tif{1,2}$",
recursive = TRUE,
mask = TRUE,
all_touched = FALSE,
overwrite = FALSE,
compress = "LZW",
verbose = TRUE
)Arguments
- files
Raster files and/or folders to search.
- m
The area to cut to: an
m_areafromdelimit_m(), polygons (sf, terra::SpatVector) or a path (for a GeoPackage written bywrite_m_area()itsMlayer is used).- out_dir
Output folder (created if needed). Output files keep their names; it must not be an input folder.
- variables
Optional variable names to select, matched as whole parts of the file name between
_,-or.(e.g.c("bio01", "bio12", "scd")selectsCHELSA_TraCE21k_bio01_-100_V1.0.tifbut not..._bio010_...). Case-insensitive.- pattern
Regular expression for the files searched in folders.
- recursive
Search sub-folders.
- mask
Set cells outside the polygon to NoData (default). With
FALSEthe rasters are only cropped to the polygon's extent.- all_touched
Keep every cell touched by the polygon, not only cells whose centre is inside.
- overwrite
Re-create output files that already exist (default: skip them, so an interrupted run can simply be repeated).
- compress
GeoTIFF compression (
"LZW","DEFLATE","ZSTD"or"NONE").- verbose
Print progress.
Value
Invisibly, a data.frame with input, output and status
(written, skipped (exists), no overlap or failed: ...).
Details
Values are copied exactly. Many climate datasets (CHELSA among them)
store integers with a scale and offset in the file metadata (e.g.
temperature x 10 in Kelvin). Cropping by reading the values into R and
writing them back in the original integer type can silently round them
and drop the scale and offset (we found errors of up to 0.95 deg C with
terra::crop(..., datatype = datatype(r))). Here the raw values, the data
type, the scale, offset and NoData value are kept, and the output grid is
aligned exactly to the input grid, so no cell is resampled.
A cell is kept when its centre lies inside the polygon (all_touched = FALSE, the usual rule) or when the polygon touches it at all
(all_touched = TRUE). Cells outside are set to NoData; if a file has no
NoData value, the extreme value of its data type is used (e.g. 65535 for
unsigned 16-bit integers, NaN for floating point).
Examples
sim <- simulate_enm_data(seed = 1, n_presence = 150)
m <- delimit_m(sim$presence, link_km = 120, thin_km = 5)
src <- file.path(tempdir(), "layers")
dir.create(src, showWarnings = FALSE)
terra::writeRaster(sim$env[[1]], file.path(src, "CHELSA_bio01_1981.tif"),
overwrite = TRUE)
terra::writeRaster(sim$env[[2]], file.path(src, "CHELSA_bio12_1981.tif"),
overwrite = TRUE)
res <- crop_rasters(src, m, file.path(tempdir(), "layers_M"),
variables = "bio01")
#> Cropping 1 file(s) to /tmp/RtmpDGf07B/layers_M
#> [1/1] written: CHELSA_bio01_1981.tif
#> Done: written = 1
res
#> input
#> 1 /tmp/RtmpDGf07B/layers/CHELSA_bio01_1981.tif
#> output status
#> 1 /tmp/RtmpDGf07B/layers_M/CHELSA_bio01_1981.tif written