I have a raster layer and a polygon layer. The raster layer has values between 0 and 600. I want to calculate the average of all values above 0 within each polygon.

The only solution I came up with so far is to clip the part of the polygons that overlaps with a non-zero (positive) raster value, and then calculate the zonal statistic only for the clipped part. Since the data is quite large, this method is not practical.

Is there a faster way, e.g. set the grid values = 0 to NODATA (if this is possible) and then calculate the average value? Unfortunately I have little experience with raster data.

1 Answer 1


Convert raster pixels with value 0 to nodata cells using Raster Calculator. Divide the raster's value by itself: a division by 0 will return nodata.

For all other pixels, to keep the value as it is, divide the pixel value by itself and multiply it again by itself.

  • 5 / 5 * 5 -> 5
  • 0 / 0 * 0 -> NODATA

Use this expression in raster calculator, where raster is the name of your raster layer:

"raster@1" / "raster@1" * "raster@1"

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.