How can I write a QGIS script for making a loop of raster calculator through a set of rasters? I know I can read multiple raster by applying:
lddLrs = qgis.utils.iface.legendInterface().layers()
for lyr in lddLrs:
And I want to apply an easy operation such as “Raster/Raster*Raster” and repeat it through multiple rasters and get the result with the same name of the input raster with a suffix.
Any Idea?
For using the script to make a new tool within the "processing Toolbox - Scripts - Create New Script" I made the following script but it doesn't work. Any idea to fix it?
##NoDATA Background=name
##lyr=multiple raster
##OUT=output raster
import qgis
from qgis.analysis import QgsRasterCalculatorEntry, QgsRasterCalculator
# Get layer object
layer = processing.getObject(lyr)
lddLrs = qgis.utils.iface.legendInterface().layers()
for lyr in lddLrs:
entries = []
ras = QgsRasterCalculatorEntry()
ras.ref = 'lyr@1'
ras.raster = lyr
ras.bandNumber = 1
entries.append( ras )
calc = QgsRasterCalculator( '("lyr@1" / "lyr@1") * "lyr@1"', +lyr.name() + "_suffix.tif", 'GTiff', lyr.extent(), lyr.width(), lyr.height(), entries )
calc.processCalculation()