2

I have two geoTIFFS. The files cover the exact same area, both have 1 band(Block = 256x256) 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?

user2856
  • 65,736
  • 6
  • 115
  • 196

2 Answers2

1

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.

MappaGnosis
  • 33,857
  • 2
  • 66
  • 129
1

You can also do this with gdalbuildvrt (using the -separate flag to treat inputs as separate bands instead of mosaicing them) and gdal_translate

In one line with no intermediate file (by writing to stdout and piping to stdin):

gdalbuildvrt -separate /vsistdout/ first.tif second.tif|gdal_translate /vsistdin/ merged.tif

If you don't want two copies of the data, you could always just write out the VRT, it's just a small text (XML) file that QGIS understands (and ArcGIS and anything else that uses GDAL under the hood).

gdalbuildvrt -separate merged.vrt first.tif second.tif
user2856
  • 65,736
  • 6
  • 115
  • 196