Skip to content

Commit 1d8ecaf

Browse files
committedMar 21, 2018
[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
1 parent 95fa89e commit 1d8ecaf

File tree

1 file changed

+13
-4
lines changed
  • python/plugins/processing/algs/gdal

1 file changed

+13
-4
lines changed
 

‎python/plugins/processing/algs/gdal/merge.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
QgsProcessingParameterEnum,
3737
QgsProcessingParameterString,
3838
QgsProcessingParameterBoolean,
39-
QgsProcessingParameterRasterDestination)
39+
QgsProcessingParameterRasterDestination,
40+
QgsProcessingUtils)
4041
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
4142
from processing.algs.gdal.GdalUtils import GdalUtils
4243

@@ -105,7 +106,6 @@ def icon(self):
105106
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'merge.png'))
106107

107108
def getConsoleCommands(self, parameters, context, feedback, executing=True):
108-
layers = self.parameterAsLayerList(parameters, self.INPUT, context)
109109
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
110110

111111
arguments = []
@@ -128,8 +128,17 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
128128
arguments.append('-o')
129129
arguments.append(out)
130130

131-
for layer in layers:
132-
arguments.append(layer.source())
131+
# Always write input files to a text file in case there are many of them and the
132+
# length of the command will be longer then allowed in command prompt
133+
listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'mergeInputFiles.txt')
134+
with open(listFile, 'w') as f:
135+
if executing:
136+
layers = []
137+
for l in self.parameterAsLayerList(parameters, self.INPUT, context):
138+
layers.append('"' + l.source() + '"')
139+
f.write('\n'.join(layers))
140+
arguments.append('--optfile')
141+
arguments.append(listFile)
133142

134143
commands = []
135144
if isWindows():

0 commit comments

Comments
 (0)
Please sign in to comment.