I am having some trouble integrating a rescaling method within a function that is calculating a vegetation index for a raster. I tried using a formula from this solution. The code would run, but I would get two warning messages and my image would be blank. I checked the rasters min and max values and they read "-Inf" "Inf" respectively. I also tried another way using the RPMG
library from this post, but ran into another error. This time after running the VARI
variable. I want to keep the rescaling method as "bland" as possible so I can integrate it into other indices such as the Triangular Greeness Index (TGI). Any suggestions?
Method 1:
# Visable Atmospherically Resistant Index
VARI.Overlay <- function(b1, b2, b3){
VARI.Calc <- (b1 - b3) / (b1 + b3 -b2)
VARI.Scale <- ((VARI.Calc - min(VARI.Calc)) / (max(VARI.Calc) - min(VARI.Calc)) - 0.5 ) * 2
return(VARI.Scale)
}
VARI <- overlay(img[[1]], img[[2]], img[[3]], fun = VARI.Overlay)
image(VARI, main = 'VARI')
Method 1 Error:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
Method 2:
# Visable Atmospherically Resistant Index
VARI.Overlay <- function(b1, b2, b3){
VARI.Calc <- (b1 - b3) / (b1 + b3 -b2)
VARI.min <- min(VARI.Calc)
VARI.max <- max(VARI.Calc)
VARI.Scale <- RESCALE(VARI.Calc, -1, 1, VARI.min, VARI.max)
return(VARI.Scale)
}
VARI <- overlay(img[[1]], img[[2]], img[[3]], fun = VARI.Overlay)
Method 2 Error:
Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE, :
cannot use this formula, probably because it is not vectorized