How to set all pixels with value <= 0 to "nodata" in DEM raster?

I didn't find a one-tool solution, but you can first use raster calculator to turn all values below a certain threshold to zero and then use gdal_translate with -a_nodata 0 to turn the 0 into nodata.

enter image description here

enter image description here


It can be done in one step in QGIS in the raster calculator.

In QGIS3, for a raster layer named "x", use the following expression:

(("x">0)*"x") / (("x">0)*1 + ("x"<=0)*0)

This trick maps raster values x>0 into the ratio x/1 = x, and raster values x<=0 into the ratio 0/0 = NaN. This NaN is rendered as FLOAT_MIN (aka -3.402832...e+38) if the raster is 4-byte float.

Strangely this question seems to have been around a while, with (as far as my quick Google search today shows) most respondents saying it can't be done in a single step. It certainly shouldn't need to be hacked like this. The QGIS documentation could be better too.


Here's the GRASS mapcalc expressions to set a range to NULL:

r.mapcalc "dem_corrected = if(dem<=0, null(), dem)"