3

I am looking for a way to specify a different nodata value for each band of a raster with rasterio.

I tried:

  • using the nodata key in the profile with a tuple or list of nodata values,
  • setting the nodatavals attribute,

but none of these methods worked.

In case it is possible at all to specify different nodata values using rasterio, how can it be done?

1
  • As per the Tour there should be only one question asked per question.
    – PolyGeo
    Sep 7, 2018 at 6:00

2 Answers 2

2

I'm not sure about rasterio, but you can use GDAL's gdalwarp function to overwrite the nodata values of each band in a multiband raster by placing the values inside quotes (single on linux, double on windows), for example this will rewrite the values of a five band raster to 0 for band 1, 1 for band 2, and so on:

gdalwarp -of GTiff -dstnodata "0 1 2 3 4" input.tif output.tif

However, this will give you warnings each time you use a different value for another band because the geotiff file format does not support different nodata values across the bands, from the gdal documentation:

"GDAL stores band nodata value in the non standard TIFFTAG_GDAL_NODATA ASCII tag (code 42113) for files created with the default profile GDALGeoTIFF. Note that all bands must use the same nodata value."

So, if you change the output format to another raster file format that can deal with the different nodata values such as GDAL virtual raster, you should be fine, such as:

gdalwarp -of VRT -dstnodata "0 1 2 3 4" input.tif output.vrt
1

Afaik you cannot write different nodata values with rasterio. From the documentation (emphasis mine):

nodata

The dataset’s single nodata value

Notes

May be set.

[...]

nodatavals

Nodata values for each band

Notes

This may not be set.

I believe this is due to the fact, as pointed out in other answers, that the GeoTIFF Standard only allows one nodata value for all bands.

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.