Skip to content
Permalink
Browse files

[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
  • Loading branch information
nyalldawson committed Mar 21, 2018
1 parent 95fa89e commit 1d8ecaf79ae48662fa546f0cd3b3a6a0d5a44f4c
Showing with 13 additions and 4 deletions.
  1. +13 −4 python/plugins/processing/algs/gdal/merge.py
@@ -36,7 +36,8 @@
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
QgsProcessingParameterRasterDestination,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

@@ -105,7 +106,6 @@ def icon(self):
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 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
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():

0 comments on commit 1d8ecaf

Please sign in to comment.
You can’t perform that action at this time.