# Calculation proportion of pixels from 2 raster in qgis

I am new to Qgis and also R, so I am struggling a bit... I have two raster layers of same resolution that perfectly align. One raster is DEM, and the other one is a binary forest map (forest = 1 , no forest = NA). All I have to do is to calculate the proportion of forest per elevation. Like, (how many pixels of forest fall in elevations between 0-50) divided by the (total number of pixels in the DEM that have the value between 0-50). How can I calculate this proportion (forest per elevation) in a automatic way?

• You didn't ask a question? If you need help with something: Show what you already tried, show what you want to achieve, show a test case that illustrates your problem. – Senshi May 29 '17 at 11:57
• I juts posted the question. Sorry for that. I have tried using R, but my results were not correct... – TFA May 29 '17 at 12:18
• what do you mean by "in an automatic way"? do you want to process data multiple times and build a python tool? or rather by using available tools step-by-step? – dru87 May 29 '17 at 14:18

I think SAGA `Zonal raster statistics` is the tool you are looking for.
1. Reclassify your DEM to zones (0~50m -> 1, 50~100m -> 2, ...) by `Processing Toolbox | SAGA | Raster tools | Reclassify values (simple)` which gives you a discrete raster
2. Calculate zonal statistics between 2 rasters by `Processing Toolbox | SAGA | Geostatistics | Zonal raster statistics`. Assign your reclassified DEM to `Zone Grids` and your forest [0,1] raster to `Categorical Grids`. Then hit [Run].
QGIS(SAGA) will return a table `Zonal Statistics` which shows combinations of DEM zones, forest zones, and `Count UCU` which is the cell counts. After that, it will be easy to turn it into Area and/or calculate their proportion.