Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two layers: a raster layer and a vector layer (polygon). I can see the values of the raster pixels but I don't know how to combine them with information from the polygon layer. More specifically, I would like to :

  1. sum values of all pixels within a the borders of a polygon
  2. count the number of pixels with a specific value within the borders of a polygon

Can anybody help and advise on how to do this in QGIS?

Many thanks!

share|improve this question
I'm not sure of the whole process in QGIS, but I would think the first step would be converting your raster to a vector layer. You can use QGIS' Polygonize tool and use your polygon layer as a mask. – Baltok Dec 6 '12 at 22:36

Use to be that you had to use v.rast.stats in the GRASS Plugin, but now you can use the inbuilt Zonal Statistics in the Raster menu.

You have a polygon layer loaded and the raster. You run the menu item.

that will answer part a) of your question.

v.rast.stats and other tools will give you more detailed output, but probably a simple workflow for b) is to extract each polygon (use the Raster|Extraction|Clipper menu) and then run a histogram on each. Depends on the structure of your data, how often you have to do it, etc.

share|improve this answer
remember that grass functions can be accessed with sextante plugin as well. With the new version of qgis sextane has been integrated in it (no more as a plugin, but as core component) so its a good time to learn it as well. – nickves Dec 6 '12 at 23:34

For your first objective, as Willy said you can use v.rast.stat module. The module will create new columns populated in your vector database with the statistical values you're looking for (min value, max , mean, standar deviation, count and etc).

As for your second question you need to "hide" the values which are bellow (or above) your threshold. One way to do this is by using qgis' raster calculator and entering the following expression to calculate :

(raster@1 >= 10) * raster@1

enter image description here

The parenthesis will create a mask, meaning all values above 10 will take the value of 1 and all values bellow the value of 0. And next you multiply both values with the original values. And obviously 0 by something equals 0.

Set the nodata value to be equal with 0 for the produced mask-raster and then re-run v.rast.stats.

share|improve this answer

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.