57
57
QgsProcessingParameterFileDestination ,
58
58
QgsProcessingOutputVectorLayer ,
59
59
QgsProcessingOutputRasterLayer ,
60
- QgsProcessingOutputHtml )
60
+ QgsProcessingOutputHtml ,
61
+ QgsProcessingUtils )
61
62
from qgis .utils import iface
62
63
63
64
from processing .core .ProcessingConfig import ProcessingConfig
66
67
67
68
from .Grass7Utils import Grass7Utils
68
69
69
- from processing .tools import dataobjects , system
70
+ #from processing.tools import dataobjects, system
71
+ from processing .tools .system import isWindows , getTempFilename
70
72
71
73
pluginPath = os .path .normpath (os .path .join (
72
74
os .path .split (os .path .dirname (__file__ ))[0 ], os .pardir ))
@@ -172,7 +174,12 @@ def initAlgorithm(self, config=None):
172
174
"""
173
175
for p in self .params :
174
176
# We use createOutput argument for automatic output creation
175
- self .addParameter (p , True )
177
+ res = self .addParameter (p , True )
178
+ # File destinations are not automatically added as outputs
179
+ if isinstance (p , QgsProcessingParameterFileDestination ):
180
+ self .addOutput (QgsProcessingOutputHtml (p .name (), p .description ()))
181
+ QgsMessageLog .logMessage ('initAlgorithm {} ({}):{}' .format (p .name (), p .isDestination (), res ), 'Grass7' , QgsMessageLog .INFO )
182
+ QgsMessageLog .logMessage ('initAlgo: {}' .format (p .toOutputDefinition ()), 'Grass7' , QgsMessageLog .INFO )
176
183
177
184
def defineCharacteristicsFromFile (self ):
178
185
"""
@@ -266,39 +273,27 @@ def getDefaultCellSize(self, parameters, context):
266
273
"""
267
274
Determine a default cell size from all the raster layers.
268
275
"""
269
- if self .cellSize :
270
- return True
271
-
272
- cellsize = 0
273
- parameters = [p for p in self .parameterDefinitions ()
276
+ cellsize = 0.0
277
+ layerParams = [p for p in self .parameterDefinitions ()
274
278
if isinstance (p , (QgsProcessingParameterRasterLayer ,
275
279
QgsProcessingParameterMultipleLayers ))]
276
280
cz = lambda l , cellsize : max (cellsize , (l .extent ().xMaximum () - l .extent ().xMinimum ()) / l .width ())
277
281
278
- for param in parameters :
282
+ for param in layerParams :
279
283
paramName = param .name ()
280
284
if isinstance (param , QgsProcessingParameterRasterLayer ):
281
- layer = self .parameterAsExtent (parameters , paramName , context )
282
- cellsize = max (cellsize , (layer .extent ().xMaximum () -
283
- layer .extent ().xMinimum ()) /
284
- layer .width ())
285
- #cellsize = cz(layer, cellsize)
285
+ layer = self .parameterAsLayer (parameters , paramName , context )
286
+ cellsize = cz (layer , cellsize )
286
287
elif isinstance (param , QgsProcessingParameterMultipleLayers ):
287
288
layers = self .parameterAsLayerList (parameters , paramName , context )
288
289
for layer in layers :
289
290
if layer .type () == QgsMapLayer .RasterLayer :
290
- #cellsize = cz(layer, cellsize)
291
- cellsize = max (cellsize , (
292
- layer .extent ().xMaximum () -
293
- layer .extent ().xMinimum ()) /
294
- layer .width ())
295
-
296
- QgsMessageLog .logMessage ('cellSize: {}' .format (cellsize ), 'Grass7' , QgsMessageLog .INFO )
297
- if cellsize == 0 :
298
- return False
299
- else :
300
- self .cellSize = cellsize
301
- return True
291
+ cellsize = cz (layer , cellsize )
292
+
293
+ if cellsize == 0.0 :
294
+ cellsize = 100.0
295
+
296
+ return cellsize
302
297
303
298
def grabDefaultGrassParameters (self , parameters , context ):
304
299
"""
@@ -309,11 +304,11 @@ def grabDefaultGrassParameters(self, parameters, context):
309
304
self .region = self .parameterAsExtent (parameters ,
310
305
self .GRASS_REGION_EXTENT_PARAMETER ,
311
306
context )
312
- QgsMessageLog .logMessage ('processAlgorithm self.region: {}' .format (self .region .isEmpty ()), 'Grass7' , QgsMessageLog .INFO )
313
307
# GRASS cell size
314
- self .cellSize = self .parameterAsString (parameters ,
315
- self .GRASS_REGION_CELLSIZE_PARAMETER ,
316
- context )
308
+ if self .parameterDefinition (self .GRASS_REGION_CELLSIZE_PARAMETER ):
309
+ self .cellSize = self .parameterAsDouble (parameters ,
310
+ self .GRASS_REGION_CELLSIZE_PARAMETER ,
311
+ context )
317
312
# GRASS snap tolerance
318
313
self .snapTolerance = self .parameterAsDouble (parameters ,
319
314
self .GRASS_SNAP_TOLERANCE_PARAMETER ,
@@ -332,7 +327,7 @@ def grabDefaultGrassParameters(self, parameters, context):
332
327
context )
333
328
334
329
def processAlgorithm (self , parameters , context , feedback ):
335
- if system . isWindows ():
330
+ if isWindows ():
336
331
path = Grass7Utils .grassPath ()
337
332
if path == '' :
338
333
raise QgsProcessingException (
@@ -407,6 +402,7 @@ def processAlgorithm(self, parameters, context, feedback):
407
402
Grass7Utils .endGrass7Session ()
408
403
409
404
# Return outputs map
405
+ QgsMessageLog .logMessage ('outputDefinitions: {}' .format (self .outputDefinitions ()), 'Grass7' , QgsMessageLog .INFO )
410
406
outputs = {}
411
407
for outName in [o .name () for o in self .outputDefinitions ()]:
412
408
if outName in parameters :
@@ -484,8 +480,12 @@ def processInputs(self, parameters, context):
484
480
self .region .xMaximum (), self .region .xMinimum ()
485
481
)
486
482
# Handle cell size
487
- if self .getDefaultCellSize (parameters , context ):
488
- command += ' res={}' .format (self .cellSize )
483
+ if self .parameterDefinition (self .GRASS_REGION_CELLSIZE_PARAMETER ):
484
+ if self .cellSize :
485
+ cellSize = self .cellSize
486
+ else :
487
+ cellSize = self .getDefaultCellSize (parameters , context )
488
+ command += ' res={}' .format (cellSize )
489
489
490
490
# Handle align to resolution
491
491
if self .alignToResolution :
@@ -496,7 +496,8 @@ def processInputs(self, parameters, context):
496
496
QgsMessageLog .logMessage ('processInputs end. Commands: {}' .format (self .commands ), 'Grass7' , QgsMessageLog .INFO )
497
497
498
498
def processCommand (self , parameters , context ):
499
- """Prepare the GRASS algorithm command
499
+ """
500
+ Prepare the GRASS algorithm command
500
501
:param parameters:
501
502
"""
502
503
noOutputs = [o for o in self .parameterDefinitions () if o not in self .destinationParameterDefinitions ()]
@@ -646,6 +647,19 @@ def processOutputs(self, parameters, context):
646
647
self .outputCommands .append (command )
647
648
QgsMessageLog .logMessage ('processOutputs. Commands: {}' .format (self .commands ), 'Grass7' , QgsMessageLog .INFO )
648
649
650
+ def exportRasterLayer (self , layerKey , layerSrc ):
651
+ """
652
+ Creates a dedicated command to load a raster into
653
+ temporary GRASS DB.
654
+ """
655
+ # TODO: handle multiple bands
656
+ #destFileName = QgsProcessingUtils.generateTempFilename(layerKey)
657
+ destFilename = 'a' + os .path .basename (getTempFilename ())
658
+ self .exportedLayers [layerKey ] = destFilename
659
+ command = 'r.external input="{}" band=1 output={} --overwrite -o' .format (
660
+ layerSrc , destFilename )
661
+ return command
662
+
649
663
def exportVectorLayer (self , layerKey , layerSrc ):
650
664
# TODO: learn about ProcessingContexts
651
665
#context = dataobjects.createContext()
@@ -671,7 +685,8 @@ def exportVectorLayer(self, layerKey, layerSrc):
671
685
# filename = orgFilename
672
686
# TODO handle selection with a where_clause?
673
687
# TODO use v.external as an option!
674
- destFileName = 'a' + os .path .basename (self .getTempFilename ())
688
+ #destFileName = QgsProcessingUtils.generateTempFilename(layerKey)
689
+ destFileName = 'a' + os .path .basename (getTempFilename ())
675
690
self .exportedLayers [layerKey ] = destFileName
676
691
layerFileName = os .path .basename (layerSrc )
677
692
layerName = os .path .splitext (layerFileName )[0 ]
@@ -698,29 +713,13 @@ def setSessionProjectionFromLayer(self, layer):
698
713
Set the projection from a QgsVectorLayer.
699
714
We creates a PROJ4 definition which is transmitted to Grass
700
715
"""
701
- context = dataobjects .createContext ()
716
+ # context = dataobjects.createContext()
702
717
if not Grass7Utils .projectionSet :
703
718
proj4 = str (layer .crs ().toProj4 ())
704
719
command = 'g.proj -c proj4="{}"' .format (proj4 )
705
720
self .commands .append (command )
706
721
Grass7Utils .projectionSet = True
707
722
708
- def exportRasterLayer (self , layerKey , layerSrc ):
709
- """
710
- Creates a dedicated command to load a raster into
711
- temporary GRASS DB.
712
- """
713
- # TODO: handle multiple bands
714
- destFilename = 'a' + os .path .basename (self .getTempFilename ())
715
- self .exportedLayers [layerKey ] = destFilename
716
- command = 'r.external input="{}" band=1 output={} --overwrite -o' .format (
717
- layerSrc , destFilename )
718
- return command
719
-
720
- def getTempFilename (self ):
721
- # TODO Replace with QgsProcessingUtils generateTempFilename
722
- return system .getTempFilename ()
723
-
724
723
def canExecute (self ):
725
724
message = Grass7Utils .checkGrass7IsInstalled ()
726
725
return not message , message
0 commit comments