4

I'm trying to make a sum of n rasters and create a new one using QgsRasterClaculator. The problem is, every time the code reaches processCalculation, it makes QGIS close and create a dump file. I don't know if I'm doing it wrong, but I'd like to ask you for some help in how I can do it.

That's how I'm trying to do:

entries = []
rasters = os.listdir("C://test/Rasters/")
for i in range(0, len(rasters)):
        raster = "C://test/Rasters/"+rasters[i]

        resp = QgsRasterLayer(raster)
        rast1 = QgsRasterCalculatorEntry()
        rast1.raster = resp
        rast1.ref = "rast"+str(i+1)
        print(rast1.ref)
        rast1.bandNumber = 1
        entries.append(rast1)

        if(i == len(rasters)-1):
            var = var + 'rast'+str(i+1)  
        else:
            var = var + 'rast'+str(i+1)+' + '

calc = QgsRasterCalculator( var, 'C://test/Rasters/raster_somado.tif', 'GTiff', resp.extent(), resp.width(), resp.height(), entries )
print(calc.processCalculation())

I think the error might be happening because I'm using a variable (var), that contains the rasters alias. The base code I found and that works is

calc = QgsRasterCalculator( 'rast1 + rast2', 'C://test/Rasters/raster_somado.tif', 'GTiff', resp.extent(), resp.width(), resp.height(), entries )
print(calc.processCalculation())

Can anybody help me? I already tried some things but it's still not working, QGIS keeps creating the dump and closes.

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
  • Has something happened to the formatting of your code in this post? The indentation looks wrong... – ndawson Nov 18 '14 at 20:08
  • Yeah, looks like the last two lines is inside the for loop, but it's not, it's outside, don't know what happened. Now it's okay, thanks – Alyson Pereira Nov 19 '14 at 10:22

2 Answers2

4

thanks for the answers. The problem was that I had to set both 'resp' and 'rast1' variables to None after each iteration in the for loop, so my code end up like this

entries = []
rasters = os.listdir("C://test/Rasters/")
for i in range(0, len(rasters)):
    raster = "C://test/Rasters/"+rasters[i]

    resp = QgsRasterLayer(raster)
    rast1 = QgsRasterCalculatorEntry()
    rast1.raster = resp
    rast1.ref = "rast"+str(i+1)
    print(rast1.ref)
    rast1.bandNumber = 1
    entries.append(rast1)
    rast1 = None
    resp = None

    if(i == len(rasters)-1):
        var = var + 'rast'+str(i+1)  
    else:
        var = var + 'rast'+str(i+1)+' + '

calc = QgsRasterCalculator( var, 'C://test/Rasters/raster_somado.tif', 'GTiff', resp.extent(), resp.width(), resp.height(), entries )
print(calc.processCalculation())

And it worked

0

The issue is likely that 'resp' goes out of scope and is deleted when the for loop ends. Then when you try to use it in the calc = QgsRasterCalculator... line QGIS crashes. Try initializing 'resp' outside of your loop first. Something like:

...
resp = None
for i in range(0, len(rasters)):
...

should do it.

ndawson
  • 27,620
  • 3
  • 61
  • 85
  • I was using 'resp' as a global variable, so I think when the for loop ends, it will exist and will have the value of the last QgsRasterLayer. But I tried to initiate it with None, and QGis still closes on calc.processCalculation() :( – Alyson Pereira Nov 19 '14 at 11:05