Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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

(cherry-picked from 1d8ecaf)
  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent c312921 commit 6da22cf
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions python/plugins/processing/algs/gdal/merge.py
Expand Up @@ -36,7 +36,8 @@
QgsProcessingParameterEnum,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterRasterDestination)
QgsProcessingParameterRasterDestination,
QgsProcessingUtils)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

Expand Down Expand Up @@ -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 = []
Expand All @@ -129,8 +129,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():
Expand Down

0 comments on commit 6da22cf

Please sign in to comment.