QGIS Raster Nodata Value – Setting Pixels with Value <= 0 to 'Nodata'

grasspythonqgissaga

I have a DEM raster with pixel values between about 3000 and -0.0003.
I need to set all pixel with a value of 0 and smaler (<=0) to "nodata" (for later raster calculations and to reduce the filesize).

It seems for me that this is a task for the raster calculator (set value of pixels <=0 to "nodata") or a reclassification (set value of pixels <=0 to "nodata" and keep all other values) but I dont know how.

Best Answer

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.