3

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.

  • Have you tried calling other attributes using this method? – Trevor J. Smith Feb 18 '16 at 15:17
  • What happens if you replace QgsZonalStatistics.Mean with zoneStat.Mean? – Joseph Feb 18 '16 at 15:20
  • If I replace QgsZonalStatistics.Mean with zoneStat.Mean, I get: name 'zoneStat' is not defined. Also if I try to call QgsZonalStatistics.Sum, I get: type object 'QgsZonalStatistics' has no attribute 'Sum' – ELD Feb 18 '16 at 15:22
  • And if you replace QgsZonalStatistics.Mean with QgsZonalStatistics.Statistic.Mean? – ArMoraer Feb 18 '16 at 15:28
  • I get the same: type object 'QgsZonalStatistics' has no attribute 'Statistic' – ELD Feb 18 '16 at 15:30
3

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.

  • 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 Feb 18 '16 at 17:16
  • I also compared my result (with the python script) to the Zonal statistics plugin and I get the same numbers for my polygons... – ELD Feb 18 '16 at 17:23
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.