16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from processing . modeler . ModelerAlgorithm import ValueFromInput , ValueFromOutput
19
+
20
20
import os
21
21
22
22
__author__ = 'Victor Olaya'
29
29
30
30
import math
31
31
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
32
- from processing .core .parameters import ParameterMultipleInput , ParameterExtent , ParameterString , ParameterRaster , ParameterNumber
33
- from processing .core .outputs import OutputRaster
34
- from processing .tools import dataobjects
35
32
from processing .algs .gdal .GdalUtils import GdalUtils
36
- from qgis .core import (QgsApplication ,
37
- QgsRectangle ,
33
+ from qgis .core import (QgsProcessing ,
34
+ QgsProcessingException ,
38
35
QgsProcessingUtils ,
39
- QgsProject )
36
+ QgsProcessingParameterMultipleLayers ,
37
+ QgsProcessingParameterNumber ,
38
+ QgsProcessingParameterExtent ,
39
+ QgsProcessingParameterRasterDestination ,
40
+ QgsProcessingParameterRasterLayer ,
41
+ QgsProcessingOutputRasterLayer ,
42
+ QgsProcessingParameterString )
40
43
from qgis .analysis import QgsRasterCalculator , QgsRasterCalculatorEntry
41
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
42
- from processing .algs .qgis .ui .RasterCalculatorWidgets import LayersListWidgetWrapper , ExpressionWidgetWrapper
43
44
44
45
45
46
class RasterCalculator (QgisAlgorithm ):
@@ -57,41 +58,54 @@ def __init__(self):
57
58
super ().__init__ ()
58
59
59
60
def initAlgorithm (self , config = None ):
60
- self .addParameter (ParameterMultipleInput (self .LAYERS ,
61
- self .tr ('Input layers' ),
62
- datatype = dataobjects .TYPE_RASTER ,
63
- optional = True ,
64
- metadata = {'widget_wrapper' : LayersListWidgetWrapper }))
61
+ layer_param = QgsProcessingParameterMultipleLayers (self .LAYERS ,
62
+ self .tr ('Input layers' ),
63
+ layerType = QgsProcessing .TypeRaster ,
64
+ optional = True )
65
+ layer_param .setMetadata ({'widget_wrapper' : 'processing.algs.qgis.ui.RasterCalculatorWidgets.LayersListWidgetWrapper' })
66
+ self .addParameter (layer_param )
67
+
68
+ class ParameterRasterCalculatorExpression (QgsProcessingParameterString ):
69
+
70
+ def __init__ (self , name = '' , description = '' , multiLine = False ):
71
+ super ().__init__ (name , description , multiLine = multiLine )
72
+ self .setMetadata ({
73
+ 'widget_wrapper' : 'processing.algs.qgis.ui.RasterCalculatorWidgets.ExpressionWidgetWrapper'
74
+ })
65
75
66
- class ParameterRasterCalculatorExpression (ParameterString ):
76
+ def type (self ):
77
+ return 'raster_calc_expression'
78
+
79
+ def clone (self ):
80
+ return ParameterRasterCalculatorExpression (self .name (), self .description (), self .multiLine ())
67
81
68
82
def evaluateForModeler (self , value , model ):
69
83
for i in list (model .inputs .values ()):
70
84
param = i .param
71
- if isinstance (param , ParameterRaster ):
85
+ if isinstance (param , QgsProcessingParameterRasterLayer ):
72
86
new = "{}@" .format (os .path .basename (param .value ))
73
87
old = "{}@" .format (param .name ())
74
88
value = value .replace (old , new )
75
89
76
90
for alg in list (model .algs .values ()):
77
91
for out in alg .algorithm .outputs :
78
- if isinstance (out , OutputRaster ):
92
+ if isinstance (out , QgsProcessingOutputRasterLayer ):
79
93
if out .value :
80
94
new = "{}@" .format (os .path .basename (out .value ))
81
95
old = "{}:{}@" .format (alg .modeler_name , out .name )
82
96
value = value .replace (old , new )
83
97
return value
84
98
85
99
self .addParameter (ParameterRasterCalculatorExpression (self .EXPRESSION , self .tr ('Expression' ),
86
- multiline = True ,
87
- metadata = { 'widget_wrapper' : ExpressionWidgetWrapper }))
88
- self .addParameter ( ParameterNumber ( self . CELLSIZE ,
89
- self . tr ( 'Cell size (use 0 or empty to set it automatically)' ) ,
90
- minValue = 0.0 , default = 0.0 , optional = True ))
91
- self .addParameter (ParameterExtent (self .EXTENT ,
92
- self .tr ('Output extent' ),
93
- optional = True ))
94
- self .addOutput ( OutputRaster (self .OUTPUT , self .tr ('Output' )))
100
+ multiLine = True ))
101
+ self . addParameter ( QgsProcessingParameterNumber ( self . CELLSIZE ,
102
+ self .tr ( 'Cell size (use 0 or empty to set it automatically)' ) ,
103
+ type = QgsProcessingParameterNumber . Double ,
104
+ minValue = 0.0 , defaultValue = 0.0 , optional = True ))
105
+ self .addParameter (QgsProcessingParameterExtent (self .EXTENT ,
106
+ self .tr ('Output extent' ),
107
+ optional = True ))
108
+ self .addParameter ( QgsProcessingParameterRasterDestination (self .OUTPUT , self .tr ('Output' )))
95
109
96
110
def name (self ):
97
111
return 'rastercalculator'
@@ -100,11 +114,10 @@ def displayName(self):
100
114
return self .tr ('Raster calculator' )
101
115
102
116
def processAlgorithm (self , parameters , context , feedback ):
103
- expression = self .getParameterValue ( self .EXPRESSION )
104
- layersValue = self .getParameterValue ( self .LAYERS )
117
+ expression = self .parameterAsString ( parameters , self .EXPRESSION , context )
118
+ layers = self .parameterAsLayerList ( parameters , self .LAYERS , context )
105
119
layersDict = {}
106
- if layersValue :
107
- layers = [QgsProcessingUtils .mapLayerFromString (f , context ) for f in layersValue .split (";" )]
120
+ if layers :
108
121
layersDict = {os .path .basename (lyr .source ().split ("." )[0 ]): lyr for lyr in layers }
109
122
110
123
for lyr in QgsProcessingUtils .compatibleRasterLayers (context .project ()):
@@ -121,26 +134,22 @@ def processAlgorithm(self, parameters, context, feedback):
121
134
entry .bandNumber = n + 1
122
135
entries .append (entry )
123
136
124
- output = self .getOutputValue ( self .OUTPUT )
125
- extentValue = self .getParameterValue ( self .EXTENT )
126
- if not extentValue :
127
- extentValue = QgsProcessingUtils .combineLayerExtents (layersValue )
137
+ output = self .parameterAsOutputLayer ( parameters , self .OUTPUT , context )
138
+ bbox = self .parameterAsExtent ( parameters , self .EXTENT , context )
139
+ if bbox . isNull () :
140
+ bbox = QgsProcessingUtils .combineLayerExtents (layers )
128
141
129
- if extentValue :
130
- extent = extentValue .split (',' )
131
- bbox = QgsRectangle (float (extent [0 ]), float (extent [2 ]),
132
- float (extent [1 ]), float (extent [3 ]))
133
- else :
142
+ if bbox .isNull ():
134
143
if layersDict :
135
144
bbox = list (layersDict .values ())[0 ].extent ()
136
145
for lyr in layersDict .values ():
137
146
bbox .combineExtentWith (lyr .extent ())
138
147
else :
139
- raise GeoAlgorithmExecutionException (self .tr ("No layers selected" ))
148
+ raise QgsProcessingException (self .tr ("No layers selected" ))
140
149
141
150
def _cellsize (layer ):
142
151
return (layer .extent ().xMaximum () - layer .extent ().xMinimum ()) / layer .width ()
143
- cellsize = self .getParameterValue ( self .CELLSIZE ) or min ([_cellsize (lyr ) for lyr in layersDict .values ()])
152
+ cellsize = self .parameterAsDouble ( parameters , self .CELLSIZE , context ) or min ([_cellsize (lyr ) for lyr in layersDict .values ()])
144
153
width = math .floor ((bbox .xMaximum () - bbox .xMinimum ()) / cellsize )
145
154
height = math .floor ((bbox .yMaximum () - bbox .yMinimum ()) / cellsize )
146
155
driverName = GdalUtils .getFormatShortNameFromFilename (output )
@@ -154,14 +163,16 @@ def _cellsize(layer):
154
163
155
164
res = calc .processCalculation ()
156
165
if res == QgsRasterCalculator .ParserError :
157
- raise GeoAlgorithmExecutionException (self .tr ("Error parsing formula" ))
166
+ raise QgsProcessingException (self .tr ("Error parsing formula" ))
167
+
168
+ return {self .OUTPUT : output }
158
169
159
170
def processBeforeAddingToModeler (self , algorithm , model ):
160
171
values = []
161
172
expression = algorithm .params [self .EXPRESSION ]
162
173
for i in list (model .inputs .values ()):
163
174
param = i .param
164
- if isinstance (param , ParameterRaster ) and "{}@" .format (param .name ) in expression :
175
+ if isinstance (param , QgsProcessingParameterRasterLayer ) and "{}@" .format (param .name ) in expression :
165
176
values .append (ValueFromInput (param .name ()))
166
177
167
178
if algorithm .name :
@@ -171,7 +182,7 @@ def processBeforeAddingToModeler(self, algorithm, model):
171
182
for alg in list (model .algs .values ()):
172
183
if alg .modeler_name not in dependent :
173
184
for out in alg .algorithm .outputs :
174
- if (isinstance (out , OutputRaster ) and
185
+ if (isinstance (out , QgsProcessingOutputRasterLayer ) and
175
186
"{}:{}@" .format (alg .modeler_name , out .name ) in expression ):
176
187
values .append (ValueFromOutput (alg .modeler_name , out .name ))
177
188
0 commit comments