Skip to content
Permalink
Browse files

Port set raster style alg to new API

  • Loading branch information
nyalldawson committed Aug 18, 2017
1 parent 387e049 commit 0a4a7acf5842ff7de8c22c4db8d46057e7195f9b
@@ -128,6 +128,7 @@
from .ServiceAreaFromLayer import ServiceAreaFromLayer
from .ServiceAreaFromPoint import ServiceAreaFromPoint
from .SetMValue import SetMValue
from .SetRasterStyle import SetRasterStyle
from .SetVectorStyle import SetVectorStyle
from .SetZValue import SetZValue
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
@@ -162,7 +163,6 @@
# from .GeometryConvert import GeometryConvert
# from .FieldsCalculator import FieldsCalculator
# from .FieldPyculator import FieldsPyculator
# from .SetRasterStyle import SetRasterStyle
# from .SelectByAttributeSum import SelectByAttributeSum
# from .Datasources2Vrt import Datasources2Vrt
# from .DefineProjection import DefineProjection
@@ -192,7 +192,6 @@ def getAlgs(self):
# SpatialJoin(),
# GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(),
# SetRasterStyle(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
@@ -289,6 +288,7 @@ def getAlgs(self):
ServiceAreaFromLayer(),
ServiceAreaFromPoint(),
SetMValue(),
SetRasterStyle(),
SetVectorStyle(),
SetZValue(),
ShortestPathLayerToPoint(),
@@ -29,13 +29,10 @@

from qgis.PyQt.QtXml import QDomDocument

from qgis.core import (QgsApplication,
QgsProcessingUtils)
from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingParameterFile,
QgsProcessingOutputRasterLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterRaster
from processing.core.outputs import OutputRaster
from processing.tools import dataobjects


class SetRasterStyle(QgisAlgorithm):
@@ -51,11 +48,11 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterRaster(self.INPUT,
self.tr('Raster layer')))
self.addParameter(ParameterFile(self.STYLE,
self.tr('Style file'), False, False, 'qml'))
self.addOutput(OutputRaster(self.OUTPUT, self.tr('Styled'), True))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Raster layer')))
self.addParameter(QgsProcessingParameterFile(self.STYLE,
self.tr('Style file'), extension='qml'))
self.addOutput(QgsProcessingOutputRasterLayer(self.INPUT, self.tr('Styled')))

def name(self):
return 'setstyleforrasterlayer'
@@ -64,19 +61,12 @@ def displayName(self):
return self.tr('Set style for raster layer')

def processAlgorithm(self, parameters, context, feedback):
filename = self.getParameterValue(self.INPUT)
layer = QgsProcessingUtils.mapLayerFromString(filename, context)

style = self.getParameterValue(self.STYLE)
if layer is None:
dataobjects.load(filename, os.path.basename(filename), style=style)
else:
with open(style) as f:
xml = "".join(f.readlines())
d = QDomDocument()
d.setContent(xml)
n = d.firstChild()
layer.readSymbology(n, '')
context.addLayerToLoadOnCompletion(self.getOutputFromName(self.OUTPUT).value)
self.setOutputValue(self.OUTPUT, filename)
layer.triggerRepaint()
layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
style = self.parameterAsFile(parameters, self.STYLE, context)
with open(style) as f:
xml = "".join(f.readlines())
d = QDomDocument()
d.setContent(xml)
layer.importNamedStyle(d)
layer.triggerRepaint()
return {self.INPUT: layer}

0 comments on commit 0a4a7ac

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