[GIS] Merge tiffs into a single multi banded tiff

gdal-mergegdalwarpgeotiff-tiffraster

I have two geoTIFFS. The files cover the exact same area, both have 1 band(Block = 256×256) and both have a no data value of 0. They also have the same coordinate system.

I want to merge the files into a single tiff with two bands.

I tried this command:

gdal_merge.bat -n 0 -a_nodata 0 -separate -of GTiff -o merged.tif first.tif second.tif

But I kept getting a Memory error. After researching online, I then used the same command with the -createonly flag:

gdal_merge.bat -createonly -n 0 -a_nodata 0 -separate -of GTiff -o merged.tif first.tif second.tif

Followed By:

gdalwarp --config GDAL_CACHEMAX 500 -wm 500 merged.tiff mergedFINAL.tiff

However opening mergedFINAL.tiff in qgis just yields a blank screen. Can anyone see something Im doing wrong and offer me some advice?

Best Answer

You are getting a blank image because you are not copying any data into the final output. The creatonly flag does just that. It creates a blank image of the right size (see the documentation). So your inputs in the warp operation should not be the blank output of the -creatonly operation (as that will obviously be blank as you discovered).

See here for an example of using gdal_warp for merging. An alternative to this approach would be to make a VRT and then warp that.

gdal_merge loads the whole raster(s) into memory for speed whereas warp uses tiling to do the operation piecemeal (so less memory overhead). A VRT is just a virtual raster - an xml representation of the result that you can 'solidify' into a real raster (if you need to) using translate or warp.