I've got 136 GeoTIFF files that I'd like to merge using a Python program and gdal_merge.py on Windows but the command line max character limit is 8191 and concatenating all the file names goes over that limit. I could rename all the files to something much shorter as a potential work-around but when I attempt to merge the files interactively in QGIS it appears to be reading all the file names from an input/config file of sorts. I'm wondering if there's a way I can manage to do something similar with gdal_merge.py
Trying to use gdal_merge.bat vs. gdal_merge.py errors out - likely because I have more than one Python environment on my system, not just the Python environment that is included with QGIS/ISGeo4W.
My code:
import os
import sys
import subprocess
import glob
print('Merging the DEMM files...')
# Find all files with a .tif extension in the subdirectory geotif_source_files
input_files = glob.glob("./geotif_source_files/*.tif")
# Define the output file
output_file = "./geotif_source_files/geotiff.tiff"
input_string = " ".join(input_files)
# Build the command to run gdal_merge with -co NUM_THREADS=1 option
cmd = f"gdal_merge.py -o {output_file} -co {input_string}"
# Run the command
subprocess.run(cmd, shell=True, check=True)
The error I get:
Merging the DEMM files... The command line is too long.
The GDAL command from interactive use in QGIS:
gdal_merge.bat -ot Float32 -of GTiff -o C:/Users/MattReidy/AppData/Local/Temp/processing_LEZXJS/ddc6540bf65e4dca9b5df2ed74b81c93/OUTPUT.tif --optfile C:/Users/MattReidy/AppData/Local/Temp/processing_LEZXJS/24dd8e3280d140eb9adb885b37cb197d/mergeInputFiles.txt {SNIP}
--optfile
from the command line why not to try the same with Python?