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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
QgsProcessingParameterFile, QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination, QgsProcessingParameterFolderDestination,
QgsProcessingOutputHtml, QgsProcessingOutputHtml,
QgsProcessingUtils,
QgsVectorLayer, QgsVectorLayer,
QgsProviderRegistry) QgsProviderRegistry)
from qgis.utils import iface from qgis.utils import iface
Expand All @@ -73,7 +72,6 @@


from .Grass7Utils import Grass7Utils from .Grass7Utils import Grass7Utils


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


pluginPath = os.path.normpath(os.path.join( 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: for p in self.params:
# We use createOutput argument for automatic output creation # We use createOutput argument for automatic output creation
res = self.addParameter(p, True) self.addParameter(p, True)


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


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


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


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


# Add a loop export from the basename # Add a loop export from the basename
for cmd in [self.commands, self.outputCommands]: for cmd in [self.commands, self.outputCommands]:
# TODO Windows support
# TODO Format/options support # TODO Format/options support
cmd.append("for r in $(g.list type=rast pattern='{}*'); do".format(basename)) if isWindows():
cmd.append(" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format( cmd.append("if not exist {0} mkdir {0}".format(outDir))
' -t' if colorTable else '', 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(
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"' basename,
) ' -t' if colorTable else '',
) outDir,
cmd.append("done") '--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): 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): def convertToHtml(self, fileName):
# Read HTML contents # Read HTML contents
lines = [] lines = []
html = False
with open(fileName, 'r', encoding='utf-8') as f: with open(fileName, 'r', encoding='utf-8') as f:
lines = f.readlines() lines = f.readlines()


Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/grass7/Grass7Utils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def executeGrass(commands, feedback, outputCommands=None):
loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output')) loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
grassOutDone = False grassOutDone = False
command, grassenv = Grass7Utils.prepareGrassExecution(commands) 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. # For MS-Windows, we need to hide the console window.
if isWindows(): if isWindows():
Expand All @@ -369,6 +369,7 @@ def executeGrass(commands, feedback, outputCommands=None):
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
universal_newlines=True, universal_newlines=True,
env=grassenv, env=grassenv,
encoding="cp{}".format(Grass7Utils.getWindowsCodePage()) if isWindows() else None,
startupinfo=si if isWindows() else None startupinfo=si if isWindows() else None
) as proc: ) as proc:
for line in iter(proc.stdout.readline, ''): for line in iter(proc.stdout.readline, ''):
Expand Down

0 comments on commit c13c97e

Please sign in to comment.