5

How can I process an existing raster in QGIS to get a mask showing all nodata cells. I want a raster with value 1 in each cell that has valid data and 0 in each cell with a nodata value.

I intend to use the outputs in the raster calculator so something like"my_raster_layer" != nodata in the raster calculator would be ideal but I can't see a way of doing it.

1
  • We couldn't come up with a one-step solution with the raster calculator but there are two-step solutions in QGIS or a one-step solution with a simple GDAL script. Mar 7 at 4:33

2 Answers 2

5

First, you need to know what value is assigned to the nodata tag. You can see it in the Information tab of the layer properties:

Information tab of layer properties

The pixels currently have that value (-1.25 in my case). I assume in my answer that in your case the nodata tag is also associated with a numeric value (if, instead, the pixels had a nan value, the procedure is not the same).

Beyond the fact that the pixels store a numerical value, no process will take them into account (and they will be rendered transparent on the map) because that value is assigned to nodata, and that assignment is what must be removed.

To remove the nodata tag, you can use the Translate (convert format) process, including the -a_nodata none additional command line parameter:

Translate (convert format) process dialog

You can find in the documentation of the gdal_translate application (which is the command that runs under the hood), the explanation of the additional parameter: https://gdal.org/programs/gdal_translate.html#cmdoption-gdal_translate-a_nodata

Once the nodata tag is removed, the mask is created with a simple conditional expression in the raster calculator that tests if the value of each pixel is not equal to -1.25, in which case it will return 1, and will return 0 if the condition is false:

"Converted@1" != -1.25

Raster calculator process dialog

2
  • Thanks for this, I'll give it a try. I was hoping I could generate the masks and then process them all in one step with QGIS's raster calculator. I might need to write a gdal script if I want it to be one step. Thanks very much for your answer. I'll accept it tonight if no-one has got back with a way of doing it in raster calculator. Mar 6 at 3:28
  • There are other ways, like use the Fill NoData cells process, so your can convert all to 1 in the raster calculator and then fill nodata with 0. I'm used to removing nodata from the terminal, and the Translate process is the equivalent in QGIS. Mar 6 at 3:58
4
  1. Run Raster Calculator with an expression like raster@1 >= 0 or raster@1 <=0 - this will produce on output with all not empty pixels set to 1

  2. Run Menu Processing > Toolbox > Fill NoData cells with the output from step 1 as raster input and set Fill value to 0

3
  • 1
    Thanks for this. I also used my_raster@1 / my_raster@1 for the first step. Your suggestions would be better incase there were any 0s in the original dataset to avoid NANs. Mar 7 at 4:28
  • 2
    This approach could also be implemented as a gdal script using gdal_calc.py and gdal_fillnodata.py Mar 7 at 4:31
  • 1
    "raster@1" * 0 + 1 models "make it 1 no matter how much it was before". If you are going to use the GDAL calculator, there is also the -hideNoData option (gdal.org/programs/gdal_calc.html#cmdoption-hideNoData) to apply a condition, as long as you know in advance the value assigned to that tag. Mar 7 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.