Skip to contents

fn receives one traced array per input raster and returns one array; it runs fused with adjacent operations in one compiled kernel. Write it with plain arithmetic and the g_* vocabulary (g_ifelse(), g_bitand(), g_cast(), ...). Inputs must share a grid (align_to() first otherwise); rasters on different graphs merge automatically.

Usage

lazy_map(..., fn, dtype = NULL, bands = NULL)

Arguments

...

LazyRaster inputs (at least one), or a single LazyDataset.

fn

Function of as many arrays as there are inputs.

dtype

Optional output dtype override.

bands

LazyDataset only: bands to map over (default: all value bands).

Value

A LazyRaster, or a LazyDataset when given one.

Details

The output dtype defaults to the promoted input dtype; pass dtype when fn changes the value domain, e.g. "f32" for a mask that introduces NaN over an integer band.

Over a LazyDataset, fn is applied to every value band (a single dataset input only); bands restricts which bands, and non-selected bands pass through unchanged.

See also

Examples

f <- system.file("extdata", "garry-example.tif", package = "garry")
red <- lazy_source(f, band = 1L)
nir <- lazy_source(f, band = 3L)
ndvi <- lazy_map(nir, red, fn = function(n, r) (n - r) / (n + r))
range(collect(ndvi))
#> Error in confirm_plugin_install(platform, url): The "cpu" PJRT plugin needs to be downloaded for pjrt to work.
#> ℹ Automatic downloads are not performed in non-interactive sessions.
#> ℹ Set `PJRT_INSTALL` to "1" to allow the download, or set
#>   `PJRT_PLUGIN_PATH_CPU` to a local plugin file.