calculate_indices {rsi} | R Documentation |
Calculate indices from the bands of a raster
Description
This function computes any number of indices from an input raster via
terra::predict()
. By default, this function is designed to work with
subsets of spectral_indices()
, but it will work with any data frame with a
formula
, bands
, and short_name
column.
Usage
calculate_indices(
raster,
indices,
output_filename,
...,
cores = 1L,
wopt = list(),
overwrite = FALSE,
extra_objects = list(),
names_suffix = NULL
)
Arguments
raster |
The raster (either as a SpatRaster or object readable by
|
indices |
A data frame of indices to compute. The intent is for this
function to work with subsets of spectral_indices, but any data frame with
columns |
output_filename |
The filename to write the computed metrics to. |
... |
These dots are for future extensions and must be empty. |
cores |
positive integer. If |
wopt |
list with named options for writing files as in |
overwrite |
logical. If |
extra_objects |
A named list of additional objects to pass to the
minimal environment that formulas are executed in. For instance, if you
need to use the |
names_suffix |
If not |
Value
output_filename
, unchanged.
Security
Note that this function is running code from the formula
column of the
spectral indices data frame, which is derived from a JSON file downloaded off
the internet. It's not impossible that an attacker could take advantage of
this to run arbitrary code on your computer. To mitigate this, indices are
calculated in a minimal environment that contains very few functions or
symbols (preventing an attacker from accessing, for example, system()
).
Still, it's good practice to inspect your formula
column to make sure
there's nothing nasty hiding in any of the formulas you're going to run.
Additionally, consider using pre-saved indices tables or
spectral_indices(download_indices = FALSE)
if using this in an unsupervised
workload.
Examples
our_raster <- system.file("rasters/example_sentinel1.tif", package = "rsi")
calculate_indices(
our_raster,
filter_bands(bands = names(terra::rast(our_raster))),
tempfile(fileext = ".tif"),
names_suffix = "sentinel1"
)
# Formulas aren't able to access most R functions or operators:
example_indices <- filter_platforms(platforms = "Sentinel-1 (Dual Polarisation VV-VH)")[1, ]
example_indices$formula <- 'system("echo something bad")'
try(
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
example_indices,
tempfile(fileext = ".tif")
)
)