how can I calculate sums, averages etc of raster-points (multi-band) per polygon of a vector-layer. I was told that this is called "zonal statistics". I tried that with QGIS first.

There is a way to do it but that is much too slow (convert raster to vector, intersect with second vector-layer, calculate geometry, export numbers, calculate statistics with spreadsheet or other program, re-import the results, takes forever for my 350.000 raster-points).

I was also given the hint to use saga-gis. That has "zonal statistics" but those are based on categories from a raster-band, not on polygons from a vector-layer. So to use this I would have to convert my vector-layer to raster and then calculate the statistics.

This seems to be the wrong way to solve this. There would be no way to account for raster-points that belong to 2 or more polygons because they are intersected by the polygon-boundary. I assume that polygon-based statistics should be able to handle this so I also assume that I haven't found the correct module yet.

Saga-gis has really many modules. Please let me know which one is the right one for this application.

I'm trying Rudivonstaden's solution, and the following error messages appear when I enter the last two commands: >>> zonalstats = qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile) Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'module' object has no attribute 'analysis' >>> zonalstats.calculateStatistics(None) Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'zonalstats' is not defined I'm wondering if the commands are correct or if the raster file I'm using is in the right format? Thanks!
– Dan WolfMar 26 '13 at 16:24

Hi Dan. Stackexchange works a bit differently from a forum. The aim is to have a Q&A type interaction rather than an ongoing discussion. If the answers to the original question don't resolve what you need to know, you should either ask for clarification in the comments section or consider asking a new question.
– Jay GuarneriMar 26 '13 at 17:10

Thanks for your comment, Dan. My answer was missing a necessary import statement, which should now be fixed.
– rudivonstadenMar 26 '13 at 18:54

5 Answers
5

I was struggling to do exactly the same thing, but for various reasons I'm committed to using QGIS. I tried using v.rast.stats using the GRASS plugin and also via the Sextante plugin. The latter approach failed, because it seems to attach the stats to a temporary vector layer which it then deletes. The GRASS plugin worked, but it doesn't deal with overlapping polygons.

After doing some digging around (in the source of the promising-sounding ZonalStats plugin), I found that QGIS actually has zonal statistics methods built into the API, and these also have Python bindings. So as long as you are only looking for count, sum and mean statistics for your polygon features, the Python Console (Plugins > Python Console) is currently the easiest way to attach the stats to the polygon attribute table.

Select your raster layer in the TOC, and type the following into the console (it grabs the source file name of your raster layer)

>>> rasterfile = qgis.utils.iface.mapCanvas().currentLayer().source()

Select your vector layer, and execute the following command in the console (it grabs the vector layer itself)

>>> vectorlayer = qgis.utils.iface.mapCanvas().currentLayer()

Execute the following three commands in the console (they pass the vector layer and raster file to QGIS' built-in zonal stats calculator)

Note that if you want to append a prefix to the new fields (rather than just having them as sum, mean and count), then you would replace the first command in step 3 with the following (assuming you wanted to use 'zonal_' as the prefix:

This issue seems to have been solved already some time ago, but I´ll add my spoon to the soup anyway since I recently needed some raster stats myself.

The QGIS methodology described by rudivonstaden worked like a charm, thanks for the tip. However, in my case I also wanted to know min&max values and std for the raster values within my polygons, and that functionality I could´nt find in QGIS. I found a solution through the function isectpolyrst in GME, which is a favorite of mine for solving GIS problems with.This function gave me just the stats I needed and rather quickly as well.

PS. GME has a bit of an unfortunate dependency (ArcGIS). So even though the program itself is freely available to everyone, you need ArcGIS to run it. But if you do have ArcGIS (any level of licence), I recommend you try it out.

EDIT:

I got the stats I wanted, but I later found out that I jumped to conclusions a little hastily after looking at the actual numbers. You get stats, yes, but only for the first band in a raster. So if you operate with a 3-band raster image, the stats are incomplete. I should have pointed out the number of channels in my original post. Best to use SAGA-GIS like stn proposed above if your working with multi band raster images.