Skip to content

Commit

Permalink
Port set raster style alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 18, 2017
1 parent 387e049 commit 0a4a7ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -128,6 +128,7 @@
from .ServiceAreaFromLayer import ServiceAreaFromLayer from .ServiceAreaFromLayer import ServiceAreaFromLayer
from .ServiceAreaFromPoint import ServiceAreaFromPoint from .ServiceAreaFromPoint import ServiceAreaFromPoint
from .SetMValue import SetMValue from .SetMValue import SetMValue
from .SetRasterStyle import SetRasterStyle
from .SetVectorStyle import SetVectorStyle from .SetVectorStyle import SetVectorStyle
from .SetZValue import SetZValue from .SetZValue import SetZValue
from .ShortestPathLayerToPoint import ShortestPathLayerToPoint from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
Expand Down Expand Up @@ -162,7 +163,6 @@
# from .GeometryConvert import GeometryConvert # from .GeometryConvert import GeometryConvert
# from .FieldsCalculator import FieldsCalculator # from .FieldsCalculator import FieldsCalculator
# from .FieldPyculator import FieldsPyculator # from .FieldPyculator import FieldsPyculator
# from .SetRasterStyle import SetRasterStyle
# from .SelectByAttributeSum import SelectByAttributeSum # from .SelectByAttributeSum import SelectByAttributeSum
# from .Datasources2Vrt import Datasources2Vrt # from .Datasources2Vrt import Datasources2Vrt
# from .DefineProjection import DefineProjection # from .DefineProjection import DefineProjection
Expand Down Expand Up @@ -192,7 +192,6 @@ def getAlgs(self):
# SpatialJoin(), # SpatialJoin(),
# GeometryConvert(), FieldsCalculator(), # GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(), # FieldsPyculator(),
# SetRasterStyle(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(), # FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# DefineProjection(), # DefineProjection(),
# RectanglesOvalsDiamondsVariable(), # RectanglesOvalsDiamondsVariable(),
Expand Down Expand Up @@ -289,6 +288,7 @@ def getAlgs(self):
ServiceAreaFromLayer(), ServiceAreaFromLayer(),
ServiceAreaFromPoint(), ServiceAreaFromPoint(),
SetMValue(), SetMValue(),
SetRasterStyle(),
SetVectorStyle(), SetVectorStyle(),
SetZValue(), SetZValue(),
ShortestPathLayerToPoint(), ShortestPathLayerToPoint(),
Expand Down
44 changes: 17 additions & 27 deletions python/plugins/processing/algs/qgis/SetRasterStyle.py
Expand Up @@ -29,13 +29,10 @@


from qgis.PyQt.QtXml import QDomDocument from qgis.PyQt.QtXml import QDomDocument


from qgis.core import (QgsApplication, from qgis.core import (QgsProcessingParameterRasterLayer,
QgsProcessingUtils) QgsProcessingParameterFile,
QgsProcessingOutputRasterLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm 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): class SetRasterStyle(QgisAlgorithm):
Expand All @@ -51,11 +48,11 @@ def __init__(self):
super().__init__() super().__init__()


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


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


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

with open(style) as f:
style = self.getParameterValue(self.STYLE) xml = "".join(f.readlines())
if layer is None: d = QDomDocument()
dataobjects.load(filename, os.path.basename(filename), style=style) d.setContent(xml)
else: layer.importNamedStyle(d)
with open(style) as f: layer.triggerRepaint()
xml = "".join(f.readlines()) return {self.INPUT: layer}
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()

0 comments on commit 0a4a7ac

Please sign in to comment.