Skip to content

Commit 6da22cf

Browse files
committed
[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)
1 parent c312921 commit 6da22cf

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 = []
@@ -129,8 +129,17 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
129129
arguments.append('-o')
130130
arguments.append(out)
131131

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

135144
commands = []
136145
if isWindows():

0 commit comments

Comments
 (0)