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?

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)
  • how would I add a combination of several of them to the QgsZonalStatistics() function as numbers? I mean from a syntax point of view. Thanks for this great hint by the way! – Fitzroy Hogsflesh Feb 1 '16 at 11:21
  • no problems - added an example to the answer. – Steven Kay Feb 1 '16 at 13:49

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.