GDAL – Merging Multiple GeoTIFF’s with gdal_merge Creates Solid Gray GeoTIFF

gdalgdal-mergegeotiff-tiff

I have these two GeoTIFF's:

https://terrafrost.com/gdal/1.tif

https://terrafrost.com/gdal/2.tif

I'm trying to merge them by doing gdal_merge -o result.tif 1-dark.tif 2-dark.tif but when I do the result is just a solid gray image:

https://terrafrost.com/gdal/result.tif

Is there something I'm doing wrong? Do I need to do some pre-processing on the input GeoTIFF's? Maybe another tool would be better?

Best Answer

Gdalbuildvrt it better than gdal_merge.py for most use cases. However, your issue is easy to fix. Gdalinfo shows that source images are paletted, and both are having the same color table

 0: 253,253,253,0
 1: 17,204,142,255
 2: 0,0,0,255
 3: 0,0,0,255
 4: 0,0,0,255
...

From the documentation https://gdal.org/programs/gdal_merge.html:

-pct

Grab a pseudo-color table from the first input image, and use it for the output. Merging pseudo-colored images this way assumes that all input files use the same color table.

So the gdal_merge command to use is

gdal_merge -pct -o result.tif 1.tif 2.tif

How to do the same with gdalbuildvrt:

gdalbuildvrt test.vrt 1.tif 2.tif
gdal_translate test.vrt result.tif

Your original result.tif image is not solid gday. Running gdalinfo result.tif -stats shows that is has values 0 and 1 like the original paletted images have. But because the color table was not copied the image turned into grayscale with totally black and almost black pixels that eye cannot separate.

STATISTICS_MAXIMUM=1
STATISTICS_MEAN=0.012810842015339
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=0.11245765577406
STATISTICS_VALID_PERCENT=100
Related Question