Skip to content
Permalink
Browse files

More append thinning

Along with some other loop optimizations
  • Loading branch information
dericke authored and nyalldawson committed Feb 20, 2021
1 parent 0e63799 commit ef6799fad41e50c03f2a1664c443e6f32fcaee05
@@ -254,9 +254,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
precision = self.parameterAsBoolean(parameters, self.PRECISION, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)

arguments = []
arguments.append('-progress')
arguments.append('--config PG_USE_COPY YES')
arguments = [
'-progress',
'--config PG_USE_COPY YES'
]
if len(shapeEncoding) > 0:
arguments.append('--config')
arguments.append('SHAPE_ENCODING')
@@ -331,14 +332,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if len(options) > 0:
arguments.append(options)

commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
return ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

return commands
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

def commandName(self):
return 'ogr2ogr'
@@ -245,9 +245,10 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
precision = self.parameterAsBoolean(parameters, self.PRECISION, context)
options = self.parameterAsString(parameters, self.OPTIONS, context)

arguments = []
arguments.append('-progress')
arguments.append('--config PG_USE_COPY YES')
arguments = [
'-progress',
'--config PG_USE_COPY YES'
]
if shapeEncoding:
arguments.append('--config')
arguments.append('SHAPE_ENCODING')
@@ -324,14 +325,11 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):
if len(options) > 0:
arguments.append(options)

commands = []
if isWindows():
commands = ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
return ['cmd.exe', '/C ', 'ogr2ogr.exe',
GdalUtils.escapeAndJoin(arguments)]
else:
commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

return commands
return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]

def commandName(self):
return "ogr2ogr"
@@ -77,22 +77,21 @@ def commandName(self):
return 'pct2rgb'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = []
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inLayer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments.append(inLayer.source())

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
arguments = [
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
'-b',
str(self.parameterAsInt(parameters, self.BAND, context)),
]

if self.parameterAsBoolean(parameters, self.RGBA, context):
arguments.append('-rgba')
@@ -77,18 +77,19 @@ def commandName(self):
def getConsoleCommands(self, parameters, context, feedback, executing=True):
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)

raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if raster is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments = [
'-n',
str(self.parameterAsInt(parameters, self.NCOLORS, context)),
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1])
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
raster.source(),
out
]
raster = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if raster is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments.append(raster.source())
arguments.append(out)

if isWindows():
commands = ["python3", "-m", self.commandName()]
@@ -86,22 +86,22 @@ def commandName(self):
return 'gdaldem'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = ['roughness']
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inLayer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments.append(inLayer.source())

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
arguments = [
'roughness',
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
'-b',
str(self.parameterAsInt(parameters, self.BAND, context))
]

if self.parameterAsBoolean(parameters, self.COMPUTE_EDGES, context):
arguments.append('-compute_edges')
@@ -108,25 +108,24 @@ def commandName(self):
return 'gdaldem'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = ['slope']
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inLayer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments.append(inLayer.source())

out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]))

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))

arguments.append('-s')
arguments.append(str(self.parameterAsDouble(parameters, self.SCALE, context)))
arguments = [
'slope',
inLayer.source(),
out,
'-of',
QgsRasterFileWriter.driverForExtension(os.path.splitext(out)[1]),
'-b',
str(self.parameterAsInt(parameters, self.BAND, context)),
'-s',
str(self.parameterAsDouble(parameters, self.SCALE, context)),
]

if self.parameterAsBoolean(parameters, self.AS_PERCENT, context):
arguments.append('-p')
@@ -84,18 +84,20 @@ def commandName(self):
return 'gdaldem'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
arguments = ['TRI']
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inLayer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))

arguments.append(inLayer.source())
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, out)
arguments.append(out)

arguments.append('-b')
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))
arguments = [
'TRI',
inLayer.source(),
out,
'-b',
str(self.parameterAsInt(parameters, self.BAND, context)),
]

if self.parameterAsBoolean(parameters, self.COMPUTE_EDGES, context):
arguments.append('-compute_edges')
@@ -423,8 +423,7 @@ def processAlgorithm(self, original_parameters, context, feedback):
getattr(self, fullName)(parameters, context, feedback)

# Run GRASS
loglines = []
loglines.append(self.tr('GRASS GIS 7 execution commands'))
loglines = [self.tr('GRASS GIS 7 execution commands')]
for line in self.commands:
feedback.pushCommandInfo(line)
loglines.append(line)
@@ -790,9 +789,7 @@ def exportRasterLayer(self, grassName, fileName,
:param createOpt: creation options for format.
:param metaOpt: metadata options for export.
"""
if not createOpt:
if outFormat in Grass7Utils.GRASS_RASTER_FORMATS_CREATEOPTS:
createOpt = Grass7Utils.GRASS_RASTER_FORMATS_CREATEOPTS[outFormat]
createOpt = createOpt or Grass7Utils.GRASS_RASTER_FORMATS_CREATEOPTS.get(outFormat)

for cmd in [self.commands, self.outputCommands]:
# Adjust region to layer before exporting
@@ -123,8 +123,7 @@ def _expressionValues(self, text):
src = QgsProcessingModelChildParameterSource.fromExpressionText(text)

# add parameters currently used by the expression
expression_values = []
expression_values.append(src)
expression_values = [src]

for k, v in model_params:
if k in variables:
@@ -157,7 +157,7 @@ def defineCharacteristicsFromFile(self):
line = lines.readline().strip('\n').strip()

def processAlgorithm(self, parameters, context, feedback):
commands = list()
commands = []
self.exportedLayers = {}

self.preProcessInputs()
@@ -230,9 +230,7 @@ def processAlgorithm(self, parameters, context, feedback):
self.exportedLayers[param.name()] = files
else:
for layer in layers:
temp_params = {}
temp_params[param.name()] = layer

temp_params = {param.name(): layer}
if not crs:
source = self.parameterAsSource(temp_params, param.name(), context)
if source is None:
@@ -256,7 +254,7 @@ def processAlgorithm(self, parameters, context, feedback):
command += ' ' + ' '.join(self.hardcoded_strings)

for param in self.parameterDefinitions():
if not param.name() in parameters or parameters[param.name()] is None:
if param.name() not in parameters or parameters[param.name()] is None:
continue
if param.isDestination():
continue
@@ -274,7 +272,7 @@ def processAlgorithm(self, parameters, context, feedback):
elif isinstance(param, QgsProcessingParameterMatrix):
tempTableFile = getTempFilename('txt')
with open(tempTableFile, 'w') as f:
f.write('\t'.join([col for col in param.headers()]) + '\n')
f.write('\t'.join(param.headers()) + '\n')
values = self.parameterAsMatrix(parameters, param.name(), context)
for i in range(0, len(values), 3):
s = '{}\t{}\t{}\n'.format(values[i], values[i + 1], values[i + 2])
@@ -287,11 +285,12 @@ def processAlgorithm(self, parameters, context, feedback):
offset = [halfcell, -halfcell, halfcell, -halfcell]
rect = self.parameterAsExtent(parameters, param.name(), context)

values = []
values.append(rect.xMinimum())
values.append(rect.xMaximum())
values.append(rect.yMinimum())
values.append(rect.yMaximum())
values = [
rect.xMinimum(),
rect.xMaximum(),
rect.yMinimum(),
rect.yMaximum(),
]

for i in range(4):
command += ' -{} {}'.format(param.name().split(' ')[i], float(values[i]) + offset[i])
@@ -338,8 +337,7 @@ def processAlgorithm(self, parameters, context, feedback):
# 3: Run SAGA
commands = self.editCommands(commands)
SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
loglines = []
loglines.append(self.tr('SAGA execution commands'))
loglines = [self.tr('SAGA execution commands')]
for line in commands:
feedback.pushCommandInfo(line)
loglines.append(line)
@@ -364,11 +362,11 @@ def processAlgorithm(self, parameters, context, feedback):
oldPath = os.path.join(oldFolder, f)
shutil.move(oldPath, newPath)

result = {}
for o in self.outputDefinitions():
if o.name() in output_files:
result[o.name()] = output_files[o.name()]
return result
return {
o.name(): output_files[o.name()]
for o in self.outputDefinitions()
if o.name() in output_files
}

def preProcessInputs(self):
name = self.name().replace('.', '_')

0 comments on commit ef6799f

Please sign in to comment.