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?

  • 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, 2012 at 22:36

2 Answers 2


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.

  • 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, 2012 at 23:34
  • just in case anyone else gets stuck, Zonal Statistics is a plugin that you have to install/enable via the plugins menu - otherwise it won't be present in the raster menu...
    – Owen
    Dec 1, 2016 at 13:37

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.

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.