Skip to contents

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, ..., names_suffix = NULL)

Arguments

raster

The raster (either as a SpatRaster or object readable by terra::rast()) to compute indices from.

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 formula (containing a string representation of the equation used to calculate the index), bands (a list column containing character vectors of the necessary bands) and short_name (which will be used as the band name) will work.

output_filename

The filename to write the computed metrics to.

...

These dots are for future extensions and must be empty.

names_suffix

If not NULL, will be used (with paste()) to add a suffix to each of the band names returned.

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

calculate_indices(
  system.file("rasters/example_sentinel1.tif", package = "rsi"),
  filter_platforms(platforms = "Sentinel-1 (Dual Polarisation VV-VH)"),
  tempfile(fileext = ".tif"),
  names_suffix = "sentinel1"
)
#> Warning: No cache file present and `download_indices` set to `FALSE`.
#>  Returning (likely outdated) package data instead.
#> [1] "/tmp/RtmplhkY2e/file1bbb2036573c.tif"

# 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")
  )
)
#> Error in system("echo something bad") : could not find function "system"