Skip to content

Commit 4e59c5a

Browse files
committed
[processing] merge two Relief algorithms into one
1 parent 0d1c9a3 commit 4e59c5a

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@
166166
from .Slope import Slope
167167
from .Ruggedness import Ruggedness
168168
from .Hillshade import Hillshade
169-
from .ReliefAuto import ReliefAuto
170169
from .Relief import Relief
171170
from .IdwInterpolationZValue import IdwInterpolationZValue
172171
from .IdwInterpolationAttribute import IdwInterpolationAttribute
@@ -248,7 +247,7 @@ def __init__(self):
248247
OffsetLine(), PolygonCentroids(), Translate(),
249248
SingleSidedBuffer(), PointsAlongGeometry(),
250249
Aspect(), Slope(), Ruggedness(), Hillshade(),
251-
ReliefAuto(), Relief(), ZonalStatisticsQgis(),
250+
Relief(), ZonalStatisticsQgis(),
252251
IdwInterpolationZValue(), IdwInterpolationAttribute(),
253252
TinInterpolationZValue(), TinInterpolationAttribute(),
254253
RemoveNullGeometry(), ExtractByExpression(),

python/plugins/processing/algs/qgis/Relief.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from processing.core.parameters import (Parameter,
3636
ParameterRaster,
3737
ParameterNumber,
38+
ParameterBoolean,
3839
_splitParameterOptions)
3940
from processing.core.outputs import OutputRaster, OutputTable
4041
from 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)

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,10 @@ tests:
11361136
hash: 58365b3715b925d6286e7f082ebd9c2a20f09fa1c922176d3f238002
11371137
type: rasterhash
11381138

1139-
- algorithm: qgis:reliefautomaticcolors
1140-
name: Relief (automatic colors calculation)
1139+
- algorithm: qgis:relief
1140+
name: Relief (automatic colors generation)
11411141
params:
1142+
AUTO_COLORS: true
11421143
INPUT_LAYER:
11431144
name: dem.tif
11441145
type: raster
@@ -1151,6 +1152,7 @@ tests:
11511152
- algorithm: qgis:relief
11521153
name: Relief (custom colors)
11531154
params:
1155+
AUTO_COLORS: false
11541156
COLORS: 85.00, 104.44, 7, 165, 144;104.44, 104.44, 12, 221, 162;104.44, 104.44,
11551157
33, 252, 183;104.44, 104.44, 247, 252, 152;104.44, 104.44, 252, 196, 8;104.44,
11561158
190.33, 252, 166, 15;190.33, 226.70, 175, 101, 15;226.70, 226.70, 255, 133,

0 commit comments

Comments
 (0)