How can I import a .qml style from a file and apply it to a sink?
In QGIS 2.18, I used something like that script:
output=QgsVectorLayer ('Point?crs=EPSG:2154','output', 'memory')
path_style='./path/to/style.qml'
output.loadNamedStyle(path_style)
output.triggerRepaint()
But when I tried it on anita graser's example of QGIS3 script, .loadNamedStyle() wouldn't work on the sink OUTPUT:
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsField, QgsFeature, QgsFeatureSink, QgsFeatureRequest, QgsProcessing, QgsProcessingAlgorithm, QgsProcessingParameterFeatureSource, QgsProcessingParameterFeatureSink)
class ExAlgo(QgsProcessingAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
def __init__(self):
super().__init__()
def name(self):
return "exalgo"
def tr(self, text):
return QCoreApplication.translate("exalgo", text)
def createInstance(self):
return type(self)()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr("Input layer"),
[QgsProcessing.TypeVectorAnyGeometry]))
self.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr("Output layer"),
QgsProcessing.TypeVectorAnyGeometry))
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())
features = source.getFeatures(QgsFeatureRequest())
for feat in features:
out_feat = QgsFeature()
out_feat.setGeometry(feat.geometry())
out_feat.setAttributes(feat.attributes())
sink.addFeature(out_feat, QgsFeatureSink.FastInsert)
# --------------------the part that doesn't work:
path_style='./path/to/style.qml'
sink.loadNamedStyle(path_style)
sink.triggerRepaint()
#--------------------
return {self.OUTPUT: dest_id}
Is there a simple way to load and apply a style in the new QGIS Processing?
def postProcessAlgorithm
method, check this sample for add a style github.com/olivierdalang/travel_time_platform_plugin/blob/…