Skip to content

Commit

Permalink
[processing] merge two Relief algorithms into one
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 19, 2016
1 parent 0d1c9a3 commit 4e59c5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -166,7 +166,6 @@
from .Slope import Slope
from .Ruggedness import Ruggedness
from .Hillshade import Hillshade
from .ReliefAuto import ReliefAuto
from .Relief import Relief
from .IdwInterpolationZValue import IdwInterpolationZValue
from .IdwInterpolationAttribute import IdwInterpolationAttribute
Expand Down Expand Up @@ -248,7 +247,7 @@ def __init__(self):
OffsetLine(), PolygonCentroids(), Translate(),
SingleSidedBuffer(), PointsAlongGeometry(),
Aspect(), Slope(), Ruggedness(), Hillshade(),
ReliefAuto(), Relief(), ZonalStatisticsQgis(),
Relief(), ZonalStatisticsQgis(),
IdwInterpolationZValue(), IdwInterpolationAttribute(),
TinInterpolationZValue(), TinInterpolationAttribute(),
RemoveNullGeometry(), ExtractByExpression(),
Expand Down
50 changes: 35 additions & 15 deletions python/plugins/processing/algs/qgis/Relief.py
Expand Up @@ -35,6 +35,7 @@
from processing.core.parameters import (Parameter,
ParameterRaster,
ParameterNumber,
ParameterBoolean,
_splitParameterOptions)
from processing.core.outputs import OutputRaster, OutputTable
from processing.tools import raster
Expand All @@ -46,6 +47,7 @@ class Relief(GeoAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
Z_FACTOR = 'Z_FACTOR'
AUTO_COLORS = 'AUTO_COLORS'
COLORS = 'COLORS'
OUTPUT_LAYER = 'OUTPUT_LAYER'
FREQUENCY_DISTRIBUTION = 'FREQUENCY_DISTRIBUTION'
Expand All @@ -62,16 +64,19 @@ class ParameterReliefColors(Parameter):
'widget_wrapper': 'processing.algs.qgis.ui.ReliefColorsWidget.ReliefColorsWidgetWrapper'
}

def __init__(self, name='', description='', parent=None):
Parameter.__init__(self, name, description)
def __init__(self, name='', description='', parent=None, optional=True):
Parameter.__init__(self, name, description, None, optional)
self.parent = parent

def setValue(self, value):
if value is None:
return False
if not self.optional:
return False
self.value = None
return True

if isinstance(value, str):
self.value = value
self.value = value if value != '' else None
else:
self.value = ParameterReliefColors.colorsToString(value)
return True
Expand Down Expand Up @@ -105,9 +110,15 @@ def colorsToString(colors):
self.addParameter(ParameterRaster(self.INPUT_LAYER,
self.tr('Elevation layer')))
self.addParameter(ParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), 1.0, 999999.99, 1.0))
self.tr('Z factor'),
1.0, 999999.99, 1.0))
self.addParameter(ParameterBoolean(self.AUTO_COLORS,
self.tr('Generate relief classes automaticaly'),
False))
self.addParameter(ParameterReliefColors(self.COLORS,
self.tr('Relief colors'), self.INPUT_LAYER))
self.tr('Relief colors'),
self.INPUT_LAYER,
True))
self.addOutput(OutputRaster(self.OUTPUT_LAYER,
self.tr('Relief')))
self.addOutput(OutputTable(self.FREQUENCY_DISTRIBUTION,
Expand All @@ -116,21 +127,30 @@ def colorsToString(colors):
def processAlgorithm(self, progress):
inputFile = self.getParameterValue(self.INPUT_LAYER)
zFactor = self.getParameterValue(self.Z_FACTOR)
colors = self.getParameterValue(self.COLORS).split(';')
automaticColors = self.getParameterValue(self.AUTO_COLORS)
colors = self.getParameterValue(self.COLORS)
outputFile = self.getOutputValue(self.OUTPUT_LAYER)
frequencyDistribution = self.getOutputValue(self.FREQUENCY_DISTRIBUTION)

outputFormat = raster.formatShortNameFromFileName(outputFile)

reliefColors = []
for c in colors:
v = c.split(',')
color = QgsRelief.ReliefColor(QColor(int(v[2]), int(v[3]), int(v[4])),
float(v[0]),
float(v[1]))
reliefColors.append(color)

relief = QgsRelief(inputFile, outputFile, outputFormat)

if automaticColors:
reliefColors = relief.calculateOptimizedReliefClasses()
else:
if colors is None:
raise GeoAlgorithmExecutionException(
self.tr('Specify relief colors or activate "Generate relief classes automaticaly" option.'))

reliefColors = []
for c in colors.split(';'):
v = c.split(',')
color = QgsRelief.ReliefColor(QColor(int(v[2]), int(v[3]), int(v[4])),
float(v[0]),
float(v[1]))
reliefColors.append(color)

relief.setReliefColors(reliefColors)
relief.setZFactor(zFactor)
relief.exportFrequencyDistributionToCsv(frequencyDistribution)
Expand Down
Expand Up @@ -1136,9 +1136,10 @@ tests:
hash: 58365b3715b925d6286e7f082ebd9c2a20f09fa1c922176d3f238002
type: rasterhash

- algorithm: qgis:reliefautomaticcolors
name: Relief (automatic colors calculation)
- algorithm: qgis:relief
name: Relief (automatic colors generation)
params:
AUTO_COLORS: true
INPUT_LAYER:
name: dem.tif
type: raster
Expand All @@ -1151,6 +1152,7 @@ tests:
- algorithm: qgis:relief
name: Relief (custom colors)
params:
AUTO_COLORS: false
COLORS: 85.00, 104.44, 7, 165, 144;104.44, 104.44, 12, 221, 162;104.44, 104.44,
33, 252, 183;104.44, 104.44, 247, 252, 152;104.44, 104.44, 252, 196, 8;104.44,
190.33, 252, 166, 15;190.33, 226.70, 175, 101, 15;226.70, 226.70, 255, 133,
Expand Down

0 comments on commit 4e59c5a

Please sign in to comment.