3535from processing .core .parameters import (Parameter ,
3636 ParameterRaster ,
3737 ParameterNumber ,
38+ ParameterBoolean ,
3839 _splitParameterOptions )
3940from processing .core .outputs import OutputRaster , OutputTable
4041from processing .tools import raster
@@ -46,6 +47,7 @@ class Relief(GeoAlgorithm):
4647
4748 INPUT_LAYER = 'INPUT_LAYER'
4849 Z_FACTOR = 'Z_FACTOR'
50+ AUTO_COLORS = 'AUTO_COLORS'
4951 COLORS = 'COLORS'
5052 OUTPUT_LAYER = 'OUTPUT_LAYER'
5153 FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION'
@@ -62,16 +64,19 @@ class ParameterReliefColors(Parameter):
6264 'widget_wrapper' : 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper'
6365 }
6466
65- def __init__ (self , name = '' , description = '' , parent = None ):
66- Parameter .__init__ (self , name , description )
67+ def __init__ (self , name = '' , description = '' , parent = None , optional = True ):
68+ Parameter .__init__ (self , name , description , None , optional )
6769 self .parent = parent
6870
6971 def setValue (self , value ):
7072 if value is None :
71- return False
73+ if not self .optional :
74+ return False
75+ self .value = None
76+ return True
7277
7378 if isinstance (value , str ):
74- self .value = value
79+ self .value = value if value != '' else None
7580 else :
7681 self .value = ParameterReliefColors .colorsToString (value )
7782 return True
@@ -105,9 +110,15 @@ def colorsToString(colors):
105110 self .addParameter (ParameterRaster (self .INPUT_LAYER ,
106111 self .tr ('Elevation layer' )))
107112 self .addParameter (ParameterNumber (self .Z_FACTOR ,
108- self .tr ('Z factor' ), 1.0 , 999999.99 , 1.0 ))
113+ self .tr ('Z factor' ),
114+ 1.0 , 999999.99 , 1.0 ))
115+ self .addParameter (ParameterBoolean (self .AUTO_COLORS ,
116+ self .tr ('Generate relief classes automaticaly' ),
117+ False ))
109118 self .addParameter (ParameterReliefColors (self .COLORS ,
110- self .tr ('Relief colors' ), self .INPUT_LAYER ))
119+ self .tr ('Relief colors' ),
120+ self .INPUT_LAYER ,
121+ True ))
111122 self .addOutput (OutputRaster (self .OUTPUT_LAYER ,
112123 self .tr ('Relief' )))
113124 self .addOutput (OutputTable (self .FREQUENCY_DISTRIBUTION ,
@@ -116,21 +127,30 @@ def colorsToString(colors):
116127 def processAlgorithm (self , progress ):
117128 inputFile = self .getParameterValue (self .INPUT_LAYER )
118129 zFactor = self .getParameterValue (self .Z_FACTOR )
119- colors = self .getParameterValue (self .COLORS ).split (';' )
130+ automaticColors = self .getParameterValue (self .AUTO_COLORS )
131+ colors = self .getParameterValue (self .COLORS )
120132 outputFile = self .getOutputValue (self .OUTPUT_LAYER )
121133 frequencyDistribution = self .getOutputValue (self .FREQUENCY_DISTRIBUTION )
122134
123135 outputFormat = raster .formatShortNameFromFileName (outputFile )
124136
125- reliefColors = []
126- for c in colors :
127- v = c .split (',' )
128- color = QgsRelief .ReliefColor (QColor (int (v [2 ]), int (v [3 ]), int (v [4 ])),
129- float (v [0 ]),
130- float (v [1 ]))
131- reliefColors .append (color )
132-
133137 relief = QgsRelief (inputFile , outputFile , outputFormat )
138+
139+ if automaticColors :
140+ reliefColors = relief .calculateOptimizedReliefClasses ()
141+ else :
142+ if colors is None :
143+ raise GeoAlgorithmExecutionException (
144+ self .tr ('Specify relief colors or activate "Generate relief classes automaticaly" option.' ))
145+
146+ reliefColors = []
147+ for c in colors .split (';' ):
148+ v = c .split (',' )
149+ color = QgsRelief .ReliefColor (QColor (int (v [2 ]), int (v [3 ]), int (v [4 ])),
150+ float (v [0 ]),
151+ float (v [1 ]))
152+ reliefColors .append (color )
153+
134154 relief .setReliefColors (reliefColors )
135155 relief .setZFactor (zFactor )
136156 relief .exportFrequencyDistributionToCsv (frequencyDistribution )
0 commit comments