From 4e59c5a045580e2c5c53aa539e00ca4b9dda1071 Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Sat, 17 Dec 2016 14:58:58 +0200 Subject: [PATCH] [processing] merge two Relief algorithms into one --- .../algs/qgis/QGISAlgorithmProvider.py | 3 +- python/plugins/processing/algs/qgis/Relief.py | 50 +++++++++++++------ .../tests/testdata/qgis_algorithm_tests.yaml | 6 ++- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index b42b559075f..828e366e584 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -166,7 +166,6 @@ from .Aspect import Aspect 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 @@ -248,7 +247,7 @@ class QGISAlgorithmProvider(AlgorithmProvider): OffsetLine(), PolygonCentroids(), Translate(), SingleSidedBuffer(), PointsAlongGeometry(), Aspect(), Slope(), Ruggedness(), Hillshade(), - ReliefAuto(), Relief(), ZonalStatisticsQgis(), + Relief(), ZonalStatisticsQgis(), IdwInterpolationZValue(), IdwInterpolationAttribute(), TinInterpolationZValue(), TinInterpolationAttribute(), RemoveNullGeometry(), ExtractByExpression(), diff --git a/python/plugins/processing/algs/qgis/Relief.py b/python/plugins/processing/algs/qgis/Relief.py index a110d7a9fd4..c7e4e5b80f0 100644 --- a/python/plugins/processing/algs/qgis/Relief.py +++ b/python/plugins/processing/algs/qgis/Relief.py @@ -35,6 +35,7 @@ from processing.core.GeoAlgorithm import GeoAlgorithm from processing.core.parameters import (Parameter, ParameterRaster, ParameterNumber, + ParameterBoolean, _splitParameterOptions) from processing.core.outputs import OutputRaster, OutputTable from processing.tools import raster @@ -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' @@ -62,16 +64,19 @@ class Relief(GeoAlgorithm): '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 @@ -105,9 +110,15 @@ class Relief(GeoAlgorithm): 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, @@ -116,21 +127,30 @@ class Relief(GeoAlgorithm): 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) diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index 7073fd07db6..26b2869ed67 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -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 @@ -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,