SetRasterStyle.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. ***************************************************************************
  3. SetRasterStyle.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. from qgis.PyQt.QtXml import QDomDocument
  22. from qgis.core import (QgsProcessingAlgorithm,
  23. QgsProcessingParameterRasterLayer,
  24. QgsProcessingParameterFile,
  25. QgsProcessingOutputRasterLayer)
  26. from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
  27. class SetRasterStyle(QgisAlgorithm):
  28. INPUT = 'INPUT'
  29. STYLE = 'STYLE'
  30. OUTPUT = 'OUTPUT'
  31. def group(self):
  32. return self.tr('Raster tools')
  33. def groupId(self):
  34. return 'rastertools'
  35. def __init__(self):
  36. super().__init__()
  37. def flags(self):
  38. return super().flags() | QgsProcessingAlgorithm.FlagNoThreading | QgsProcessingAlgorithm.FlagDeprecated | QgsProcessingAlgorithm.FlagNotAvailableInStandaloneTool
  39. def initAlgorithm(self, config=None):
  40. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
  41. self.tr('Raster layer')))
  42. self.addParameter(QgsProcessingParameterFile(self.STYLE,
  43. self.tr('Style file'), extension='qml'))
  44. self.addOutput(QgsProcessingOutputRasterLayer(self.INPUT, self.tr('Styled')))
  45. def name(self):
  46. return 'setstyleforrasterlayer'
  47. def displayName(self):
  48. return self.tr('Set style for raster layer')
  49. def processAlgorithm(self, parameters, context, feedback):
  50. layer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  51. style = self.parameterAsFile(parameters, self.STYLE, context)
  52. with open(style) as f:
  53. xml = "".join(f.readlines())
  54. d = QDomDocument()
  55. d.setContent(xml)
  56. layer.importNamedStyle(d)
  57. layer.triggerRepaint()
  58. return {self.INPUT: layer}