Skip to content

Commit

Permalink
Port gdal color relief to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 13, 2017
1 parent 1cbbbc4 commit f9750dd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 78 deletions.
3 changes: 1 addition & 2 deletions python/plugins/processing/algs/gdal/AssignProjection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@


class AssignProjection(GdalAlgorithm):

INPUT = 'INPUT'
CRS = 'CRS'
OUTPUT = 'OUTPUT'
Expand All @@ -52,7 +51,7 @@ def __init__(self):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer'), optional=False))
self.addParameter(QgsProcessingParameterCrs(self.CRS,
self.tr('Desired CRS')))
self.tr('Desired CRS')))

self.addOutput(QgsProcessingOutputRasterLayer(self.OUTPUT, self.tr('Layer with projection')))

Expand Down
48 changes: 25 additions & 23 deletions python/plugins/processing/algs/gdal/ColorRelief.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterBand,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProcessingParameterFile,
QgsProcessingParameterRasterDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputRaster
from processing.algs.gdal.GdalUtils import GdalUtils


Expand All @@ -51,17 +51,17 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer')))
self.addParameter(ParameterNumber(
self.BAND, self.tr('Band number'), 1, 99, 1))
self.addParameter(ParameterBoolean(self.COMPUTE_EDGES,
self.tr('Compute edges'), False))
self.addParameter(ParameterFile(self.COLOR_TABLE,
self.tr('Color configuration file'), optional=False))
self.addParameter(ParameterSelection(self.MATCH_MODE,
self.tr('Matching mode'), self.MATCHING_MODES, 0))

self.addOutput(OutputRaster(self.OUTPUT, self.tr('Color relief')))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterBand(
self.BAND, self.tr('Band number'), parentLayerParameterName=self.INPUT))
self.addParameter(QgsProcessingParameterBoolean(self.COMPUTE_EDGES,
self.tr('Compute edges'), defaultValue=False))
self.addParameter(QgsProcessingParameterFile(self.COLOR_TABLE,
self.tr('Color configuration file'), optional=False))
self.addParameter(QgsProcessingParameterEnum(self.MATCH_MODE,
self.tr('Matching mode'), options=self.MATCHING_MODES, defaultValue=0))

self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Color relief')))

def name(self):
return 'colorrelief'
Expand All @@ -74,21 +74,23 @@ def group(self):

def getConsoleCommands(self, parameters, context, feedback):
arguments = ['color-relief']
arguments.append(str(self.getParameterValue(self.INPUT)))
arguments.append(str(self.getParameterValue(self.COLOR_TABLE)))
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
arguments.append(inLayer.source())
arguments.append(self.parameterAsFile(parameters, self.COLOR_TABLE, context))
#filePath = unicode(self.getParameterValue(self.COLOR_TABLE))
#if filePath is None or filePath == '':
# filePath = os.path.join(os.path.dirname(__file__), 'terrain.txt')
#arguments.append(filePath)
arguments.append(str(self.getOutputValue(self.OUTPUT)))
out = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
arguments.append(out)

arguments.append('-b')
arguments.append(str(self.getParameterValue(self.BAND)))
arguments.append(str(self.parameterAsInt(parameters, self.BAND, context)))

if self.getParameterValue(self.COMPUTE_EDGES):
if self.parameterAsBool(parameters, self.COMPUTE_EDGES, context):
arguments.append('-compute_edges')

mode = self.getParameterValue(self.MATCH_MODE)
mode = self.parameterAsEnum(parameters, self.MATCH_MODE, context)
if mode == 1:
arguments.append('-exact_color_entry')
elif mode == 2:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def processAlgorithm(self, parameters, context, feedback):
if o.name() in parameters:
results[o.name()] = parameters[o.name()]
for k, v in self.output_values.items():
results[k]=v
results[k] = v

return results

Expand Down
29 changes: 15 additions & 14 deletions python/plugins/processing/algs/gdal/GdalAlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self, alg):

self.runAsBatchButton = QPushButton(QCoreApplication.translate("AlgorithmDialog", "Run as Batch Process…"))
self.runAsBatchButton.clicked.connect(self.runAsBatch)
self.buttonBox.addButton(self.runAsBatchButton, QDialogButtonBox.ResetRole) # reset role to ensure left alignment
self.buttonBox.addButton(self.runAsBatchButton,
QDialogButtonBox.ResetRole) # reset role to ensure left alignment

self.mainWidget.parametersHaveChanged()

Expand Down Expand Up @@ -99,18 +100,18 @@ def connectParameterSignals(self):
self.connectWidgetChangedSignals(c)

def connectWidgetChangedSignals(self, w):
if isinstance(w, QLineEdit):
w.textChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QComboBox):
w.currentIndexChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QgsProjectionSelectionWidget):
w.crsChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QCheckBox):
w.stateChanged.connect(self.parametersHaveChanged)
elif isinstance(w, MultipleInputPanel):
w.selectionChanged.connect(self.parametersHaveChanged)
elif isinstance(w, NumberInputPanel):
w.hasChanged.connect(self.parametersHaveChanged)
if isinstance(w, QLineEdit):
w.textChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QComboBox):
w.currentIndexChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QgsProjectionSelectionWidget):
w.crsChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QCheckBox):
w.stateChanged.connect(self.parametersHaveChanged)
elif isinstance(w, MultipleInputPanel):
w.selectionChanged.connect(self.parametersHaveChanged)
elif isinstance(w, NumberInputPanel):
w.hasChanged.connect(self.parametersHaveChanged)

def parametersHaveChanged(self):
context = createContext()
Expand All @@ -121,7 +122,7 @@ def parametersHaveChanged(self):
if not output.name() in parameters or parameters[output.name()] is None:
parameters[output.name()] = self.tr("[temporary file]")
for p in self.alg.parameterDefinitions():
if ( not p.name() in parameters and not p.flags() & QgsProcessingParameterDefinition.FlagOptional) \
if (not p.name() in parameters and not p.flags() & QgsProcessingParameterDefinition.FlagOptional) \
or (not p.checkValueIsAcceptable(parameters[p.name()], context)):
# not ready yet
self.text.setPlainText('')
Expand Down
5 changes: 2 additions & 3 deletions python/plugins/processing/algs/gdal/GdalAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from .AssignProjection import AssignProjection
from .aspect import aspect
from .ColorRelief import ColorRelief
from .tri import tri
from .warp import warp
# from .nearblack import nearblack
Expand All @@ -59,7 +60,6 @@
# from .slope import slope
# from .tpi import tpi
# from .roughness import roughness
# from .ColorRelief import ColorRelief
# from .GridInvDist import GridInvDist
# from .GridAverage import GridAverage
# from .GridNearest import GridNearest
Expand Down Expand Up @@ -144,6 +144,7 @@ def loadAlgorithms(self):
# information(),
AssignProjection(),
aspect(),
ColorRelief(),
tri(),
warp(),
# translate(),
Expand All @@ -164,10 +165,8 @@ def loadAlgorithms(self):
# gdal2xyz(),
# hillshade(),
# slope(),
#
# tpi(),
# roughness(),
# ColorRelief(),
# GridInvDist(),
# GridAverage(),
# GridNearest(),
Expand Down
70 changes: 35 additions & 35 deletions python/plugins/processing/tests/testdata/gdal_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -547,41 +547,41 @@ tests:
# OUTPUT:
# hash: f468bb08cbede2b606018a7a45a51cc221307a8421a11daefabeac12
# type: rasterhash
#
# - algorithm: gdal:colorrelief
# name: Standard Color Relief
# params:
# BAND: 1
# COLOR_TABLE:
# name: custom/color_relief.txt
# type: file
# COMPUTE_EDGES: false
# INPUT:
# name: dem.tif
# type: raster
# MATCH_MODE: '0'
# results:
# OUTPUT:
# hash: f714597fadc9cfc3f5263dc0e35f7c6ba285de238dce439e4988faac
# type: rasterhash
#
# - algorithm: gdal:colorrelief
# name: Color Relief with edges
# params:
# BAND: 1
# COLOR_TABLE:
# name: custom/color_relief.txt
# type: file
# COMPUTE_EDGES: true
# INPUT:
# name: dem.tif
# type: raster
# MATCH_MODE: '0'
# results:
# OUTPUT:
# hash: f714597fadc9cfc3f5263dc0e35f7c6ba285de238dce439e4988faac
# type: rasterhash
#

- algorithm: gdal:colorrelief
name: Standard Color Relief
params:
BAND: 1
COLOR_TABLE:
name: custom/color_relief.txt
type: file
COMPUTE_EDGES: false
INPUT:
name: dem.tif
type: raster
MATCH_MODE: '0'
results:
OUTPUT:
hash: f714597fadc9cfc3f5263dc0e35f7c6ba285de238dce439e4988faac
type: rasterhash

- algorithm: gdal:colorrelief
name: Color Relief with edges
params:
BAND: 1
COLOR_TABLE:
name: custom/color_relief.txt
type: file
COMPUTE_EDGES: true
INPUT:
name: dem.tif
type: raster
MATCH_MODE: '0'
results:
OUTPUT:
hash: f714597fadc9cfc3f5263dc0e35f7c6ba285de238dce439e4988faac
type: rasterhash

# - algorithm: gdal:merge
# name: Merge 2 raster layers
# params:
Expand Down

0 comments on commit f9750dd

Please sign in to comment.