I would like to perform a raster calculation with the if condition and I wrote this code:
import qgis
import gdal
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from qgis.core import QgsRasterLayer
inputpath='/Users/macbook/Desktop/TESI/PROGETTO/rasterUfinali/U_1990.tif'
inputpath2='/Users/macbook/Desktop/TESI/PROGETTO/SIGMAmax/SIGMAmax,har.tif'
inputpath3 = '/Users/macbook/Desktop/TESI/PROGETTO/sigm/sigmahar90.tif'
inputpath4 = '/Users/macbook/Desktop/TESI/PROGETTO/lambdamin/lambminhar90.tif'
outputpath = '/Users/macbook/Desktop/TESI/PROGETTO/alfamax/alphamaxhar90.tif'
inputrasterfile = QgsRasterLayer(inputpath)
entries = []
ras = QgsRasterCalculatorEntry()
ras.ref = 'ras@1'
ras.raster = inputrasterfile
ras.bandNumber = 1
entries.append( ras )
inputrasterfile2 = QgsRasterLayer(inputpath2)
ras2 = QgsRasterCalculatorEntry()
ras2.ref = 'ras@2'
ras2.raster = inputrasterfile2
ras2.bandNumber = 1
entries.append( ras2 )
inputrasterfile3 = QgsRasterLayer(inputpath3)
ras3 = QgsRasterCalculatorEntry()
ras3.ref = 'ras@3'
ras3.raster = inputrasterfile3
ras3.bandNumber = 1
entries.append( ras3 )
inputrasterfile4 = QgsRasterLayer(inputpath4)
ras4 = QgsRasterCalculatorEntry()
ras4.ref = 'ras@4'
ras4.raster = inputrasterfile4
ras4.bandNumber = 1
entries.append( ras4 )
if '(ras@2 - ras@1) > 0':
alpha_max='1'
else: alpha_max = 'ras@3 / (1-2.71828^(ras@4))'
new_path = outputpath
print(alpha_max)
alfamax= QgsRasterCalculator(alpha_max, new_path, 'GTiff', inputrasterfile.extent(), inputrasterfile.width(), inputrasterfile.height(), entries )
alfamax.processCalculation()
iface.addRasterLayer(new_path)
The code works and print a valid output raster, but it doesn't respect the "if condition", because it prints out alpha_max = 1
even where (ras@1 - ras@1)<0
.
Can someone tell me where is the mistake?