Process a SpatRaster in parallel tiles
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
SpatRasterwith hyperspectral data.- n_tiles
Integer or integer vector of length 1 or 2. Number of tiles to split
xinto. 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 availablemiraidaemons.- 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 otherHSItoolsfunctions,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.
See also
Other HSI Transformations:
hsi_apply_mnf(),
hsi_calc_difference(),
hsi_calc_mnf(),
hsi_calc_ndi(),
hsi_calc_raba(),
hsi_calc_rabd(),
hsi_calc_ratio(),
hsi_calc_rcv(),
hsi_calc_reflectance(),
hsi_calc_remp(),
hsi_calc_rmean(),
hsi_calc_rmedian(),
hsi_calc_rsd(),
hsi_calc_stretch(),
hsi_destripe(),
hsi_remove_continuum(),
hsi_smooth_median(),
hsi_smooth_savgol(),
hsi_write_scaled()
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)
} # }