16

I would like to use GDAL (under Windows; OSGeo4W Shell) to merge all GeoTIFFs from one directory into a new GeoTIFF. I have tried to address them by writing c:\data\....\*.tif which however does not seem to work, using the Windows environment.

I am looking for the most practicable way to perform this operation; if possible completely within the OSGeo4W Shell. Addressing every single mosaic part name 'by hand' in the Shell is not an option.

laser
  • 966
  • 7
  • 13
Arne
  • 815
  • 2
  • 8
  • 22

2 Answers2

37

You could create a virtual mosaic from all Tiff files:

gdalbuildvrt mosaic.vrt c:\data\....\*.tif

and convert it afterwards:

gdal_translate -of GTiff -co "COMPRESS=JPEG" -co "PHOTOMETRIC=YCBCR" -co "TILED=YES" mosaic.vrt mosaic.tif

Keep an eye on all the GDAL creation parameters to compress your mosaic and use gdaladdo to add overviews.

More info here: GeoTiff Compression for Dummies - Paul Ramsey

christoph
  • 5,605
  • 1
  • 20
  • 34
  • 5
    Just a small hint: increasing GDAL_CACHEMAX could bring a performance boost.

    On Windows: set GDAL_CACHE_MAX=128

    or gdal_translate --config GDAL_CACHE_MAX 128 -co "COMPRESS=JPEG" ...

    – christoph Mar 05 '17 at 11:17
  • Does the same command work for Linux? – wondim May 06 '19 at 18:49
  • E:\>gdal_translate -a_srs EPSG:4326 -of GTiff output.vrt mosaic10.tif Input file size is 2818, 1267 0Warning 6: GDAL_RASTERIO_RESAMPLING = {nearest} not supported Warning 6: GDAL_RASTERIO_RESAMPLING = {nearest} not supported ERROR 4: E:\GHRC\20200203\202003401_20200203040951_20200203104318_20200203115400_frames\FRAME0001.tif: No such file or directory I am getting error like this. How to fix it? – Gokul Anand Feb 13 '20 at 09:56
  • @wondim the same command works across all platforms – shubhamgoel27 May 04 '20 at 11:34
  • Here is how the same thing can be done in Python: https://stackoverflow.com/a/66663626/2842067 – Alexey Mar 23 '23 at 16:56
2

If you are willing to use python, then you can use wildcards to select files with the glob module, and then you can execute the command with os.system.

import glob
import os

file_list = glob.glob("c:\data\....\*.tif")

files_string = " ".join(file_list)

command = "gdal_merge.py -o output.tif -of gtiff " + files_string

os.system(command)
user6072577
  • 1,572
  • 2
  • 11
  • 23
  • 1
    Important to point out gdal_merge loads all tiffs in memory, so probably wouldn't be a great choice while working with big rasters – shubhamgoel27 May 10 '20 at 07:04