30
30
from qgis .PyQt .QtGui import QIcon , QColor
31
31
32
32
from qgis .analysis import QgsRelief
33
- from qgis .core import QgsProcessingParameterDefinition
33
+ from qgis .core import (QgsProcessingParameterDefinition ,
34
+ QgsProcessingParameterRasterLayer ,
35
+ QgsProcessingParameterNumber ,
36
+ QgsProcessingParameterBoolean ,
37
+ QgsProcessingParameterRasterDestination ,
38
+ QgsProcessingParameterFileDestination ,
39
+ QgsRasterFileWriter ,
40
+ QgsProcessingException )
34
41
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
35
- from processing .core .parameters import (Parameter ,
36
- ParameterRaster ,
37
- ParameterNumber ,
38
- ParameterBoolean ,
39
- _splitParameterOptions )
40
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
41
- from processing .core .outputs import OutputRaster , OutputTable
42
- from processing .tools import raster
42
+ from processing .tools .dataobjects import exportRasterLayer
43
43
44
44
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
45
45
46
46
47
+ class ParameterReliefColors (QgsProcessingParameterDefinition ):
48
+
49
+ def __init__ (self , name = '' , description = '' , parent = None , optional = True ):
50
+ super ().__init__ (name , description , None , optional )
51
+ self .parent = parent
52
+ self .setMetadata ({'widget_wrapper' : 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper' })
53
+
54
+ def type (self ):
55
+ return 'relief_colors'
56
+
57
+ def clone (self ):
58
+ return ParameterReliefColors (self .name (), self .description (), self .parent ,
59
+ self .flags () & QgsProcessingParameterDefinition .FlagOptional )
60
+
61
+ @staticmethod
62
+ def valueToColors (value ):
63
+ if value is None :
64
+ return None
65
+
66
+ if value == '' :
67
+ return None
68
+
69
+ if isinstance (value , str ):
70
+ return value .split (';' )
71
+ else :
72
+ return ParameterReliefColors .colorsToString (value )
73
+
74
+ @staticmethod
75
+ def colorsToString (colors ):
76
+ s = ''
77
+ for c in colors :
78
+ s += '{:f}, {:f}, {:d}, {:d}, {:d};' .format (c [0 ],
79
+ c [1 ],
80
+ c [2 ],
81
+ c [3 ],
82
+ c [4 ])
83
+ return s [:- 1 ]
84
+
85
+
47
86
class Relief (QgisAlgorithm ):
48
87
49
- INPUT_LAYER = 'INPUT_LAYER '
88
+ INPUT = 'INPUT '
50
89
Z_FACTOR = 'Z_FACTOR'
51
90
AUTO_COLORS = 'AUTO_COLORS'
52
91
COLORS = 'COLORS'
53
- OUTPUT_LAYER = 'OUTPUT_LAYER '
92
+ OUTPUT = 'OUTPUT '
54
93
FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION'
55
94
56
95
def icon (self ):
@@ -63,74 +102,22 @@ def __init__(self):
63
102
super ().__init__ ()
64
103
65
104
def initAlgorithm (self , config = None ):
66
- class ParameterReliefColors (Parameter ):
67
- default_metadata = {
68
- 'widget_wrapper' : 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper'
69
- }
70
-
71
- def __init__ (self , name = '' , description = '' , parent = None , optional = True ):
72
- Parameter .__init__ (self , name , description , None , optional )
73
- self .parent = parent
74
-
75
- def setValue (self , value ):
76
- if value is None :
77
- if not self .flags () & QgsProcessingParameterDefinition .FlagOptional :
78
- return False
79
- self .value = None
80
- return True
81
-
82
- if value == '' :
83
- if not self .flags () & QgsProcessingParameterDefinition .FlagOptional :
84
- return False
85
-
86
- if isinstance (value , str ):
87
- self .value = value if value != '' else None
88
- else :
89
- self .value = ParameterReliefColors .colorsToString (value )
90
- return True
91
-
92
- def getValueAsCommandLineParameter (self ):
93
- return '"{}"' .format (self .value )
94
-
95
- def getAsScriptCode (self ):
96
- param_type = ''
97
- param_type += 'relief colors '
98
- return '##' + self .name + '=' + param_type
99
-
100
- @classmethod
101
- def fromScriptCode (self , line ):
102
- isOptional , name , definition = _splitParameterOptions (line )
103
- descName = QgsProcessingParameters .descriptionFromName (name )
104
- parent = definition .lower ().strip ()[len ('relief colors' ) + 1 :]
105
- return ParameterReliefColors (name , descName , parent )
106
-
107
- @staticmethod
108
- def colorsToString (colors ):
109
- s = ''
110
- for c in colors :
111
- s += '{:f}, {:f}, {:d}, {:d}, {:d};' .format (c [0 ],
112
- c [1 ],
113
- c [2 ],
114
- c [3 ],
115
- c [4 ])
116
- return s [:- 1 ]
117
-
118
- self .addParameter (ParameterRaster (self .INPUT_LAYER ,
119
- self .tr ('Elevation layer' )))
120
- self .addParameter (ParameterNumber (self .Z_FACTOR ,
121
- self .tr ('Z factor' ),
122
- 1.0 , 999999.99 , 1.0 ))
123
- self .addParameter (ParameterBoolean (self .AUTO_COLORS ,
124
- self .tr ('Generate relief classes automatically' ),
125
- False ))
105
+ self .addParameter (QgsProcessingParameterRasterLayer (self .INPUT ,
106
+ self .tr ('Elevation layer' )))
107
+ self .addParameter (QgsProcessingParameterNumber (self .Z_FACTOR ,
108
+ self .tr ('Z factor' ), type = QgsProcessingParameterNumber .Double ,
109
+ minValue = 1.0 , maxValue = 999999.99 , defaultValue = 1.0 ))
110
+ self .addParameter (QgsProcessingParameterBoolean (self .AUTO_COLORS ,
111
+ self .tr ('Generate relief classes automatically' ),
112
+ defaultValue = False ))
126
113
self .addParameter (ParameterReliefColors (self .COLORS ,
127
114
self .tr ('Relief colors' ),
128
- self .INPUT_LAYER ,
115
+ self .INPUT ,
129
116
True ))
130
- self .addOutput ( OutputRaster (self .OUTPUT_LAYER ,
131
- self .tr ('Relief' )))
132
- self .addOutput ( OutputTable (self .FREQUENCY_DISTRIBUTION ,
133
- self .tr ('Frequency distribution' )))
117
+ self .addParameter ( QgsProcessingParameterRasterDestination (self .OUTPUT ,
118
+ self .tr ('Relief' )))
119
+ self .addParameter ( QgsProcessingParameterFileDestination (self .FREQUENCY_DISTRIBUTION ,
120
+ self .tr ('Frequency distribution' ), 'CSV files (*.csv)' , optional = True ))
134
121
135
122
def name (self ):
136
123
return 'relief'
@@ -139,26 +126,26 @@ def displayName(self):
139
126
return self .tr ('Relief' )
140
127
141
128
def processAlgorithm (self , parameters , context , feedback ):
142
- inputFile = self .getParameterValue (self .INPUT_LAYER )
143
- zFactor = self .getParameterValue (self .Z_FACTOR )
144
- automaticColors = self .getParameterValue (self .AUTO_COLORS )
145
- colors = self .getParameterValue (self .COLORS )
146
- outputFile = self .getOutputValue (self .OUTPUT_LAYER )
147
- frequencyDistribution = self .getOutputValue (self .FREQUENCY_DISTRIBUTION )
129
+ inputFile = exportRasterLayer (self .parameterAsRasterLayer (parameters , self .INPUT , context ))
130
+ zFactor = self .parameterAsDouble (parameters , self .Z_FACTOR , context )
131
+ automaticColors = self .parameterAsBool (parameters , self .AUTO_COLORS , context )
132
+ outputFile = self .parameterAsOutputLayer (parameters , self .OUTPUT , context )
133
+ frequencyDistribution = self .parameterAsFileOutput (parameters , self .FREQUENCY_DISTRIBUTION , context )
148
134
149
- outputFormat = raster . formatShortNameFromFileName ( outputFile )
135
+ outputFormat = QgsRasterFileWriter . driverForExtension ( os . path . splitext ( outputFile )[ 1 ] )
150
136
151
137
relief = QgsRelief (inputFile , outputFile , outputFormat )
152
138
153
139
if automaticColors :
154
140
reliefColors = relief .calculateOptimizedReliefClasses ()
155
141
else :
156
- if colors is None :
157
- raise GeoAlgorithmExecutionException (
142
+ colors = ParameterReliefColors .valueToColors (parameters [self .COLORS ])
143
+ if colors is None or len (colors ) == 0 :
144
+ raise QgsProcessingException (
158
145
self .tr ('Specify relief colors or activate "Generate relief classes automatically" option.' ))
159
146
160
147
reliefColors = []
161
- for c in colors . split ( ';' ) :
148
+ for c in colors :
162
149
v = c .split (',' )
163
150
color = QgsRelief .ReliefColor (QColor (int (v [2 ]), int (v [3 ]), int (v [4 ])),
164
151
float (v [0 ]),
@@ -169,3 +156,5 @@ def processAlgorithm(self, parameters, context, feedback):
169
156
relief .setZFactor (zFactor )
170
157
relief .exportFrequencyDistributionToCsv (frequencyDistribution )
171
158
relief .processRaster (None )
159
+
160
+ return {self .OUTPUT : outputFile , self .FREQUENCY_DISTRIBUTION : frequencyDistribution }
0 commit comments