Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'd like to calculate zonal statistics for a raster where the zones are defined by polygons in a vector layer within QGIS. Specifically I'm interested in calculating the median and mode raster values for each feature. QGIS includes a 'zonal statistics' plugin, but it only seems to calculate mean and sum statistics.

Is there another plugin that could do this task for me? I've looked at the source code for the existing statistics plugin and it looks like it would be fairly easy to modify, although my skills in C++ are fairly lacking.

QgsZonalStatistics source: http://www.qgis.org/api/qgszonalstatistics_8cpp_source.html

How to use QGIS Zonal Stats Plugin from Python Console?

Raster statistics available in ArcGIS: http://resources.arcgis.com/en/help/main/10.1/index.html#//009z000000w7000000

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I haven't calculated mode values before (most frequently occurring value), but at least for quantile values you could try to use the plugin I coded (LecoS). Although not primarily intended for this use it is able to calculate some "zonal statistics" for polygon overlays.
How to:

  • Install numpy and scipy for your System and QGIS. On Windows choose the OSGEO4W advanced Install and select python-numpy and python-scipy. On Linux consult your linux package manager/compiler.
  • Download the experimental(!) LecoS plugin via the plugin downloader. The rest should be pretty self-explaining. enter image description here

Please pay attention that the plugin is still experimental and calculation might fail sometimes! If you want a clear procedure suited for your needs than i would advise you to look into numpy and also R with the raster and sp packages. I am also happy to see detailed bug reports and i will do the best i can to fix them. However if the calculation fails for you, you better lookout for other tools as well.

share|improve this answer
raster, numpy, and R while indeed can do those operations are limited on the amount of ram your machine has.(meaning if your raster is more than you memory can handle, you program will crush). If your data are problematic in that kind of aspect, I recommend importing and using specialized GIS software, like grass –  nickves Apr 27 '13 at 17:57
I've managed to adapt the code from LecoS to do exactly what I need to do. I never would have though to use PIL to rasterize the features - very clever. The polygons I'm using are individual buildings and thus aren't very large, so I don't expect to hit any memory allocation issues. –  Snorfalorpagus Apr 28 '13 at 12:13

The v.rast.stats module in GRASS GIS is very powerful and will allow you to calculate univariate statistics (n, min, max, range, mean, stddev, variance, coeff_var, sum) from a raster map based on polygon boundaries.

share|improve this answer
See this Wiki page how to use GRASS GIS in QGIS: grasswiki.osgeo.org/wiki/GRASS_and_Sextante –  markusN Apr 27 '13 at 9:24
I think the OP wants to calculate the median and mode values, which is not possible with the v.rast.stats tool (you listed the available stats yourself) –  Curlew Apr 27 '13 at 10:15
the documentation page provided above states that the -e flag can be used to calculate the median value (as well as the percentiles). the mode shouldn't be too hard to calculate with some scripting. –  dmci Apr 27 '13 at 12:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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