Skip to content

Commit

Permalink
processing: add windows support to exportRasterLayersIntoDirectory (f…
Browse files Browse the repository at this point in the history
…ixes #20146)

(cherry picked from commit b39e5a0)
  • Loading branch information
jef-n committed Nov 11, 2018
1 parent 7c9429a commit c13c97e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
33 changes: 18 additions & 15 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -61,7 +61,6 @@
QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputHtml,
QgsProcessingUtils,
QgsVectorLayer,
QgsProviderRegistry)
from qgis.utils import iface
Expand All @@ -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(
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/grass7/Grass7Utils.py
Expand Up @@ -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():
Expand All @@ -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, ''):
Expand Down

0 comments on commit c13c97e

Please sign in to comment.