Skip to contents

Process a SpatRaster in parallel tiles

Usage

hsi_tiled(fun, x, n_tiles, filename = "", overwrite = FALSE, ...)

Arguments

fun

Function. Applied to each tile. Must be written as an anonymous function with explicit HSItools:: namespacing, e.g. \(tile) HSItools::hsi_smooth_savgol(tile, p = 3, n = 17). Only suitable for per-pixel operations with no spatial neighbourhood dependency. All parameters must be supplied as literal values — variables from the calling environment are not visible to parallel workers.

x

A SpatRaster with hyperspectral data.

n_tiles

Integer or integer vector of length 1 or 2. Number of tiles to split x into. A single integer creates row strips (e.g. 30). A length-2 vector creates a 2D tile grid (e.g. c(8, 8)). For best performance, match to the number of available mirai daemons.

filename

Character. Output filename. Default "" writes the result to a session-scoped temporary file and emits a warning. Providing a path is strongly recommended. Unlike other HSItools functions, filename = "" never keeps the result in memory — see Details.

overwrite

Logical. Overwrite existing file. Default FALSE.

...

Additional arguments passed to terra::writeRaster().

Value

A SpatRaster merged from processed tiles.

Details

Parallelism is provided by mirai::mirai_map() and mirai::daemons(). Daemons must be initialised by the caller before invoking this function via mirai::daemons(n). If no daemons are active, the function errors.

This function does not support in-memory processing. terra::makeTiles() requires a filename and errors if one is not provided — tiles are always written to disk. As a consequence, the in_memory parameter present in other HSItools functions is intentionally absent here. The merged result is always file-backed: either the path supplied via filename, or a session-scoped temporary file when filename = "". In the latter case a warning is emitted and the temporary file persists until the R session ends.

Intermediate tiles are written to a session-scoped temporary directory that is not cleaned up on function exit. This is intentional: persistent mirai daemon processes may hold open GDAL file handles to tile files after the function returns, and premature cleanup causes access violations on Windows.

Examples

if (FALSE) { # \dontrun{
mirai::daemons(30)

# Recommended: always provide a filename
hsi_tiled(
  fun = \(tile) HSItools::hsi_smooth_savgol(tile, p = 3, n = 17),
  x = my_raster,
  n_tiles = 30,
  filename = "output.tif",
  overwrite = TRUE
)

# Bad: variables from calling environment are not visible to workers
p <- 3
n <- 17
hsi_tiled(
  fun = \(tile) HSItools::hsi_smooth_savgol(tile, p = p, n = n),
  x = my_raster,
  n_tiles = 30
)

mirai::daemons(0)
} # }