Skip to content

Commit 9a52e34

Browse files
author
Médéric Ribreux
committed
First working raster algorithm
1 parent 483a4b9 commit 9a52e34

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,9 @@
5959
QgsProcessingOutputHtml)
6060
from qgis.utils import iface
6161

62-
#from processing.core.GeoAlgorithm import GeoAlgorithm (replaced by QgsProcessingAlgorithm)
6362
from processing.core.ProcessingConfig import ProcessingConfig
64-
#from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException (replaced by QgsProcessingException).
6563

6664
from processing.core.parameters import (getParameterFromString)
67-
#from processing.core.outputs import (getOutputFromString,
68-
# OutputRaster,
69-
# OutputVector,
70-
# OutputFile,
71-
# OutputHTML)
7265

7366
from .Grass7Utils import Grass7Utils
7467

@@ -183,7 +176,6 @@ def initAlgorithm(self, config=None):
183176
def defineCharacteristicsFromFile(self):
184177
"""
185178
Create algorithm parameters and outputs from a text file.
186-
187179
"""
188180
with open(self.descriptionFile) as lines:
189181
# First line of the file is the Grass algorithm name
@@ -308,6 +300,7 @@ def grabDefaultGrassParameters(self, parameters, context):
308300
self.region = self.parameterAsExtent(parameters,
309301
self.GRASS_REGION_EXTENT_PARAMETER,
310302
context)
303+
QgsMessageLog.logMessage('processAlgorithm self.region: {}'.format(self.region.isEmpty()), 'Grass7', QgsMessageLog.INFO)
311304
# GRASS cell size
312305
self.cellSize = self.parameterAsString(parameters,
313306
self.GRASS_REGION_CELLSIZE_PARAMETER,
@@ -426,24 +419,25 @@ def processInputs(self, parameters, context):
426419
if not paramName in parameters:
427420
continue
428421
layer = self.parameterAsRasterLayer(parameters, paramName, context)
429-
layerSrc = self.parameterAsCompatibleSourceLayerPath(parameters, paramName, context,
430-
QgsVectorFileWriter.supportedFormatExtensions())
431-
422+
layerSrc = parameters[paramName]
432423
# Check if the layer hasn't already been exported in, for
433424
# example, previous GRASS calls in this session
434425
if paramName in self.exportedLayers:
435426
continue
436427
else:
428+
layers.append(layer)
437429
self.setSessionProjectionFromLayer(layer)
438430
self.commands.append(self.exportRasterLayer(paramName, layerSrc))
439431
# Vector inputs needs to be imported into temp GRASS DB
440432
if isinstance(param, QgsProcessingParameterVectorLayer):
441433
if not paramName in parameters:
442434
continue
443435
layer = self.parameterAsVectorLayer(parameters, paramName, context)
444-
layerSrc = self.parameterAsCompatibleSourceLayerPath(parameters, paramName, context,
445-
QgsVectorFileWriter.supportedFormatExtensions())
446-
436+
layerSrc = self.parameterAsCompatibleSourceLayerPath(
437+
parameters, paramName, context,
438+
self.provider().supportedOutputVectorLayerExtensions()
439+
)
440+
447441
if paramName in self.exportedLayers:
448442
continue
449443
else:
@@ -482,11 +476,11 @@ def processInputs(self, parameters, context):
482476
self.setSessionProjectionFromProject()
483477

484478
# Build GRASS region
485-
if not self.region:
479+
if self.region.isEmpty():
486480
self.region = QgsProcessingUtils.combineLayerExtents(layers)
487481
command = 'g.region n={} s={} e={} w={}'.format(
488482
self.region.yMaximum(), self.region.yMinimum(),
489-
self.region.xMinimum(), self.region.xMaximum()
483+
self.region.xMaximum(), self.region.xMinimum()
490484
)
491485
# TODO Handle cell size
492486
#if not self.cellSize:
@@ -625,7 +619,7 @@ def processOutputs(self, parameters, context):
625619
self.commands.append(command)
626620
self.outputCommands.append(command)
627621
else:
628-
command += ' {} output="{}"'.format(uniqName, fileName)
622+
command += '{} output="{}"'.format(uniqName, fileName)
629623
self.commands.append(command)
630624
self.outputCommands.append(command)
631625

@@ -712,6 +706,10 @@ def setSessionProjectionFromLayer(self, layer):
712706
Grass7Utils.projectionSet = True
713707

714708
def exportRasterLayer(self, layerKey, layerSrc):
709+
"""
710+
Creates a dedicated command to load a raster into
711+
temporary GRASS DB.
712+
"""
715713
# TODO: handle multiple bands
716714
destFilename = 'a' + os.path.basename(self.getTempFilename())
717715
self.exportedLayers[layerKey] = destFilename

python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
from qgis.PyQt.QtCore import QCoreApplication
3131
from qgis.core import (QgsApplication,
3232
QgsProcessingProvider,
33+
QgsVectorFileWriter,
3334
QgsMessageLog,
3435
QgsProcessingUtils)
35-
from processing.core.ProcessingConfig import ProcessingConfig, Setting
36+
from processing.core.ProcessingConfig import (ProcessingConfig, Setting)
3637
from .Grass7Utils import Grass7Utils
3738
from .Grass7Algorithm import Grass7Algorithm
3839
from processing.tools.system import isWindows, isMac
3940
#from .nviz7 import nviz7
41+
from processing.algs.gdal.GdalUtils import GdalUtils
4042

4143
pluginPath = os.path.normpath(os.path.join(
4244
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -124,7 +126,18 @@ def svgIconPath(self):
124126
return QgsApplication.iconPath("providerGrass.svg")
125127

126128
def supportedOutputVectorLayerExtensions(self):
127-
return ['shp']
129+
# We use the same extensions than QGIS because:
130+
# - QGIS is using OGR like GRASS
131+
# - There are very chances than OGR version used in GRASS is
132+
# different from QGIS OGR version.
133+
return QgsVectorFileWriter.supportedFormatExtensions()
134+
135+
def supportedOutputRasterLayerExtensions(self):
136+
# We use the same extensions than GDAL because:
137+
# - GRASS is also using GDAL for raster imports.
138+
# - Chances that GRASS is compiled with another version of
139+
# GDAL than QGIS are very limited!
140+
return GdalUtils.getSupportedRasterExtensions()
128141

129142
def canBeActivated(self):
130143
return not bool(Grass7Utils.checkGrass7IsInstalled())

0 commit comments

Comments
 (0)