Saturday 24 September 2016

qgis 3 - Calling the correct element of an output via PyQGIS Script


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:\\Users\\ct279359\\Desktop\\Styles\\Roads.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:\\Users\\ct279359\\Desktop\\Styles\\Roads.qml")
self.source.triggerRepaint()

return results


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...