4

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?

2
  • Did you ever find a resolution to this? Using vrt didn't help me either. I have 0 0 0 0 across 4 bands which should equal noData. However, pixels with 0 in Band 1, e.g. 0 153 216 255 will also end up in noData which I don't want to be the case. So either I am able to recalculate that 0 to something else or as you describe above something gives me the option to use AND instead of OR. Commented Aug 26, 2020 at 10:54
  • I ended up using gdal_calc with something like this gdal_calc.py -A test_tif --A_band=1 -B test_tif --B_band=2 -C test_tif --C_band=3 -D test_tif --D_band=4 --outfile result_tif --calc="(A==0)*(B==0)*(C==0)*(D==0) + (A==0)*(B==153)*(C==216)*(D==255)*2 + (A==250)*(B==169)*(C==74)*(D==255)*3" to make sure noData can be considered (ideas from spatialthoughts.com/2019/12/28/gdal-calc) Commented Aug 26, 2020 at 11:11

1 Answer 1

6

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

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.