1

I would like to make the sum of 12 input rasters in order to have one output raster, the sum. I run this code, that works, but the output raster has all the cells with invalid data (1.79769e+308). If I make the same calculation manually, not in a python script, with the Raster Calculator, it gives me the right result. Can someone tell me where I make mistake?

import qgis
import gdal 
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from qgis.core import QgsRasterLayer

suffix_input=['I_jan', 'I_feb', 'I_mar', 'I_apr', 'I_may', 'I_jun', 'I_jul', 'I_aug', 'I_sep', 'I_oct', 'I_nov' ,'I_dec']
inputpath = '/Users/macbook/Desktop/TESI/PROGETTO/I/'

expr = ''
rasters = []
for i, suffix in enumerate(suffix_input):
     inputrasterfile = QgsRasterLayer(inputpath + suffix + ".tif", suffix)
     rasters.append(inputrasterfile)

     expr = expr + '"{}@1"+'.format(suffix)
 
expr = expr.rstrip('+')
print(expr)

alg_params = {
     'CELLSIZE': 0,
     'CRS': None,
     'EXPRESSION': expr,
     'EXTENT': rasters[0].extent(),
     'LAYERS': rasters,
     'OUTPUT': '/Users/macbook/Desktop/TESI/PROGETTO/Itot2.tif'
}

result = processing.run('qgis:rastercalculator', alg_params)
iface.addRasterLayer('/Users/macbook/Desktop/TESI/PROGETTO/Itot2.tif')

1 Answer 1

1

In following code I commented with # character, your suffix_input, inputpath and 'OUTPUT' path and I used my owns paths for testing the code. As I only have three layers in equivalent folder to your '/Users/macbook/Desktop/TESI/PROGETTO/I/', my suffix_input list only has three elements. On the other hand, as CRS was considered as None, for an adequate execution of code, you need to change it in status bar to CRS of your raster layers.

import qgis
import gdal 
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from qgis.core import QgsRasterLayer

#suffix_input=['I_jan', 'I_feb', 'I_mar', 'I_apr', 'I_may', 'I_jun', 'I_jul', 'I_aug', 'I_sep', 'I_oct', 'I_nov' ,'I_dec']
suffix_input=['I_jan', 'I_feb', 'I_mar']
#inputpath = '/Users/macbook/Desktop/TESI/PROGETTO/I/'
inputpath = '/home/zeito/Desktop/I/'

expr = ''
rasters = []
for i, suffix in enumerate(suffix_input):
     inputrasterfile = QgsRasterLayer(inputpath + suffix + ".tif", suffix)
     rasters.append(inputrasterfile)

     expr = expr + '"{}@1"+'.format(suffix)
 
expr = expr.rstrip('+')
print(expr)

alg_params = {
     'CELLSIZE': 0,
     'CRS': None,
     'EXPRESSION': expr,
     'EXTENT': rasters[0].extent(),
     'LAYERS': rasters,
     'OUTPUT': '/home/zeito/Desktop/PROGETTO/Itot2.tif' #'/Users/macbook/Desktop/TESI/PROGETTO/Itot2.tif'
}

result = processing.run('qgis:rastercalculator', alg_params)
#iface.addRasterLayer('/Users/macbook/Desktop/TESI/PROGETTO/Itot2.tif')
iface.addRasterLayer('/home/zeito/Desktop/PROGETTO/Itot2.tif')

After all changes, I ran above code and it was produced sum raster as expected. It can be observed in following image. If you still have some issues for running this code probably your raster layers are bad aligned.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.