2

I think this is more of a syntax problem that I have here. But I am absolutely not able to figure it out. On a different Linux machine I got the following working:

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1, QgsZonalStatistics.Mean())
zoneStat.calculateStatistics(None)

to just calculate the mean and then append it to the .dbf file of the shapefile. The error I get is TypeError: 'Statistic' object is not callable. I see whats the problem, but even after studying the API I couldn't figure it out. What am I doing wrong here?

1 Answers1

4

If you look at QgsZonalStatistics.Mean it's part of an enumeration - it's actually the integer 4. You're calling it as if it's a function.

Try removing the brackets:-

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1, QgsZonalStatistics.Mean)
zoneStat.calculateStatistics(None)

Note that these values are done as powers of two; to get a combination of several stats, you can sum them (or use a binary or)

e.g. to get maximum, minimum and mean use the python bitwise or (the 'pipe' character)

QgsZonalStatistics.Mean | QgsZonalStatistics.Max | QgsZonalStatistics.Min

so...

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1, QgsZonalStatistics.Mean | QgsZonalStatistics.Max | QgsZonalStatistics.Min)
zoneStat.calculateStatistics(None)
Steven Kay
  • 20,405
  • 5
  • 31
  • 82