2

I want to extract the sum of my raster cell values with a specific area through zonal statistics and have prepared the following code (see below). When I run the code I get the following error message (see bottom).

Any suggestions?

from qgis.analysis import QgsZonalStatistics

#specify polygon shapefile vector
polygonLayer = QgsVectorLayer('C:/Users/Anna Nilsson/Desktop/AIRPOLIM-ES/GIS data/Kenya/cpp_KE_buf1.gpkg', 'zonepolygons', "ogr") 

QgsProject.instance().addMapLayer(polygonLayer)

# specify raster filename
rasterFilePath = 'C:/Users/Anna Nilsson/Desktop/AIRPOLIM-ES/GIS data/Kenya/KEN_worldpop/KEN_popmap15adj_v2b.tif'

# usage - QgsZonalStatistics (QgsVectorLayer *polygonLayer, const QString &rasterFile, const QString &attributePrefix="", int rasterBand=1)
zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1,QgsZonalStatistics.Sum)
zoneStat.calculateStatistics(None)

Error message:

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

1 Answer 1

3

Error message says "QgsZonalStatistics has different usages, and one of the argument types for every usage is wrong."

If you look at QgsZonalStatistics Class Reference you will see that the second parameter has to be a raster layer reference, not a raster path. The usage you specify in your code belongs to QGIS 2.x. In QGIS 3.x, you have to define a raster reference by QgsRasterLayer and pass as an argument to QgsZonalStatistics (constructor) method.

from qgis.analysis import QgsZonalStatistics

polygonLayer = QgsVectorLayer('C:/Users/Anna Nilsson/Desktop/AIRPOLIM-ES/GIS data/Kenya/cpp_KE_buf1.gpkg', 'zonepolygons', "ogr")     
QgsProject.instance().addMapLayer(polygonLayer)

rasterFilePath = 'C:/Users/Anna Nilsson/Desktop/AIRPOLIM-ES/GIS data/Kenya/KEN_worldpop/KEN_popmap15adj_v2b.tif'

### ADD NEXT TWO LINES TO CHANGE "rasterFilePath" INTO "rasterLayer"
rasterLayer = QgsRasterLayer(rasterFilePath, "Raster Layer")
QgsProject.instance().addMapLayer(rasterLayer)

zoneStat = QgsZonalStatistics (polygonLayer, rasterLayer , 'pre-', 1,QgsZonalStatistics.Sum)
zoneStat.calculateStatistics(None)
2
  • Many thanks, this seem to work better. Now I came across an additional issue; my input vector layer is a temporary file and has therefore not have any location. How can I call a temporary layer in PyQGIS?
    – A.N
    Jul 18, 2019 at 9:21
  • Please review the answer in this post: Getting layer by name in PyQGIS?. Jul 18, 2019 at 9:35

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.