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'