library(ggplot2)
library(terra)
## Calculate NDVI
ndvi <- spectralIndices(lsat, red = "B3_dn", nir = "B4_dn", indices = "NDVI")
ndvi
ggR(ndvi, geom_raster = TRUE) +
scale_fill_gradientn(colours = c("black", "white"))
# \donttest{
## Calculate all possible indices, given the provided bands
## Convert DNs to reflectance (required to calculate EVI and EVI2)
mtlFile <- system.file("external/landsat/LT52240631988227CUB02_MTL.txt", package="RStoolbox")
lsat_ref <- radCor(lsat, mtlFile, method = "apref")
SI <- spectralIndices(lsat_ref, red = "B3_tre", nir = "B4_tre")
plot(SI)
## Custom Spectral Index Calculation (beta) (supports only bands right now...)
# Get all indices
# Supports: Parentheses (), Addition +, Subtraction -, Multiplication *, Division /
idxdb <- getOption("RStoolbox.idxdb")
# Customize the RStoolbox index-database and overwrite the option
cdb <- c(idxdb, CUSTOM = list( list(c("Mueller2024", "Super custom index"),
function(blue, red) {blue + red})))
rsOpts(idxdb = cdb)
# Calculate the custom index, (also together with the provided ones)
custom_ind <- spectralIndices(lsat, blue = 1, red = 3, nir = 4, indices = c("NDVI", "CUSTOM"))
# }
Run the code above in your browser using DataLab