23

How does one go about obtaining a summary of the counts of cell values within a raster in QGIS? I know QGIS is doing this, in order to provide the summary statistics and histogram in the properties of the layer. I do not need the summary statistics nor a histogram image, but rather counts of individual values.

It looks like there was at one point an attempt to add a special feature for this (Feature #4321, http://hub.qgis.org/issues/4321).

Am I missing something really obvious?

2 Answers 2

31

r.report from GRASS in the Processing toolbox lets you get a count if you set the Units to c.

getting count of raster values with r.report in qgis processing with grass

Results in (this includes NoData represented as int 0):

+-----------------------------------------------------------------------------+ 
| Category Information | cell| 
| #|description | count| 
|-----------------------------------------------------------------------------| 
|  0| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |147600504| 
| 10| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 46945| 
| 20| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 34371070| 
| 30| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 38494918| 
| 34| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 3542216| 
| 50| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 37053253| 
| 80| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 24184039| 
|110| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 8622768| 
|120| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 4279049| 
|122| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 3126056| 
|131| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 786| 
|133| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 452832| 
|136| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 29765| 
|137| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 317| 
|140| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 1725632| 
|147| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 130470| 
|153| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 1153795| 
|162| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 125372| 
|167| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 176| 
|175| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 36144| 
|181| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 160465| 
|188| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 50| 
|194| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 4854| 
|210| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |257580769| 
|220| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 35232865| 
|230| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | 6891335| 
|-----------------------------------------------------------------------------| 
|TOTAL |604846445| 
+-----------------------------------------------------------------------------+

Taken from: example raster with paletted band

4
  • 1
    @SaultDon, I tried to use r.report on my rasterdata to count individual values, like in your example. But, I only got a summary - number of pixels,min/max etc. The report only contains: <pre></pre> What can I be missing? I changed the units to 'c'.
    – SoS
    Commented Feb 11, 2016 at 11:07
  • @SoS It's difficult to guess without more info or a sample data set to test on.
    – SaultDon
    Commented Feb 11, 2016 at 15:35
  • This is great, but is there a solution that produces a plainer file- either a csv or a dbf, so that the user can Import it directly to Excel or use it as Input for another tool? Commented Sep 21, 2017 at 9:59
  • 2
    @MonsterMushroom use r.stats - it can output in a computer friendly format ready for spreadsheets, in fact r.report uses r.stats in the background
    – SaultDon
    Commented Sep 21, 2017 at 17:36
12

Just came across this now (2021) and QGIS offers this as "Raster layer unique values report" (I am using QGIS 3.16). It is a simpler form than the GRASS module, and will output a report and a table.

enter image description here

1
  • Also, if you want to extract this data for a specific polygon (or series of polygons) the Zonal Histogram tool does this. Commented Nov 10, 2021 at 8:43

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.