Dear QGIS Users,

I am trying to run a script in python console to calculate zonal statistics
but it always throws an error. This is the script that I am running:

import qgis.analysis
vectorlayer=r'E:\filepath\vector.shp'
rasterfile = r'E:\filepath\raster.tif'
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(1), 1)
zonalstats.calculateStatistics(None)

The error says:

TypeError: arguments dit not match any overloaded call:
QgsZonalStatistics(QgsVectorLayer, QString, QString attributePrefix="", int
rasterBand=1): argument 1 has unexpected type 'str'
QgsZonalStatistics(QgsZonalStatistics): argument 1 has unexpected type 'str'

However, if I load my raster and vector file in QGIS and run the script
below, it is working properly:

import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(1), 1)
zonalstats.calculateStatistics(None)

Thanks in advance for any help.
-Leo
_______________________________________________
Qgis-user mailing list
Qgis-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to