41
41
QgsProcessingParameterMultipleLayers ,
42
42
QgsProcessingParameterMatrix ,
43
43
QgsProcessingParameterString ,
44
- QgsProcessingParameterField )
44
+ QgsProcessingParameterField ,
45
+ QgsProcessingParameterFile ,
46
+ QgsProcessingParameterExtent ,
47
+ QgsProcessingParameterRasterDestination ,
48
+ QgsProcessingParameterVectorDestination )
45
49
from processing .core .ProcessingConfig import ProcessingConfig
46
- from processing .core .parameters import (getParameterFromString ,
47
- ParameterExtent )
48
- from processing .core .outputs import (getOutputFromString ,
49
- OutputVector ,
50
- OutputRaster )
51
- from processing .tools import dataobjects
50
+ from processing .core .parameters import getParameterFromString
52
51
from processing .algs .help import shortHelp
53
52
from processing .tools .system import getTempFilename
54
53
from processing .algs .saga .SagaNameDecorator import decoratedAlgorithmName , decoratedGroupName
@@ -131,8 +130,8 @@ def defineCharacteristicsFromFile(self):
131
130
elif line .startswith ('Extent' ):
132
131
# An extent parameter that wraps 4 SAGA numerical parameters
133
132
self .extentParamNames = line [6 :].strip ().split (' ' )
134
- self .params .append (ParameterExtent (self .OUTPUT_EXTENT ,
135
- 'Output extent' ))
133
+ self .params .append (QgsProcessingParameterExtent (self .OUTPUT_EXTENT ,
134
+ 'Output extent' ))
136
135
else :
137
136
pass # TODO
138
137
#self.addOutput(getOutputFromString(line))
@@ -160,9 +159,14 @@ def processAlgorithm(self, parameters, context, feedback):
160
159
elif isinstance (param , QgsProcessingParameterFeatureSource ):
161
160
if param .name () not in parameters or parameters [param .name ()] is None :
162
161
continue
163
- layer_path = self .parameterAsCompatibleSourceLayerPath (parameters , param .name (), context , 'shp' , feedback = feedback )
162
+
163
+ if not crs :
164
+ source = self .parameterAsSource (parameters , param .name (), context )
165
+ crs = source .sourceCrs ()
166
+
167
+ layer_path = self .parameterAsCompatibleSourceLayerPath (parameters , param .name (), context , ['shp' ], 'shp' , feedback = feedback )
164
168
if layer_path :
165
- self .exportedLayers [parameters [ param .name ()] ] = layer_path
169
+ self .exportedLayers [param .name ()] = layer_path
166
170
else :
167
171
raise QgsProcessingException (
168
172
self .tr ('Unsupported file format' ))
@@ -186,6 +190,11 @@ def processAlgorithm(self, parameters, context, feedback):
186
190
temp_params = deepcopy (parameters )
187
191
for layer in layers :
188
192
temp_params [param .name ()] = layer
193
+
194
+ if not crs :
195
+ source = self .parameterAsSource (temp_params , param .name (), context )
196
+ crs = source .sourceCrs ()
197
+
189
198
layer_path = self .parameterAsCompatibleSourceLayerPath (temp_params , param .name (), context , 'shp' ,
190
199
feedback = feedback )
191
200
if layer_path :
@@ -197,31 +206,26 @@ def processAlgorithm(self, parameters, context, feedback):
197
206
raise QgsProcessingException (
198
207
self .tr ('Unsupported file format' ))
199
208
200
- # TODO - set minimum extent
201
- if not extent :
202
- extent = QgsProcessingUtils .combineLayerExtents ([layer ])
203
-
204
209
# 2: Set parameters and outputs
205
210
command = self .undecorated_group + ' "' + self .cmdname + '"'
206
211
command += ' ' + ' ' .join (self .hardcoded_strings )
207
212
208
213
for param in self .parameterDefinitions ():
209
214
if not param .name () in parameters or parameters [param .name ()] is None :
210
215
continue
216
+ if param .isDestination ():
217
+ continue
218
+
211
219
if isinstance (param , (QgsProcessingParameterRasterLayer , QgsProcessingParameterFeatureSource )):
212
- value = parameters [param .name ()]
213
- if value in list (self .exportedLayers .keys ()):
214
- command += ' -' + param .name () + ' "' \
215
- + self .exportedLayers [value ] + '"'
216
- else :
217
- command += ' -' + param .name () + ' "' + value + '"'
220
+ command += ' -' + param .name () + ' "' \
221
+ + self .exportedLayers [param .name ()] + '"'
218
222
elif isinstance (param , QgsProcessingParameterMultipleLayers ):
219
223
s = parameters [param .name ()]
220
224
for layer in list (self .exportedLayers .keys ()):
221
225
s = s .replace (layer , self .exportedLayers [layer ])
222
226
command += ' -' + ';' .join (self .exportedLayers [param .name ()]) + ' "' + s + '"'
223
227
elif isinstance (param , QgsProcessingParameterBoolean ):
224
- if parameters [ param .name ()] :
228
+ if self . parameterAsBool ( parameters , param .name (), context ) :
225
229
command += ' -' + param .name ().strip () + " true"
226
230
else :
227
231
command += ' -' + param .name ().strip () + " false"
@@ -234,31 +238,48 @@ def processAlgorithm(self, parameters, context, feedback):
234
238
s = values [i ] + '\t ' + values [i + 1 ] + '\t ' + values [i + 2 ] + '\n '
235
239
f .write (s )
236
240
command += ' -' + param .name () + ' "' + tempTableFile + '"'
237
- elif isinstance (param , ParameterExtent ):
241
+ elif isinstance (param , QgsProcessingParameterExtent ):
238
242
# 'We have to substract/add half cell size, since SAGA is
239
243
# center based, not corner based
240
- halfcell = self .getOutputCellsize (parameters ) / 2
244
+ halfcell = self .getOutputCellsize (parameters , context ) / 2
241
245
offset = [halfcell , - halfcell , halfcell , - halfcell ]
242
- values = parameters [param .name ()].split (',' )
246
+ rect = self .parameterAsExtent (parameters , param .name (), context )
247
+
248
+ values = []
249
+ values .append (rect .xMinimum ())
250
+ values .append (rect .yMinimum ())
251
+ values .append (rect .xMaximum ())
252
+ values .append (rect .yMaximum ())
253
+
243
254
for i in range (4 ):
244
255
command += ' -' + self .extentParamNames [i ] + ' ' \
245
256
+ str (float (values [i ]) + offset [i ])
246
257
elif isinstance (param , QgsProcessingParameterNumber ):
247
258
command += ' -' + param .name () + ' ' + str (self .parameterAsDouble (parameters , param .name (), context ))
248
259
elif isinstance (param , QgsProcessingParameterEnum ):
249
260
command += ' -' + param .name () + ' ' + str (self .parameterAsEnum (parameters , param .name (), context ))
250
- elif isinstance (param , QgsProcessingParameterString , QgsProcessingParameterField ):
261
+ elif isinstance (param , (QgsProcessingParameterString , QgsProcessingParameterFile )):
262
+ command += ' -' + param .name () + ' "' + self .parameterAsFile (parameters , param .name (), context ) + '"'
263
+ elif isinstance (param , (QgsProcessingParameterString , QgsProcessingParameterField )):
251
264
command += ' -' + param .name () + ' "' + self .parameterAsString (parameters , param .name (), context ) + '"'
252
265
253
- for out in self .outputs :
254
- command += ' -' + out .name + ' "' + out .getCompatibleFileName (self ) + '"'
266
+ output_layers = []
267
+ output_files = {}
268
+ for out in self .destinationParameterDefinitions ():
269
+ # TODO
270
+ # command += ' -' + out.name() + ' "' + out.getCompatibleFileName(self) + '"'
271
+ file = self .parameterAsOutputLayer (parameters , out .name (), context )
272
+ if isinstance (out , (QgsProcessingParameterRasterDestination , QgsProcessingParameterVectorDestination )):
273
+ output_layers .append (file )
274
+ output_files [out .name ()] = file
275
+ command += ' -' + out .name () + ' "' + file + '"'
255
276
256
277
commands .append (command )
257
278
258
279
# special treatment for RGB algorithm
259
280
# TODO: improve this and put this code somewhere else
260
- for out in self .outputs :
261
- if isinstance (out , OutputRaster ):
281
+ for out in self .destinationParameterDefinitions () :
282
+ if isinstance (out , QgsProcessingParameterRasterDestination ):
262
283
filename = out .getCompatibleFileName (self )
263
284
filename2 = filename + '.sgrd'
264
285
if self .cmdname == 'RGB Composite' :
@@ -277,12 +298,17 @@ def processAlgorithm(self, parameters, context, feedback):
277
298
QgsMessageLog .logMessage ('\n ' .join (loglines ), self .tr ('Processing' ), QgsMessageLog .INFO )
278
299
SagaUtils .executeSaga (feedback )
279
300
280
- if self .crs is not None :
281
- for out in self .outputs :
282
- if isinstance (out , (OutputVector , OutputRaster )):
283
- prjFile = os .path .splitext (out .getCompatibleFileName (self ))[0 ] + ".prj"
284
- with open (prjFile , "w" ) as f :
285
- f .write (self .crs .toWkt ())
301
+ if crs is not None :
302
+ for out in output_layers :
303
+ prjFile = os .path .splitext (out )[0 ] + ".prj"
304
+ with open (prjFile , "w" ) as f :
305
+ f .write (crs .toWkt ())
306
+
307
+ result = {}
308
+ for o in self .outputDefinitions ():
309
+ if o .name () in output_files :
310
+ result [o .name ()] = output_files [o .name ()]
311
+ return result
286
312
287
313
def preProcessInputs (self ):
288
314
name = self .name ().replace ('.' , '_' )
@@ -305,7 +331,7 @@ def editCommands(self, commands):
305
331
else :
306
332
return commands
307
333
308
- def getOutputCellsize (self , parameters ):
334
+ def getOutputCellsize (self , parameters , context ):
309
335
"""Tries to guess the cell size of the output, searching for
310
336
a parameter with an appropriate name for it.
311
337
:param parameters:
@@ -314,7 +340,7 @@ def getOutputCellsize(self, parameters):
314
340
cellsize = 0
315
341
for param in self .parameterDefinitions ():
316
342
if param .name () in parameters and param .name () == 'USER_SIZE' :
317
- cellsize = float (parameters [ param .name ()] )
343
+ cellsize = self . parameterAsDouble (parameters , param .name (), context )
318
344
break
319
345
return cellsize
320
346
0 commit comments