2
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry import processing
from osgeo import osr, gdal from gdalconst import *

dataset1 = gdal.Open(input1, GA_Update) band1 = dataset1.GetRasterBand(1) dataset1.SetProjection( srs.ExportToWkt() ) dataset1 = None

layer1=QgsRasterLayer(dataset1,'test1')
#layer1.setCrs( QgsCoordinateReferenceSystem(3003, QgsCoordinateReferenceSystem.EpsgCrsId) )

print('load first layer')

input2='D:\\ascii2grids\\dati_ascii\\PR_2018091500UTCplus1.txt'

dataset2 = gdal.Open(input2, GA_Update) band2 = dataset2.GetRasterBand(1) dataset2.SetProjection( srs.ExportToWkt() ) dataset2 = None

layer2=QgsRasterLayer(dataset2,'test2')

print('load II layer')

exp = "%s - %s" % (layer1,layer2)

output='C:\\Users\\assis\\test.tif'

entries=[layer1,layer2]

calc = QgsRasterCalculator(exp,
                    output,
                    'GTiff',
                    layer1.extent,
                    QgsCoordinateReferenceSystem("EPSG:3003"),
                    layer1.width,
                    layer1.height,
                    entries
                    )

res = calc.processCalculation(feedback) if res == QgsRasterCalculator.ParserError:
    raise QgsProcessingException(self.tr("Error parsing formula"))

This pyqgis script give me the following error:

Traceback (most recent call last):   File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)   File "<input>", line 1, in <module>   File "<string>", line 57, in <module> TypeError: QgsRasterCalculator(): arguments did not match any overloaded call:   overload 1: argument 4 has unexpected type 'builtin_function_or_method'   overload 2: argument 4 has unexpected type 'builtin_function_or_method'   overload 3: argument 1 has unexpected type 'str'

I do not understand the error. Perhaps the CRS definition in the QgsRasterCalculator function. Unfortunately I do not find any example of how to define CRS

1 Answer 1

5

You're specifying the function instead of calling it. I.e.

layer1.extent

should be:

layer1.extent()

Do this with width and height:

calc = QgsRasterCalculator(exp,
                        output,
                        'GTiff',
                        layer1.extent(),
                        QgsCoordinateReferenceSystem("EPSG:3003"),
                        layer1.width(),
                        layer1.height(),
                        entries
                        )
1
  • 2
    a small addition: PyQGIS always(?) uses methods instead of properties. I think this is because of sip, which is the way/interface PyQIS calls from C++ libraries. Jun 4, 2019 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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