I try to perform some zonal statistics on a shape- and rasterfile and write the output to a completely new shapefile!
import sys
# where is the module
sys.path.append("/Applications/QGIS.app/Contents/Resources/python/")
sys.path.append('/Applications/QGIS.app/Contents/Resources/python/plugins')
import gdal
from qgis.analysis import *
from qgis.core import *
from PyQt4.QtCore import *
# initialize application
qpp = QgsApplication([], True)
QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/PlugIns", True)
QgsApplication.initQgis()
# rasterfile
filename = "/.../200001.asc"
vLayer = QgsVectorLayer("/.../my.shp", "plz", "ogr")
print vLayer.isValid()
> true
# zonal statistics
zonalStats = QgsZonalStatistics(vLayer, filename, 'pre-', 1)
zonalStats.calculateStatistics(None)
print zonalStats
> <qgis._analysis.QgsZonalStatistics object at 0x10cdf9e90>
# write shp
QgsVectorFileWriter.writeAsVectorFormat(zonalStats, "/.../zonal.shp", "utf-8", None, "ESRI Shapefile")
That returns.
TypeError: arguments did not match any overloaded call:
QgsVectorFileWriter.writeAsVectorFormat(QgsVectorLayer, QString, QString, QgsCoordinateReferenceSystem, QString driverName="ESRI Shapefile", bool onlySelected=False, QString errorMessage=None, QStringList datasourceOptions=QStringList(), QStringList layerOptions=QStringList(), bool skipAttributeCreation=False, QString newFilename=None, QgsVectorFileWriter.SymbologyExport symbologyExport=QgsVectorFileWriter.NoSymbology, float symbologyScale=1, QgsRectangle filterExtent=None): argument 1 has unexpected type 'QgsZonalStatistics'
QgsVectorFileWriter.writeAsVectorFormat(QgsVectorLayer, QString, QString, QgsCoordinateTransform, QString driverName="ESRI Shapefile", bool onlySelected=False, QString errorMessage=None, QStringList datasourceOptions=QStringList(), QStringList layerOptions=QStringList(), bool skipAttributeCreation=False, QString newFilename=None, QgsVectorFileWriter.SymbologyExport symbologyExport=QgsVectorFileWriter.NoSymbology, float symbologyScale=1, QgsRectangle filterExtent=None):argument 1 has unexpected type 'QgsZonalStatistics'
I assume that QgsVectorFileWriter.writeAsVectorFormat
needs something different than an object of type 'QgsZonalStatistics'
. But how would I write my shp
then?
I also tried
QgsVectorFileWriter.writeAsVectorFormat(QString(zonalStats), "/.../zonal.shp", "utf-8", None, "ESRI Shapefile")
which returned
NameError: name 'QString' is not defined
Best Answer
When you use
QgsZonalStatistics
, the results are are automatically added to the shapefile (look at Problem with calculating QGIS Zonal Statistics MEAN or EstadÃstica zonal con PyQGIS: clase QgsZonalStatistics)Detail
If you examine the functions in ZoneStat with see (equivalent of
dir
)And I did not understand how do we get the values (QGIS Zonal Stats Plugin - Python Console - compute one specific statistic, QgsZonalStatistics Class Reference)
You can also use the QGIS Zonal Statistics tool from the Processing plugin (look at How to calculate zonal statistics from multi-band raster for each polygon using python?
But I also do not understand why you use QGIS from outside when there are pure Python modules for that as rasterstats with one polygon here
And you can use Fiona to save the result to a new shapefile without the "complications" of PyQGIS from outside
New
If there are two or many polygons in the shapefile,
the results are for example
And