7

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

4
  • I'm using the The v.rast.stats module in GRASS GIS. working on a huge data set (all europe at 100 m resolution). I have 32GB of installed memory (RAM). I have this error: Raster MASK found, temporarily disabled ERROR: G_calloc: unable to allocate 236326912 * 1 bytes of memory at raster.c:83 ERROR: An error occurred while converting vector to raster any suggestion? thanks
    – user36143
    Commented Aug 22, 2014 at 14:33
  • This doesn't actually answer the question. Instead of posting an answer, please create a new question, with a link to this question if it is relevant. Commented Aug 22, 2014 at 14:52
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation.
    – Aaron
    Commented Aug 22, 2014 at 14:53
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.
    – vinayan
    Commented Aug 22, 2014 at 15:18

4 Answers 4

5

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.

2
  • 1
    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
    Commented Apr 27, 2013 at 17:57
  • 1
    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. Commented Apr 28, 2013 at 12:13
4

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.

3
  • See this Wiki page how to use GRASS GIS in QGIS: grasswiki.osgeo.org/wiki/GRASS_and_Sextante
    – markusN
    Commented Apr 27, 2013 at 9:24
  • 3
    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
    Commented Apr 27, 2013 at 10:15
  • 2
    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
    Commented Apr 27, 2013 at 12:04
1

For the record, SAGA GIS has a module "Grid Statistics" for polygons which can calculate the quantiles of a grid. http://www.saga-gis.org/saga_tool_doc/2.3.0/shapes_grid_2.html

This module is available in QGIS processing under the name raster statistics. If you want to calculate the median you can set "quantile" to 50.

1

If anyone is still facing this issue, QGIS's builtin raster zonal statistics can calculate medians and modes since at least version 2.14. They can be found in the Processing Toolbox, under Raster Analysis -> Zonal Statistics (2.18 QGIS geoalgorithms -> Raster tools -> Zonal Statistics for version 2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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