I have 1,000 very large single-band rasters and many of them have nodata values. I want to take those nodata values and create a binary map to visualize all of the areas where I do not have data coverage.

Even after researching for quite awhile I don't really understand alpha bands/masks, where they exist, how to examine them. I thought I could just add an alpha band in GDAL but when I export the band I just get a raster full of 255s (checking with np.unique(tif))

# Adds alpha band
gdalbuildvrt out_vrt src_dir/*.tif -b 1 -vrtnodata -9999 -addalpha  

# Deletes the first band, leaving just the alpha
gdal_translate in_vrt out_tif -b 2

My next idea was to use gdal_calc to create a binary tif where nodata=1 and data=0

# Both of these attempts left me with just a raster of zeroes
# Attempt #1
gdal_calc.py -A src_tif.tif --A_band=1 --calc="(A<=-9999)" --outfile out_tif.tif --co compress=Deflate --type=Byte

# Attempt #2
gdal_calc.py -A src_tif.tif --A_band=1 --calc="(A*0)" --NoDataValue=1 --outfile out_tif.tif --co compress=Deflate --type=Byte

Any tips? This seems like it should be so simple, it's driving me crazy

  • Do you have -9999 as nodata value in all images?
    – user30184
    Commented May 25, 2021 at 8:19
  • 1
    Try gdal_calc -A src.tif --outfile=out.tif --type=Byte --calc="255*(A>-9999)". It should create you an 8-bit tiff where white presents the data and black means nodata. Please test.
    – user30184
    Commented May 25, 2021 at 12:44
  • This is brilliant, thank you very much!
    – la_leche
    Commented May 25, 2021 at 17:57

1 Answer 1


If the aim is just to divide the image into two values, one for data and another for nodata and exact values are not so important, a short gdal_calc https://gdal.org/programs/gdal_calc.html command can do it.

gdal_calc -A src.tif --outfile=out.tif --type=Byte --calc="255*(A>-9999)"

The command creates a single band, 8-bit GeoTIFF because there is no need to keep the higher bit depth. Adding a creation option for compressed output --creation-option=compress=deflate will save a lot of disk space.

The calc expression is changing all pixel values that are greater than -9999 into 255 that is pure white. This stands for data. The remaining pixels (-9999 or less) are getting zero as pixel value because that seems to be the default for gdal_calc.py. The result is a binary image that can be checked visually unlike if it was created to use pixel values 0 and 1 which both appear visually black. If vector data are needed the image can be converted into vector with gdal_polygonize https://gdal.org/programs/gdal_polygonize.html.

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.