View source: R/calculate_indices.R
calculate_indices | R Documentation |
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.
calculate_indices(
raster,
indices,
output_filename,
...,
cores = 1L,
wopt = list(),
overwrite = FALSE,
extra_objects = list(),
names_suffix = NULL
)
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 |
output_filename
, unchanged.
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.
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,
# in order to try and keep formulas from doing something bad:
example_indices <- filter_platforms(platforms = "Sentinel-1 (Dual Polarisation VV-VH)")[1, ]
example_indices$formula <- 'system("echo something bad")'
# So this will error:
try(
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
example_indices,
tempfile(fileext = ".tif")
)
)
# Because of this, formulas which try to use most R functions
# will wind up erroring as well:
example_indices$formula <- "pmax(VH, VV)"
try(
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
example_indices,
tempfile(fileext = ".tif")
)
)
# To fix this, pass the objects you want to use to `extra_objects`
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
example_indices,
tempfile(fileext = ".tif"),
extra_objects = list(pmax = pmax)
) |>
suppressWarnings(classes = "rsi_extra_objects")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.