Monday, 14 September 2015

qgis - Is there a zonal statistics tool to calculate median and mode raster values for each polygon?


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



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...