% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/spatial_more.R
\name{rast_calc}
\alias{rast_calc}
\title{Cellwise calculation over aligned rasters (map algebra)}
\usage{
rast_calc(
  rasters,
  expr,
  band = 1L,
  path = NULL,
  dtype = "f32",
  compression = c("fast", "balanced", "max")
)
}
\arguments{
\item{rasters}{A named list of \code{vectra_raster} handles or \code{.vec} paths sharing
a grid. The names are the variables available inside \code{expr}.}

\item{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.}

\item{band}{Band read from every input (1-based). Default 1.}

\item{path}{Optional output \code{.vec} path. When given the result is streamed to
disk and the opened \code{\link[=vec_open_raster]{vec_open_raster()}} handle is returned invisibly; when
\code{NULL} the result is returned as an in-memory matrix.}

\item{dtype}{Storage dtype for \code{.vec} output (see \code{\link[=vec_write_raster]{vec_write_raster()}}).
Default \code{"f32"}.}

\item{compression}{Compression effort for \code{.vec} output. Default \code{"fast"}.}
}
\value{
When \code{path} is \code{NULL}, a numeric matrix (row 1 northmost) carrying
\code{gt}, \code{extent}, and \code{crs} attributes. When \code{path} is given, the written
\code{vectra_raster} handle (invisibly).
}
\description{
Evaluates an expression cell by cell across one or more \code{.vec} rasters that
share a grid, reading every input one tile-row strip at a time so no whole
band is ever resident. Inside \code{expr} each name in \code{rasters} refers to that
raster's strip as a numeric vector, so ordinary vectorised R expresses the
calculation: a band index \code{(nir - red) / (nir + red)}, a reclassification
\code{cut(dem, breaks)}, a threshold \code{ifelse(slope > 30, 1L, 0L)}, or arithmetic
across layers. The result is written one strip at a time to a single-band
output.
}
\details{
This is the \emph{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; \code{\link[=warp]{warp()}} them onto a common grid first if they
do not. No \pkg{sf} is needed.
}
\examples{
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))

}
\seealso{
\code{\link[=warp]{warp()}} to align rasters onto a shared grid first, \code{\link[=focal]{focal()}} for
neighbourhood rather than cellwise calculation.
}
