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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -192,7 +192,6 @@ def getAlgs(self):
# SpatialJoin(),
# GeometryConvert(), FieldsCalculator(),
# FieldsPyculator(),
# SetRasterStyle(),
# FieldsMapper(), SelectByAttributeSum(), Datasources2Vrt(),
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
Expand Down Expand Up @@ -289,6 +288,7 @@ def getAlgs(self):
ServiceAreaFromLayer(),
ServiceAreaFromPoint(),
SetMValue(),
SetRasterStyle(),
SetVectorStyle(),
SetZValue(),
ShortestPathLayerToPoint(),
Expand Down
44 changes: 17 additions & 27 deletions python/plugins/processing/algs/qgis/SetRasterStyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
Expand All @@ -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.