geo_index_custom: Compute a Custom Spectral or Geospatial Index

View source: R/geo_index_custom.R

geo_index_customR Documentation

Compute a Custom Spectral or Geospatial Index

Description

Computes a custom spectral or geospatial index from a raster image or numeric inputs using a user-defined mathematical formula. The formula is securely parsed, validated against an AST whitelist of safe mathematical functions and operators, and evaluated vectorially with terra.

Usage

geo_index_custom(
  image,
  formula,
  bands = NULL,
  params = list(),
  name = "CustomIndex",
  scale_factor = NULL,
  denominator_tolerance = 1e-06,
  sensor = NULL,
  ...
)

Arguments

image

A terra::SpatRaster object or a character string specifying the file path to a raster image on disk.

formula

Character string specifying the mathematical expression to compute (e.g., "(nir - red) / (nir + red)" or "G * (nir - red) / (nir + C1 * red - C2 * blue + L)").

bands

Optional named vector or list specifying custom band mapping (e.g., c(nir = 4, red = 3) or c(nir = "B08", red = "B04")).

params

Optional named list of numeric parameter constants used in the formula (e.g., list(G = 2.5, C1 = 6.0, C2 = 7.5, L = 1.0)).

name

Character string specifying the name of the output index layer. Defaults to "CustomIndex".

scale_factor

Optional numeric scaling divisor (e.g., 10000 for Sentinel-2 or Landsat surface reflectance products) applied to input bands before evaluating the formula.

denominator_tolerance

Numeric threshold for near-zero denominators. Divisions where the absolute denominator is smaller than this value are safely converted to NA. Defaults to 1e-6.

sensor

Optional character string specifying a sensor preset for automatic band name resolution (e.g., "sentinel2", "landsat8").

...

Additional named numeric constants passed directly to the formula evaluation.

Value

A single-layer terra::SpatRaster (or numeric object) containing the computed custom index values, preserving all original spatial properties.

Examples

img <- get_example_data()

# 1. Simple custom ratio index
custom_ratio <- geo_index_custom(
  img,
  formula = "(nir - red) / (nir + red)",
  bands = c(red = "red", nir = "nir"),
  name = "CustomNDVI"
)
print(custom_ratio)

# 2. Custom parameterized index
custom_evi <- geo_index_custom(
  img,
  formula = "G * (nir - red) / (nir + C1 * red - C2 * blue + L)",
  bands = c(blue = "blue", red = "red", nir = "nir"),
  params = list(G = 2.5, C1 = 6.0, C2 = 7.5, L = 1.0),
  name = "CustomEVI"
)
print(custom_evi)


GeoIndexR documentation built on Oct. 10, 2026, 5:08 p.m.