I start to write my own simple code to automatically calculate NDVI from Landsat 5.
Here is my code:
from PyQt5.QtGui import *
from qgis.analysis import *
land_3 = 'D:\park\LT05_L1TP_131047_19871207_20170418_01_T1_B3.tif'
land_4 = 'D:\park\LT05_L1TP_131047_19871207_20170418_01_T1_B4.tif'
rasterb3 = iface.addRasterLayer(land_3, "B3")
rasterb4 = iface.addRasterLayer(land_4, "B4")
ir = QgsRasterCalculatorEntry()
r = QgsRasterCalculatorEntry()
ir = rasterb4
r = rasterb3
arr = (ir, r, ir, r)
exp = "1.0*(%s-%s)/(1.0*(%s+%s)"%arr
output = 'D:\\park\\output.tif'
e = rasterb3.extent()
w = rasterb3.width()
h = rasterb3.height()
entries = [ir,r]
ndvi = QgsRasterCalculatorEntry()
ndvi = QgsRasterCalculator(exp, output, "GTiff", e, w, h, entries)
ndvi.processCalculation()
lyr = iface.addRasterLayer(output,"NDVI")
But this error shown up:
Traceback ( most recent call last): File "D:\QGIS\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 25, in <module> TypeError: index 0 has type 'QgsRasterLayer' but 'QgsRasterCalculatorEntry' is expected.
e = rasterb3.extent()
, but line 25 is that in your code.