3

I'm applying the Green Leaf Index (GLI) to a multi-temporal set of RGB raster images to generate false colour overlays of the sections of the hay field. I can create GLI layers using the raster calculator and the RGB channels manually in the raster calculator. I'd like to speed up the process.

I'm looking for some pointers in how to set up a batch process in the raster calculator (or alternative process) so I can just enter the list of raster files and have the GLI generated for each raster on the input.

I'm just getting started on Python, so don't make any assumptions on the knowledge side.

2 Answers 2

2

You can try using the "Graphical Modeler..." placed in the Processing tab.

As input use the "Multiple input" for storing the list of raster files, see image below

example

And later connect this input with a "Raster calculator" from Algorithms. Mind the formula

formula


References:

2
  • This would probably be the way to go as you prefer to use batch processes!
    – MarcM
    Commented Aug 14, 2020 at 6:26
  • Raster calculator requires bands in "[layer name]@[band number]" format. How do I specify this in a batch calculation? Commented Jun 26, 2023 at 14:44
1

Now below is a script that could give you a hint on how to do it. Please mind I did not have the opportunity to test it. There are numerous GLI so adapt to the one you use. e.g. SPOT7 would be

2GREEN - RED - BLUE / (2GREEN + RED + BLUE)

Make the following script into a function and call the function with each time your raster as the input fn_template.

Calculate GLI for 1 raster file:

#imports
import gdal

fn_template   = "string to .tif"         #original raster
fn_GLI        = "string to .tif"         #GLI raster

#open file and make copy
fn_template = gdal.Open(fn_template)
driver_tiff = gdal.GetDriverByName("GTiff")
ds_new = driver_tiff.CreateCopy(fn_GLI, ds_template, strict=0)

#make NumPy array of bands R,G, and B
RED = ds_template.GetRasterBand(1).ReadAsArray()
GREEN = ds_template.GetRasterBand(1).ReadAsArray()
BLUE = ds_template.GetRasterBand(1).ReadAsArray()

#get GLI
GLI = (2*GREEN - RED - BLUE) / (2*GREEN + RED + BLUE)

#write to output raster
ds_new.GetRasterBand(1).WriteArray(GLI)

Since you're new to Python, let me know if you need help writing this in a function. You'd also need to structure your input files into a list of sorts to iterate through as inputs for your function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.