Skip to content
Permalink
Browse files

processing: add windows support to exportRasterLayersIntoDirectory (f…

…ixes #20146)

(cherry picked from commit b39e5a0)
  • Loading branch information
jef-n committed Nov 11, 2018
1 parent 7c9429a commit c13c97eeb2d2fb5c765d0b3e7ea7fad956eb243a
@@ -61,7 +61,6 @@
QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputHtml,
QgsProcessingUtils,
QgsVectorLayer,
QgsProviderRegistry)
from qgis.utils import iface
@@ -73,7 +72,6 @@

from .Grass7Utils import Grass7Utils

#from processing.tools import dataobjects, system
from processing.tools.system import isWindows, getTempFilename

pluginPath = os.path.normpath(os.path.join(
@@ -187,7 +185,7 @@ def initAlgorithm(self, config=None):
"""
for p in self.params:
# We use createOutput argument for automatic output creation
res = self.addParameter(p, True)
self.addParameter(p, True)

def defineCharacteristicsFromFile(self):
"""
@@ -441,7 +439,7 @@ def processInputs(self, parameters, context, feedback):
QgsProcessingParameterMultipleLayers))]
for param in inputs:
paramName = param.name()
if not paramName in parameters:
if paramName not in parameters:
continue
# Handle Null parameter
if parameters[paramName] is None:
@@ -640,7 +638,6 @@ def processCommand(self, parameters, context, feedback, delOutputs=False):
if outName in parameters and parameters[outName] is not None:
# We add an output name to make sure it is unique if the session
# uses this algorithm several times.
#value = self.parameterAsOutputLayer(parameters, outName, context)
uniqueOutputName = outName + self.uniqueSuffix
command += ' {}={}'.format(outName, uniqueOutputName)

@@ -669,7 +666,7 @@ def processOutputs(self, parameters, context, feedback):

for out in self.destinationParameterDefinitions():
outName = out.name()
if not outName in parameters:
if outName not in parameters:
# skipped output
continue

@@ -785,15 +782,22 @@ def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=

# Add a loop export from the basename
for cmd in [self.commands, self.outputCommands]:
# TODO Windows support
# TODO Format/options support
cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename))
cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format(
' -t' if colorTable else '', outDir,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"'
)
)
cmd.append("done")
if isWindows():
cmd.append("if not exist {0} mkdir {0}".format(outDir))
cmd.append("for /F %%r IN ('g.list type^=rast pattern^=\"{0}*\"') do r.out.gdal -m{1} input=%%r output={2}/%%r.tif {3}".format(
basename,
' -t' if colorTable else '',
outDir,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"'
))
else:
cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename))
cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format(
' -t' if colorTable else '', outDir,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"'
))
cmd.append("done")

def loadVectorLayerFromParameter(self, name, parameters, context, feedback, external=False):
"""
@@ -999,7 +1003,6 @@ def setSessionProjectionFromLayer(self, layer):
def convertToHtml(self, fileName):
# Read HTML contents
lines = []
html = False
with open(fileName, 'r', encoding='utf-8') as f:
lines = f.readlines()

@@ -353,7 +353,7 @@ def executeGrass(commands, feedback, outputCommands=None):
loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
grassOutDone = False
command, grassenv = Grass7Utils.prepareGrassExecution(commands)
#QgsMessageLog.logMessage('exec: {}'.format(command), 'DEBUG', Qgis.Info)
# QgsMessageLog.logMessage('exec: {}'.format(command), 'DEBUG', Qgis.Info)

# For MS-Windows, we need to hide the console window.
if isWindows():
@@ -369,6 +369,7 @@ def executeGrass(commands, feedback, outputCommands=None):
stderr=subprocess.STDOUT,
universal_newlines=True,
env=grassenv,
encoding="cp{}".format(Grass7Utils.getWindowsCodePage()) if isWindows() else None,
startupinfo=si if isWindows() else None
) as proc:
for line in iter(proc.stdout.readline, ''):

0 comments on commit c13c97e

Please sign in to comment.
You can’t perform that action at this time.