I am a QGIS beginner and I want to combine two layers using the raster calculator via Python. For the beginning, I simply want to add the two layers but currently, I don't get an output file.
Code is the following:
lyr1 = QgsRasterLayer(r'C:\layer1.tif')
lyr2 = QgsRasterLayer(r'C:\layer2.tif')
output = r'C:\output.tif'
entries = []
ras = QgsRasterCalculatorEntry()
ras.ref = 'ras@1'
ras.raster = lyr1
ras.bandNumber = 1
entries.append(ras)
ras = QgsRasterCalculatorEntry()
ras.ref = 'ras@2'
ras.raster = lyr2
ras.bandNumber = 1
entries.append(ras)
calc = QgsRasterCalculator('ras@1 + ras@2', output, 'GTiff', \
lyr1.extent(), lyr1.width(), lyr1.height(), entries)
calc.processCalculation()