I am using rasterio to open image files related to 2 specific bands:
b4 = rio.open(r)
b8 = rio.open(poly)
I then read the bands into a python array, I think it's a numpy array but not sure?:
#Read Red(b4) and NIR(b8) as arrays
red = b4.read()
nir = b8.read()
Then I perform band based calculation against them via the following:
#Calculate NDVI
ndvi = (nir.astype(float) - red.astype(float)) / (nir + red)
Now, this of course can have the denominator (nir + red) be equal to zero, which will cause a divide by zero issue that I believe is messing with the results.
Is there a way to set the instances where the denominator is equal to zero to have a value of -1 or something? How are you handling those in your own raster functions?