[processing][gdal] Move method to write input files to text file to GdalUtils

This commit is contained in:
Nyall Dawson 2018-03-21 21:05:31 +10:00
parent b17feaaa97
commit 49d585ef17
3 changed files with 22 additions and 20 deletions

View File

@ -397,3 +397,17 @@ class GdalUtils:
for p in parts:
options.extend(['-co', p])
return options
@staticmethod
def writeLayerParameterToTextFile(filename, alg, parameters, parameter_name, context, quote=True, executing=False):
listFile = os.path.join(QgsProcessingUtils.tempFolder(), filename)
with open(listFile, 'w') as f:
if executing:
layers = []
for l in alg.parameterAsLayerList(parameters, parameter_name, context):
if quote:
layers.append('"' + l.source() + '"')
else:
layers.append(l.source())
f.write('\n'.join(layers))
return listFile

View File

@ -113,15 +113,9 @@ class buildvrt(GdalAlgorithm):
arguments.append('-allow_projection_difference')
# 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(), 'buildvrtInputFiles.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))
list_file = GdalUtils.writeLayerParameterToTextFile(filename='buildvrtInputFiles.txt', alg=self, parameters=parameters, parameter_name=self.INPUT, context=context, executing=executing, quote=False)
arguments.append('-input_file_list')
arguments.append(listFile)
arguments.append(list_file)
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append(out)

View File

@ -83,10 +83,10 @@ class merge(GdalAlgorithm):
self.addParameter(nodata_param)
nodata_out_param = QgsProcessingParameterNumber(self.NODATA_OUTPUT,
self.tr('Assign specified "nodata" value to output'),
type=QgsProcessingParameterNumber.Integer,
defaultValue=None,
optional=True)
self.tr('Assign specified "nodata" value to output'),
type=QgsProcessingParameterNumber.Integer,
defaultValue=None,
optional=True)
nodata_out_param.setFlags(nodata_out_param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(nodata_out_param)
@ -159,15 +159,9 @@ class merge(GdalAlgorithm):
# 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))
list_file = GdalUtils.writeLayerParameterToTextFile(filename='mergeInputFiles.txt', alg=self, parameters=parameters, parameter_name=self.INPUT, context=context, quote=True, executing=executing)
arguments.append('--optfile')
arguments.append(listFile)
arguments.append(list_file)
commands = []
if isWindows():