[GIS] Use gdal to set multiband raster values to no data

gdalgdalwarpnodatanullsetnull

I need to set a 3 band multiband raster (GeoTIff) where band 1 = 0 AND band 2 = 0 AND band 3 = 0 then this is no data (0,0,0 is No Data). Not OR in each band but AND across the 3 bands.

I think it is GDALWARP -dstnodata but am unsure of the syntax.

How do I set the No Data value as 0,0,0 in the header?

Best Answer

The syntax is documented on the manual page https://www.gdal.org/gdalwarp.html as

If more than one value is supplied all values should be quoted to keep them together as a single operating system argument.

On Windows it means double quotation marks, on Linux I guess that single quotes work. So in theory you would do it like this

gdalwarp -of GTiff -dstnodata "0 1 2" global_mosaic_0.png nodata.tif

However, the target format must also support different nodata values for each band and GeoTIFF is not such format.

Creating output file that is 58P x 40L.
Processing global_mosaic_0.png [1/1] : 0
Warning 1: Setting nodata to 1 on band 2, but band 1 has nodata at 0. The TIFFTAG_GDAL_NODATA only support one value per dataset. This value of 1 will be used for all bands on re-opening
Warning 1: Setting nodata to 2 on band 3, but band 1 has nodata at 0. The TIFFTAG_GDAL_NODATA only support one value per dataset. This value of 2 will be used for all bands on re-opening

So you can't do what you want with GeoTIFF because the one and only supported nodata value gets just overwritten and finally all bands use the nodata value of the blue band as nodata.

If you use GDAL based software for reading your data then GDAL virtual raster (VRT) might be the best option. As you can see it can handle band specific nodata.

gdalinfo nodata.vrt
Driver: VRT/Virtual Raster
Files: nodata.vrt
       global_mosaic_0.png
Size is 58, 40
Coordinate System is `'
Origin = (6.346174908474390,38.511570852579098)
Pixel Size = (0.049940902207883,-0.049940902207883)
Corner Coordinates:
Upper Left  (   6.3461749,  38.5115709)
Lower Left  (   6.3461749,  36.5139348)
Upper Right (   9.2427472,  38.5115709)
Lower Right (   9.2427472,  36.5139348)
Center      (   7.7944611,  37.5127528)
Band 1 Block=58x40 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=58x40 Type=Byte, ColorInterp=Green
  NoData Value=1
Band 3 Block=58x40 Type=Byte, ColorInterp=Blue
  NoData Value=2
Related Question