Skip to content

Commit 733218c

Browse files
author
Médéric Ribreux
committedNov 4, 2017
(WIP) enable Grass7 provider and start to work on QgsProcessing new framework
1 parent 297c7b5 commit 733218c

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed
 

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@
3838
QgsProcessingUtils,
3939
QgsMessageLog,
4040
QgsProcessingAlgorithm,
41-
QgsProcessingParameterDefinition)
41+
QgsProcessingParameterDefinition,
42+
QgsProcessingException,
43+
QgsProcessingParameterExtent,
44+
QgsProcessingParameterNumber)
4245
from qgis.utils import iface
4346

44-
from processing.core.GeoAlgorithm import GeoAlgorithm
47+
#from processing.core.GeoAlgorithm import GeoAlgorithm (replaced by QgsProcessingAlgorithm)
4548
from processing.core.ProcessingConfig import ProcessingConfig
46-
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
49+
#from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException (replaced by QgsProcessingException).
4750

4851
from processing.core.parameters import (getParameterFromString,
4952
ParameterVector,
5053
ParameterMultipleInput,
51-
ParameterExtent,
52-
ParameterNumber,
53-
ParameterSelection,
54+
#ParameterExtent,
55+
#ParameterNumber,
56+
#ParameterSelection,
5457
ParameterRaster,
5558
ParameterTable,
5659
ParameterBoolean,
@@ -70,7 +73,7 @@
7073
os.path.split(os.path.dirname(__file__))[0], os.pardir))
7174

7275

73-
class Grass7Algorithm(GeoAlgorithm):
76+
class Grass7Algorithm(QgsProcessingAlgorithm):
7477

7578
GRASS_OUTPUT_TYPE_PARAMETER = 'GRASS_OUTPUT_TYPE_PARAMETER'
7679
GRASS_MIN_AREA_PARAMETER = 'GRASS_MIN_AREA_PARAMETER'
@@ -82,10 +85,12 @@ class Grass7Algorithm(GeoAlgorithm):
8285
OUTPUT_TYPES = ['auto', 'point', 'line', 'area']
8386

8487
def __init__(self, descriptionfile):
85-
GeoAlgorithm.__init__(self)
88+
super().__init__()
89+
#GeoAlgorithm.__init__(self)
8690
self._name = ''
8791
self._display_name = ''
8892
self._group = ''
93+
self.grass7Name = ''
8994
self.hardcodedStrings = []
9095
self.descriptionFile = descriptionfile
9196
self.defineCharacteristicsFromFile()
@@ -114,6 +119,11 @@ def icon(self):
114119
def svgIconPath(self):
115120
return QgsApplication.iconPath("providerGrass.svg")
116121

122+
def tr(self, string, context=''):
123+
if context == '':
124+
context = self.__class__.__name__
125+
return QCoreApplication.translate(context, string)
126+
117127
def helpUrl(self):
118128
helpPath = Grass7Utils.grassHelpPath()
119129
if helpPath == '':
@@ -144,9 +154,15 @@ def getParameterDescriptions(self):
144154
return descs
145155

146156
def defineCharacteristicsFromFile(self):
157+
"""
158+
Create algorithm parameters and outputs from a text file.
159+
160+
"""
147161
with open(self.descriptionFile) as lines:
162+
# First line of the file is the Grass algorithm name
148163
line = lines.readline().strip('\n').strip()
149164
self.grass7Name = line
165+
# Second line if the algorithm name in Processing
150166
line = lines.readline().strip('\n').strip()
151167
self._name = line
152168
self._display_name = QCoreApplication.translate("GrassAlgorithm", line)
@@ -155,12 +171,13 @@ def defineCharacteristicsFromFile(self):
155171
self._display_name = self.grass7Name + " - " + self._display_name
156172

157173
self._name = self._name[:self._name.find(' ')].lower()
158-
174+
# Read the grass group
159175
line = lines.readline().strip('\n').strip()
160176
self._group = QCoreApplication.translate("GrassAlgorithm", line)
161177
hasRasterOutput = False
162178
hasVectorInput = False
163179
vectorOutputs = 0
180+
# Then you have parameters/output definition
164181
line = lines.readline().strip('\n').strip()
165182
while line != '':
166183
try:
@@ -191,29 +208,34 @@ def defineCharacteristicsFromFile(self):
191208
QgsMessageLog.logMessage(self.tr('Could not open GRASS GIS 7 algorithm: {0}\n{1}').format(self.descriptionFile, line), self.tr('Processing'), QgsMessageLog.CRITICAL)
192209
raise e
193210

194-
self.addParameter(ParameterExtent(
211+
self.addParameter(QgsProcessingParameterExtent(
195212
self.GRASS_REGION_EXTENT_PARAMETER,
196213
self.tr('GRASS GIS 7 region extent'))
197214
)
198215
if hasRasterOutput:
199-
self.addParameter(ParameterNumber(
216+
self.addParameter(QgsProcessingParameterNumber(
200217
self.GRASS_REGION_CELLSIZE_PARAMETER,
201218
self.tr('GRASS GIS 7 region cellsize (leave 0 for default)'),
202-
0, None, 0.0))
219+
type=QgsProcessingParameterNumber.Double,
220+
minValue=0.0, maxValue=None, defaultValue=0.0)
221+
)
203222
if hasVectorInput:
204-
param = ParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER,
205-
'v.in.ogr snap tolerance (-1 = no snap)',
206-
-1, None, -1.0)
223+
param = QgsProcessingParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER,
224+
self.tr('v.in.ogr snap tolerance (-1 = no snap)'),
225+
type=QgsProcessingParameterNumber.Double,
226+
minValue=-1.0, maxValue=None, defaultValue=-1.0)
207227
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
208228
self.addParameter(param)
209-
param = ParameterNumber(self.GRASS_MIN_AREA_PARAMETER,
210-
'v.in.ogr min area', 0, None, 0.0001)
229+
param = QgsProcessingParameterNumber(self.GRASS_MIN_AREA_PARAMETER,
230+
self.tr('v.in.ogr min area'),
231+
type=QgsProcessingParameterNumber.double,
232+
minValue=0.0, maxValue=None, defaultValue=0.0001)
211233
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
212234
self.addParameter(param)
213235
if vectorOutputs == 1:
214-
param = ParameterSelection(self.GRASS_OUTPUT_TYPE_PARAMETER,
215-
'v.out.ogr output type',
216-
self.OUTPUT_TYPES)
236+
param = QgsProcessingParameterEnum(self.GRASS_OUTPUT_TYPE_PARAMETER,
237+
self.tr('v.out.ogr output type'),
238+
self.OUTPUT_TYPES)
217239
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
218240
self.addParameter(param)
219241

@@ -250,7 +272,7 @@ def processAlgorithm(self, parameters, context, feedback):
250272
if system.isWindows():
251273
path = Grass7Utils.grassPath()
252274
if path == '':
253-
raise GeoAlgorithmExecutionException(
275+
raise QgsProcessingException(
254276
self.tr('GRASS GIS 7 folder is not configured. Please '
255277
'configure it before running GRASS GIS 7 algorithms.'))
256278

@@ -421,7 +443,7 @@ def processCommand(self, parameters):
421443
elif isinstance(param, ParameterBoolean):
422444
if param.value:
423445
command += ' ' + param.name()
424-
elif isinstance(param, ParameterSelection):
446+
elif isinstance(param, QgsProcessingParameterEnum):
425447
idx = int(param.value)
426448
command += ' ' + param.name() + '=' + str(param.options[idx][1])
427449
elif isinstance(param, ParameterString):
@@ -579,6 +601,7 @@ def exportRasterLayer(self, layer):
579601
return command
580602

581603
def getTempFilename(self):
604+
# TODO Replace with QgsProcessingUtils generateTempFilename
582605
return system.getTempFilename()
583606

584607
def canExecute(self):

‎python/plugins/processing/algs/grass7/TODO.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ QGIS3 Processing Port
44
=====================
55

66
* Port to Python3.
7+
* print -> print(
8+
* unicode -> str
9+
* dict iteritems
710
* Replace all parameters by QgsProcessingParameters.
811
* Re-enable GRASS algorithm by default.
912
* Add GRASS 7.2 new algorithms.
1013
* Improve unit tests
14+
* GRASS_REGION_CELLSIZE_PARAMETER is integer or double?
15+
* GRASS_SNAP_TOLERANCE_PARAMETER is integer or double?
16+
* Do we need to use QgsProcessingParameters::parameterFromScriptCode for description files?
17+
We don't NEED but we have to improve getParameterFromString (or use an internal method) at least.
18+
There is also a problem for parameterFromScriptCode: it doesn't use description very well.
19+
We can also use parameterFromVariantMap and a custom internal Grass7Algorithm getParameterFromString.
20+
* Make tests under MS-Windows 7 for Utf-8 support.
1121

1222
Unit tests
1323
==========

‎python/plugins/processing/core/Processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from processing.tools import dataobjects
5555

5656
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider # NOQA
57-
#from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider # NOQA
57+
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider
5858
from processing.algs.gdal.GdalAlgorithmProvider import GdalAlgorithmProvider # NOQA
5959
#from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider # NOQA
6060
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider # NOQA

0 commit comments

Comments
 (0)
Please sign in to comment.