I have four 12 – bit Bands in single images which I want to merge to one 8bit Geotiff using GDAL (GDAL 2.1.3)
First thing I do is building a VRT:
gdalbuildvrt -srcnodata 0 -separate bands_merge.vrt band1.jp2 band2.jp2 band3.jp2 band4.jp2
Then with gdal_translate I create the tif.
gdal_translate -of GTiff -ot Byte -scale 0 4095 0 255 bands_merge.vrt bands_merge.tif
That works just perfect. But as the nodata value of the source files is 0 and all values will be scaled, original values below ~8 will become 0.
My first thought on how to fix that issue was using -scale 1 4095 1 255
but that does not work because it assigns 1 to all previous 0 values.
How can I make sure that the -scale 0 4095 0 255
does not create false nodata values ?
Best Answer
Instead of using NODATA, in this case it is better to use a mask:
As the NODATA value is 0, I can easily create the mask with
-co NBITS=1
as everything above a value of 1 will be cut to 1.Unfortunately QGIS cannot handle GeoTiff mask files yet, but I need it for GeoServer.