22

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?

underdark
  • 84,148
  • 21
  • 231
  • 413
Loren
  • 323
  • 1
  • 2
  • 4

2 Answers2

30

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

SaultDon
  • 10,389
  • 1
  • 43
  • 78
  • 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:
     What can I be missing? I changed the units to 'c'.
    
    – SoS Feb 11 '16 at 11:07
  • @SoS It's difficult to guess without more info or a sample data set to test on. – SaultDon Feb 11 '16 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? – MonsterMushroom Sep 21 '17 at 09: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 Sep 21 '17 at 17:36
11

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

nickbearman
  • 141
  • 1
  • 4
  • Also, if you want to extract this data for a specific polygon (or series of polygons) the Zonal Histogram tool does this. – nickbearman Nov 10 '21 at 08:43