5

I am struggling to sum some rasters using QgsRasterCalculator. When the step "calc.processCalculation()" is executed, QGIS crashes with a runtime error (R6025 - pure virtual function call) or a dump.

I have a list of raster paths generated in a for loop looking like the following one. Please tell me what do I do wrong and how could I make it work...

Here is my code:

from qgis.core import *
from PyQt4.QtCore import *
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry

s1 = "E:\rcalc\OUTPUT11.sdat"
s2 = "E:\rcalc\OUTPUT22.sdat"
s3 = "E:\rcalc\OUTPUT33.sdat"
list = [s1,s2,s3]

entries = []
formula = ""

for i in range(0, len(list)):
    raster = list[i]
    resp = QgsRasterLayer(raster)
    rast1 = QgsRasterCalculatorEntry()
    rast1.raster = resp
    rast1.ref = "rast"+str(i+1)
    rast1.bandNumber = 1
    entries.append(rast1)
    rast1 = None
    resp = None

    if(i == len(list)-1):
        formula = formula + 'rast'+str(i+1)  
    else:
        formula = formula + 'rast'+str(i+1)+' + '

resp = QgsRasterLayer(list[0])

calc = QgsRasterCalculator( formula, "E:\output.tif", 'GTiff', resp.extent(), resp.width(), resp.height(), entries )
calc.processCalculation()
2

I found the answer: When I replaced

resp = QgsRasterLayer(raster) 

with

resp = processing.getObject(raster)

The calculator worked fine.

In addition, the reference must have @ character to count the entries, so the line

rast1.ref = "rast"+str(i+1)

become

rast1.ref = "rast@"+str(i+1)

and the formula will be

rast@1 + rast@2 + rast@3 +...

When I used the formula

rast1 + rast2 + rast3

the result was a raster with a constant value of 6 (1+2+3). Hope my answer will be useful.

  • The runtime error was solved when I uninstalled and installed qgiss again. – Flaviu Meseșan Dec 30 '16 at 11:26

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.