Skip to content
Permalink
Browse files

Port gdal color relief to new API

  • Loading branch information
nyalldawson committed Aug 13, 2017
1 parent 1cbbbc4 commit f9750dde52796445c41f2ed5074c4bdb089d760f
@@ -41,7 +41,6 @@


class AssignProjection(GdalAlgorithm):

INPUT = 'INPUT'
CRS = 'CRS'
OUTPUT = 'OUTPUT'
@@ -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')))

@@ -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


@@ -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'
@@ -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:
@@ -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

@@ -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()

@@ -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()
@@ -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('')
@@ -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
@@ -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
@@ -144,6 +144,7 @@ def loadAlgorithms(self):
# information(),
AssignProjection(),
aspect(),
ColorRelief(),
tri(),
warp(),
# translate(),
@@ -164,10 +165,8 @@ def loadAlgorithms(self):
# gdal2xyz(),
# hillshade(),
# slope(),
#
# tpi(),
# roughness(),
# ColorRelief(),
# GridInvDist(),
# GridAverage(),
# GridNearest(),
@@ -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:

0 comments on commit f9750dd

Please sign in to comment.
You can’t perform that action at this time.