[GIS] How to extract pixel value counts from a raster in QGIS, in different polygons

qgisrasterzonal statistics

I have a raster with values from 1 – 12 (0 is no data) that has the extent of Sweden. These values represent factors, or categories rather then a numeric value. I would like to know how many of each factor there is in each of Swedens regions, which i have in a polygon layer. Can this be done without chopping up my raster in to 21 (or 12) peices? Zonal statistics will give me some statistics based on a polygon layer, but not the count of different values. r.stat and r.report will give me the output that i'm interested in but only across the entire raster. Is there a tool that will do both at the same time? Give me the count of each value in each region?

What i'm trying to do is exactly what is asked about here but not for the entire raster.

Best Answer

you may use the grass plugin within qgis, or go directly to grass; there's the module called r.report which takes two rasters (just convert your shapefile with v.to.rast taking care of the resolution; the sample syntax for reporting on your data would be:

r.report map=categories@PERMANENT,polygons@PERMANENT -c 

where flag -c means pixel count; other module, r.stats gives you a more usable csv like format of r.report

Related Question