From 1d8ecaf79ae48662fa546f0cd3b3a6a0d5a44f4c Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 21 Mar 2018 13:43:41 +1000 Subject: [PATCH] [processing][gdal] Use a text file for input file list to gdal_merge Otherwise command fails when attempting to merge many rasters due to length of command line. Now the algorithm uses the same approach as buildvrt and creates a text file containing the names of the rasters and then passes this to the gdal_merge command Fixes gdal merge algorithm fails with many input files --- python/plugins/processing/algs/gdal/merge.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/python/plugins/processing/algs/gdal/merge.py b/python/plugins/processing/algs/gdal/merge.py index 970b74f32fb..39de11572dc 100644 --- a/python/plugins/processing/algs/gdal/merge.py +++ b/python/plugins/processing/algs/gdal/merge.py @@ -36,7 +36,8 @@ from qgis.core import (QgsRasterFileWriter, QgsProcessingParameterEnum, QgsProcessingParameterString, QgsProcessingParameterBoolean, - QgsProcessingParameterRasterDestination) + QgsProcessingParameterRasterDestination, + QgsProcessingUtils) from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm from processing.algs.gdal.GdalUtils import GdalUtils @@ -105,7 +106,6 @@ class merge(GdalAlgorithm): return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'merge.png')) def getConsoleCommands(self, parameters, context, feedback, executing=True): - layers = self.parameterAsLayerList(parameters, self.INPUT, context) out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context) arguments = [] @@ -128,8 +128,17 @@ class merge(GdalAlgorithm): arguments.append('-o') arguments.append(out) - for layer in layers: - arguments.append(layer.source()) + # Always write input files to a text file in case there are many of them and the + # length of the command will be longer then allowed in command prompt + listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'mergeInputFiles.txt') + with open(listFile, 'w') as f: + if executing: + layers = [] + for l in self.parameterAsLayerList(parameters, self.INPUT, context): + layers.append('"' + l.source() + '"') + f.write('\n'.join(layers)) + arguments.append('--optfile') + arguments.append(listFile) commands = [] if isWindows():