View source: R/geo_index_custom.R
| geo_index_custom | R Documentation |
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.
geo_index_custom(
image,
formula,
bands = NULL,
params = list(),
name = "CustomIndex",
scale_factor = NULL,
denominator_tolerance = 1e-06,
sensor = NULL,
...
)
image |
A |
formula |
Character string specifying the mathematical expression to compute
(e.g., |
bands |
Optional named vector or list specifying custom band mapping
(e.g., |
params |
Optional named list of numeric parameter constants used in the formula
(e.g., |
name |
Character string specifying the name of the output index layer.
Defaults to |
scale_factor |
Optional numeric scaling divisor (e.g., |
denominator_tolerance |
Numeric threshold for near-zero denominators.
Divisions where the absolute denominator is smaller than this value are safely
converted to |
sensor |
Optional character string specifying a sensor preset for automatic
band name resolution (e.g., |
... |
Additional named numeric constants passed directly to the formula evaluation. |
A single-layer terra::SpatRaster (or numeric object) containing
the computed custom index values, preserving all original spatial properties.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.