By ELD


2016-02-18 14:52:34 8 Comments

I need to calculate some zonal statistics from raster files for some polygons in a shapefile. I am using the following code which works:

    eligibleLayer = QgsVectorLayer("/home/usr/Desktop/polygons.shp", "polygons", "ogr")
    for raster in findRasters(windMapsDirectory, '*.tif'):
        (infilepath, infilename) = os.path.split(raster) 
        windMapName = infilename
        list1 = re.split('\W+',windMapName)
        list2 = re.split("_", list1[0])
        list3 = list2[2]
        string1 = ''.join(list3)
        if string1 == 'K':
            zoneStat = QgsZonalStatistics(eligibleLayer, windMapName, 'K_', 1)
            zoneStat.calculateStatistics(None)

The problem is that I only need the mean from the zonal statistics (no count and sum). I tried using:

        if string1 == 'K':
            zoneStat = QgsZonalStatistics(eligibleLayer, windMapName, 'K_', 1, QgsZonalStatistics.Mean)

However, I get the following error: type object 'QgsZonalStatistics' has no attribute 'Mean' . I am using QGIS 2.8.6.

2 comments

@ArMoraer 2017-01-26 08:51:52

A bit late, but I just had the same issue using QGIS 2.14 (type object 'QgsZonalStatistics' has no attribute 'Mean', with a valid QgsZonalStatistics object).

It turns out that it works well with QGIS 2.18, so there was probably some change in QGIS API that is not documented, or a bug that was corrected.

@xunilk 2016-02-18 17:07:26

For some reason your code is not producing a QgsZonalStatistics object. Print 'zoneStats' to corroborate this. It should be observed some similar to:

<qgis._analysis.QgsZonalStatistics object at 0x9fc316ec>

I tried out the next code:

from qgis.analysis import QgsZonalStatistics

mapcanvas = iface.mapCanvas()

layers = mapcanvas.layers()
provider = layers[1].dataProvider()

path = provider.dataSourceUri()

zoneStat = QgsZonalStatistics(layers[0], path,"", 1, QgsZonalStatistics.Mean)

print zoneStat

zoneStat.calculateStatistics(None)

for this situation:

enter image description here

and, after running the script, it was added only the mean value; as it can be observed at the next image:

enter image description here

It works perfectly for me.

@ELD 2016-02-18 17:16:33

I tried to print out zoneStat after my first condition with the K string and I get: <qgis._analysis.QgsZonalStatistics object at 0x7fc3fc785a68> <qgis._analysis.QgsZonalStatistics object at 0x7fc3fc785b00> <qgis._analysis.QgsZonalStatistics object at 0x7fc3fc785b00> <qgis._analysis.QgsZonalStatistics object at 0x7fc3fc785a68> <qgis._analysis.QgsZonalStatistics object at 0x7fc3fc785b00>

@ELD 2016-02-18 17:23:26

I also compared my result (with the python script) to the Zonal statistics plugin and I get the same numbers for my polygons...

Related Questions

Sponsored Content

4 Answered Questions

0 Answered Questions

Zonal Statistics calculator differences between QGIS Lyon and Wien

1 Answered Questions

[SOLVED] Conditional zonal statistics

0 Answered Questions

QGIS Zonal Statistics for polygons is missing results?

1 Answered Questions

1 Answered Questions

1 Answered Questions

0 Answered Questions

Does zonal statistics do weighted average?

1 Answered Questions

Sponsored Content