38
38
QgsProcessingUtils ,
39
39
QgsMessageLog ,
40
40
QgsProcessingAlgorithm ,
41
- QgsProcessingParameterDefinition )
41
+ QgsProcessingParameterDefinition ,
42
+ QgsProcessingException ,
43
+ QgsProcessingParameterExtent ,
44
+ QgsProcessingParameterNumber )
42
45
from qgis .utils import iface
43
46
44
- from processing .core .GeoAlgorithm import GeoAlgorithm
47
+ # from processing.core.GeoAlgorithm import GeoAlgorithm (replaced by QgsProcessingAlgorithm)
45
48
from processing .core .ProcessingConfig import ProcessingConfig
46
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
49
+ # from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException (replaced by QgsProcessingException).
47
50
48
51
from processing .core .parameters import (getParameterFromString ,
49
52
ParameterVector ,
50
53
ParameterMultipleInput ,
51
- ParameterExtent ,
52
- ParameterNumber ,
53
- ParameterSelection ,
54
+ # ParameterExtent,
55
+ # ParameterNumber,
56
+ # ParameterSelection,
54
57
ParameterRaster ,
55
58
ParameterTable ,
56
59
ParameterBoolean ,
70
73
os .path .split (os .path .dirname (__file__ ))[0 ], os .pardir ))
71
74
72
75
73
- class Grass7Algorithm (GeoAlgorithm ):
76
+ class Grass7Algorithm (QgsProcessingAlgorithm ):
74
77
75
78
GRASS_OUTPUT_TYPE_PARAMETER = 'GRASS_OUTPUT_TYPE_PARAMETER'
76
79
GRASS_MIN_AREA_PARAMETER = 'GRASS_MIN_AREA_PARAMETER'
@@ -82,10 +85,12 @@ class Grass7Algorithm(GeoAlgorithm):
82
85
OUTPUT_TYPES = ['auto' , 'point' , 'line' , 'area' ]
83
86
84
87
def __init__ (self , descriptionfile ):
85
- GeoAlgorithm .__init__ (self )
88
+ super ().__init__ ()
89
+ #GeoAlgorithm.__init__(self)
86
90
self ._name = ''
87
91
self ._display_name = ''
88
92
self ._group = ''
93
+ self .grass7Name = ''
89
94
self .hardcodedStrings = []
90
95
self .descriptionFile = descriptionfile
91
96
self .defineCharacteristicsFromFile ()
@@ -114,6 +119,11 @@ def icon(self):
114
119
def svgIconPath (self ):
115
120
return QgsApplication .iconPath ("providerGrass.svg" )
116
121
122
+ def tr (self , string , context = '' ):
123
+ if context == '' :
124
+ context = self .__class__ .__name__
125
+ return QCoreApplication .translate (context , string )
126
+
117
127
def helpUrl (self ):
118
128
helpPath = Grass7Utils .grassHelpPath ()
119
129
if helpPath == '' :
@@ -144,9 +154,15 @@ def getParameterDescriptions(self):
144
154
return descs
145
155
146
156
def defineCharacteristicsFromFile (self ):
157
+ """
158
+ Create algorithm parameters and outputs from a text file.
159
+
160
+ """
147
161
with open (self .descriptionFile ) as lines :
162
+ # First line of the file is the Grass algorithm name
148
163
line = lines .readline ().strip ('\n ' ).strip ()
149
164
self .grass7Name = line
165
+ # Second line if the algorithm name in Processing
150
166
line = lines .readline ().strip ('\n ' ).strip ()
151
167
self ._name = line
152
168
self ._display_name = QCoreApplication .translate ("GrassAlgorithm" , line )
@@ -155,12 +171,13 @@ def defineCharacteristicsFromFile(self):
155
171
self ._display_name = self .grass7Name + " - " + self ._display_name
156
172
157
173
self ._name = self ._name [:self ._name .find (' ' )].lower ()
158
-
174
+ # Read the grass group
159
175
line = lines .readline ().strip ('\n ' ).strip ()
160
176
self ._group = QCoreApplication .translate ("GrassAlgorithm" , line )
161
177
hasRasterOutput = False
162
178
hasVectorInput = False
163
179
vectorOutputs = 0
180
+ # Then you have parameters/output definition
164
181
line = lines .readline ().strip ('\n ' ).strip ()
165
182
while line != '' :
166
183
try :
@@ -191,29 +208,34 @@ def defineCharacteristicsFromFile(self):
191
208
QgsMessageLog .logMessage (self .tr ('Could not open GRASS GIS 7 algorithm: {0}\n {1}' ).format (self .descriptionFile , line ), self .tr ('Processing' ), QgsMessageLog .CRITICAL )
192
209
raise e
193
210
194
- self .addParameter (ParameterExtent (
211
+ self .addParameter (QgsProcessingParameterExtent (
195
212
self .GRASS_REGION_EXTENT_PARAMETER ,
196
213
self .tr ('GRASS GIS 7 region extent' ))
197
214
)
198
215
if hasRasterOutput :
199
- self .addParameter (ParameterNumber (
216
+ self .addParameter (QgsProcessingParameterNumber (
200
217
self .GRASS_REGION_CELLSIZE_PARAMETER ,
201
218
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
+ )
203
222
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 )
207
227
param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
208
228
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 )
211
233
param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
212
234
self .addParameter (param )
213
235
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 )
217
239
param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
218
240
self .addParameter (param )
219
241
@@ -250,7 +272,7 @@ def processAlgorithm(self, parameters, context, feedback):
250
272
if system .isWindows ():
251
273
path = Grass7Utils .grassPath ()
252
274
if path == '' :
253
- raise GeoAlgorithmExecutionException (
275
+ raise QgsProcessingException (
254
276
self .tr ('GRASS GIS 7 folder is not configured. Please '
255
277
'configure it before running GRASS GIS 7 algorithms.' ))
256
278
@@ -421,7 +443,7 @@ def processCommand(self, parameters):
421
443
elif isinstance (param , ParameterBoolean ):
422
444
if param .value :
423
445
command += ' ' + param .name ()
424
- elif isinstance (param , ParameterSelection ):
446
+ elif isinstance (param , QgsProcessingParameterEnum ):
425
447
idx = int (param .value )
426
448
command += ' ' + param .name () + '=' + str (param .options [idx ][1 ])
427
449
elif isinstance (param , ParameterString ):
@@ -579,6 +601,7 @@ def exportRasterLayer(self, layer):
579
601
return command
580
602
581
603
def getTempFilename (self ):
604
+ # TODO Replace with QgsProcessingUtils generateTempFilename
582
605
return system .getTempFilename ()
583
606
584
607
def canExecute (self ):
0 commit comments