3

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 ?

0

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.

# Create the mask
gdal_translate -of GTiff -ot Byte -co NBITS=1 band1.jp2 mask.tif
# Create VRT
gdalbuildvrt -separate bands_merge.vrt band1.jp2 band2.jp2 band3.jp2 band4.jp2 mask.tif
# Create the tif
gdal_translate -of GTiff -ot Byte -b 1 -b 2 -b 3 -b 4 -mask 5 -scale 0 4095 0 255 bands_merge.vrt bands_merge.tif
# Remove the temporary files
rm bands_merge.vrt mask.tif

Unfortunately QGIS cannot handle GeoTiff mask files yet, but I need it for GeoServer.

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.