2014-06-15 15:26:46 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2017-09-08 20:34:52 +10:00
|
|
|
SetRasterStyle.py
|
2014-06-15 15:26:46 +02:00
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
|
2014-06-20 12:12:31 +02:00
|
|
|
import os
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtXml import QDomDocument
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
|
2018-01-29 12:21:41 +10:00
|
|
|
from qgis.core import (QgsProcessingAlgorithm,
|
|
|
|
QgsProcessingParameterRasterLayer,
|
2017-08-19 00:30:32 +10:00
|
|
|
QgsProcessingParameterFile,
|
|
|
|
QgsProcessingOutputRasterLayer)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-06-15 15:26:46 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class SetRasterStyle(QgisAlgorithm):
|
2014-06-15 15:26:46 +02:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
2014-06-18 23:59:46 +02:00
|
|
|
STYLE = 'STYLE'
|
2014-06-15 15:26:46 +02:00
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Raster tools')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'rastertools'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
2018-01-29 12:21:41 +10:00
|
|
|
def flags(self):
|
|
|
|
return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
|
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2017-08-19 00:30:32 +10:00
|
|
|
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')))
|
2014-06-15 15:26:46 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'setstyleforrasterlayer'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Set style for raster layer')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-08-19 00:30:32 +10:00
|
|
|
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}
|