Geographic Information Systems Asked by Herbert Santos on August 25, 2020
I am trying to apply a style to the output file of a script that I´m running from the toolbox in QGIS 3.4.7. The last step is as described below.
def processAlgorithm(self, parameters, context, feedback):
if parameters['Input1'] is not None:
source = self.parameterAsSource(parameters,'Input1',context)
(sink, dest_id3) = self.parameterAsSink(parameters,'Output1',context,source.fields(),source.wkbType(),source.sourceCrs())
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()
for current, feature in enumerate(features):
sink.addFeature(feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
After that, I would like to apply a fixed style to the output file so that it will be displayed on Layers with the applied style. I reached the lines below:
self.source.loadNamedStyle(u"C:Usersct279359DesktopStylesRoads.qml")
self.source.triggerRepaint()
I got this error:
AttributeError: ‘InOut’ object has no attribute ‘source’
I would like to know the correct way to call the output file created in the first part.
I did some research on the subject Here, Here, Here, Here, Here and Here.
All explain ways to invoke the QML file or how to change the style of a file that is already in the Layers section. I need next to this, to invoke the correct output file.
————————————————————-
Full code here.
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterRasterDestination,
QgsVectorLayer,
QgsProject)
import processing
class InOut(QgsProcessingAlgorithm):
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return InOut()
def name(self):
return 'InOut2'
def displayName(self):
return self.tr('InOut2')
def group(self):
return self.tr('Auxiliares')
def groupId(self):
return 'auxiliares1'
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('Input1','Input1',optional=True, types=[QgsProcessing.TypeVector], defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Output1','Output1', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
def processAlgorithm(self, parameters, context, feedback):
if parameters['Input1'] is not None:
source = self.parameterAsSource(parameters,'Input1',context)
(sink, dest_id3) = self.parameterAsSink(parameters,'Output1',context,source.fields(),source.wkbType(),source.sourceCrs())
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()
for current, feature in enumerate(features):
sink.addFeature(feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
self.source.loadNamedStyle(u"C:Usersct279359DesktopStylesRoads.qml")
self.source.triggerRepaint()
return results
you need to turn the layer into a qgis vector Layer when importing it:
#define vector layer
vlayer = QgsVectorLayer('your_output_file', 'layer_name', "ogr")
# add the layer to the registry
QgsProject.instance().addMapLayer(vlayer)
# load style
vlayer.loadNamedStyle('path_to_qml.qml')
Answered by B-C B. on August 25, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP