I'm trying to run an NDVI calculation on a set of raster layers. The approach is to adapt this QGIS 2.x code to run in GQIS 3.x. A far as I'm aware it's the "legendInterface" that's changed, but I'm getting lost trying to suss out how it changed, and what to use as an alternative.
lddLrs = qgis.utils.iface.legendInterface().layers()
path = "/QGIS/Project/"
for lyr in lddLrs:
entries = []
#Define red band#
ras1 = QgsRasterCalculatorEntry()
ras1.ref = 'layer.name() +@121'
ras1.raster = lyr
ras1.bandNumber = 121
entries.append(ras1)
#Define nir band#
ras2 = QgsRasterCalculatorEntry()
ras2.ref = 'layer.name() +@192'
ras2.raster = lyr
ras2.bandNumber = 192
entries.append( ras2 )
calc = QgsRasterCalculator( '("layer.name() +@192" - "layer.name()
+@121") / ("layer.name() +@192" + "layer.name() +@121")', path + lyr.name()
+ "_NDVI.tif", 'GTiff', lyr.extent(), lyr.width(), lyr.height(),
entries )
calc.processCalculation()