I have a raster stack of 108 elements
The stack 'images' is created using 4 bands (RGB+NIR) of 27 satellite images. The sequence of the rasters in the stack is:
Position [[1]]: blue band image 1
Position [[2]]: green band image 1
Position [[3]]: red band image 1
Position [[4]]: NIR band image 1
Position [[5]]: blue band image 2
...
Position [[108]]: NIR band image 27
I want to derive two basic vegetation indices for each image (27 NDVI and 27 NDWI2 indices): NDVI – (NIR-R)/(NIR+R) and NDWI2 (G-NIR)/(G+NIR)
So far I have doing it manually. For example:
NDVI_1-> (images[[4]] - images[[3]]) / (images[[4]] + images[[3]])
NDVI_2-> (images[[8]] - images[[7]]) / (images[[8]] + images[[7]])
and
NDWI2_1-> (images[[2]] - images[[4]]) / (images[[2]] + images[[4]])
NDWI2_2-> (images[[6]] - images[[8]]) / (images[[6]] + images[[8]])
Since my raster stack as a pattern, I am looking for an automatic way to derive the indices. Something like a loop that calculates the indeces, save it with the appropriate name and then moves to the next 'position' to repeat the process.
Any help on that?
Best Answer
Just use a for loop with a little trick to iterate.
First, as always is recommended, a reproducible example:
Create two lists to store NDVI and NDWI indices:
Loop through
images
with a condition to skip every 4 layers per iteration:Check results (need to be the same, I used one scene in this example):