3030from qgis .PyQt .QtGui import QIcon , QColor
3131
3232from 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 )
3441from 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
4343
4444pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
4545
4646
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+
4786class Relief (QgisAlgorithm ):
4887
49- INPUT_LAYER = 'INPUT_LAYER '
88+ INPUT = 'INPUT '
5089 Z_FACTOR = 'Z_FACTOR'
5190 AUTO_COLORS = 'AUTO_COLORS'
5291 COLORS = 'COLORS'
53- OUTPUT_LAYER = 'OUTPUT_LAYER '
92+ OUTPUT = 'OUTPUT '
5493 FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION'
5594
5695 def icon (self ):
@@ -63,74 +102,22 @@ def __init__(self):
63102 super ().__init__ ()
64103
65104 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 ))
126113 self .addParameter (ParameterReliefColors (self .COLORS ,
127114 self .tr ('Relief colors' ),
128- self .INPUT_LAYER ,
115+ self .INPUT ,
129116 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 ))
134121
135122 def name (self ):
136123 return 'relief'
@@ -139,26 +126,26 @@ def displayName(self):
139126 return self .tr ('Relief' )
140127
141128 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 )
148134
149- outputFormat = raster . formatShortNameFromFileName ( outputFile )
135+ outputFormat = QgsRasterFileWriter . driverForExtension ( os . path . splitext ( outputFile )[ 1 ] )
150136
151137 relief = QgsRelief (inputFile , outputFile , outputFormat )
152138
153139 if automaticColors :
154140 reliefColors = relief .calculateOptimizedReliefClasses ()
155141 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 (
158145 self .tr ('Specify relief colors or activate "Generate relief classes automatically" option.' ))
159146
160147 reliefColors = []
161- for c in colors . split ( ';' ) :
148+ for c in colors :
162149 v = c .split (',' )
163150 color = QgsRelief .ReliefColor (QColor (int (v [2 ]), int (v [3 ]), int (v [4 ])),
164151 float (v [0 ]),
@@ -169,3 +156,5 @@ def processAlgorithm(self, parameters, context, feedback):
169156 relief .setZFactor (zFactor )
170157 relief .exportFrequencyDistributionToCsv (frequencyDistribution )
171158 relief .processRaster (None )
159+
160+ return {self .OUTPUT : outputFile , self .FREQUENCY_DISTRIBUTION : frequencyDistribution }
0 commit comments