| rast_calc | R Documentation |
Evaluates an expression cell by cell across one or more .vec rasters that
share a grid, reading every input one tile-row strip at a time so no whole
band is ever resident. Inside expr each name in rasters refers to that
raster's strip as a numeric vector, so ordinary vectorised R expresses the
calculation: a band index (nir - red) / (nir + red), a reclassification
cut(dem, breaks), a threshold ifelse(slope > 30, 1L, 0L), or arithmetic
across layers. The result is written one strip at a time to a single-band
output.
rast_calc(
rasters,
expr,
band = 1L,
path = NULL,
dtype = "f32",
compression = c("fast", "balanced", "max")
)
rasters |
A named list of |
expr |
An expression in those names producing one value per cell (or a scalar, recycled). Evaluated against each strip with the caller's environment as the enclosing scope. |
band |
Band read from every input (1-based). Default 1. |
path |
Optional output |
dtype |
Storage dtype for |
compression |
Compression effort for |
This is the monoid fold tier of the spatial toolbox: bounded to one strip
per input, a single streaming pass, no spill. The rasters must share
dimensions and geotransform; warp() them onto a common grid first if they
do not. No sf is needed.
When path is NULL, a numeric matrix (row 1 northmost) carrying
gt, extent, and crs attributes. When path is given, the written
vectra_raster handle (invisibly).
warp() to align rasters onto a shared grid first, focal() for
neighbourhood rather than cellwise calculation.
nir <- matrix(c(40, 50, 60, 70), 2, 2)
red <- matrix(c(10, 20, 30, 40), 2, 2)
fn <- tempfile(fileext = ".vec"); fr <- tempfile(fileext = ".vec")
vec_write_raster(nir, fn, dtype = "f64", extent = c(0, 0, 2, 2))
vec_write_raster(red, fr, dtype = "f64", extent = c(0, 0, 2, 2))
ndvi <- rast_calc(list(nir = fn, red = fr), (nir - red) / (nir + red))
round(ndvi, 3)
unlink(c(fn, fr))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.