In a QGIS Python script I'm trying to produce raster 0/1 mask with use of procesing package. The pipeline looks like that:

  • Create an image of index (NDSI if that matters) with numpy arrays from a satellite image,

  • Do thresholding with saga:rastercalculator from processing to obtain 0/1 mask where the index exceeds a value. I can force data type on this stage, so I set it to 1 bit file. Because of some peculiarity of format and QGIS (as I understand it), assigning 0 in a saga formula results with NaNs in the output file,

  • Convert nans to 0 with saga:reclassifygridvalues.

Here my problem begins. There's no flag for datatype in reclassification, so it seems, that I get Float32 by default. And the values are 0 and 0.999, which is a problem, as I have to subtract them later on from another mask (raster calculation again) and instead of clean 0/1 cut I get a few shades of gray.

How can I make processing algorithms return 0/1 images consistently?


Make a model with your steps and in the end of it insert a GDAl translate. Inside this GDAL translate set "Additional creation parameters" to "-co NBITS=1" (1 bit image) and set "Output raster type" to "Byte". That's it!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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