4

I have a raster image that contains a different value and the polygons over the raster's (attached below). Is it possible to calculate the percentage of different cells inside the polygon i.e., 20% cells have x value, 80% have y values. If I use zonal histograms they give me no of cells of the same value i.e., 200 cells of histogram 1. 800 cells of histogram 2 and blah blah .....etc but I want in percentage, not in numbers.

Is it possible to get this percentage in QGIS?

enter image description here

4
  • 2
    Did you try the zonal statistics tool?
    – Ben W
    Commented Jul 9, 2021 at 13:47
  • Yes I tried zonal statistics tool but there in no percentage option in it -Ben W .
    – user_3264
    Commented Jul 9, 2021 at 13:51
  • 3
    It's late evening here & I'm just going to bed, but if noone else answers in the meantime or you don't find a solution, I will try to help more tomorrow.
    – Ben W
    Commented Jul 9, 2021 at 14:13
  • Yes please, waiting for your reply.. -Ben W
    – user_3264
    Commented Jul 9, 2021 at 14:42

1 Answer 1

5

To tool to use for this is Zonal histogram (see documentation). Set the raster layer and the polygon (red hached in the screenshot) as input and it calculates the no. of pixels inside the polygon for each pixel value.

From this, it's easy to count the percentage (e.g. with field calculator) - in the example here:

  • all pixels: 245150 + 427556 = 672706 = 100%
  • green pixels (value 5): 245150/672706*100= 36.4%
  • yellow pixels (value 6): 427556/672706*100= 63.6%

If you have a lot of layers, you can use Zonal histogram in batch mode (click in the lower left of the dialog window). There, click autofill... to get several options like Add Values by Expression… and Add Files by Pattern….

Screenshot: see the attribute table of the output layer: it shows that inside the red polygon, there are 245150 pixels with value=5 (green) and 427556 pixels with value=6 (yellow):

enter image description here

4
  • -Babel thank you so much for great explanation. I have 200 polygons and 30 raster image so it is possible to do direct calculation? Because by doing this manually it will take so much time. Any shortcut?
    – user_3264
    Commented Jul 9, 2021 at 18:28
  • 1
    Use batch mode - see updated answer
    – Babel
    Commented Jul 9, 2021 at 19:31
  • -Babel which updated answer?
    – user_3264
    Commented Jul 10, 2021 at 14:25
  • My answer above was updated - read this part: " If you have a lot of layers, you can..."
    – Babel
    Commented Jul 10, 2021 at 15:01

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.