[Qgis-user] Piping processing algorithm with pyQGIS (QGIS3)

Yoann QUENACH yquenach at itlink.fr
Mon Mar 18 04:12:33 PDT 2019


Hello everyone,

Is there a simple way to pipe processing algorithms within a processing
script?
I know you can pipe algorithm using a model, but I'd like to pipe them
directly inside my custom algorithm

I know I can call processing.run from within the processAlgorithm function,
but I do not see how to use the result of this call as my custom algorithm
output.

When you create a new processing script from the template, here is what you
get

...
    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )
        self.addParameter(
            QgsProcessingParameterFeatureSink(
                self.OUTPUT,
                self.tr('Output layer')
            )
        )

    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

        source = self.parameterAsSource(
            parameters,
            self.INPUT,
            context
        )

        (sink, dest_id) = self.parameterAsSink(
            parameters,
            self.OUTPUT,
            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):
            if feedback.isCanceled():
                break
            sink.addFeature(feature, QgsFeatureSink.FastInsert)
            feedback.setProgress(int(current * total))

        if True:
            buffered_layer = processing.run("native:buffer", {
                'INPUT': dest_id,
                'DISTANCE': 1.5,
                'SEGMENTS': 5,
                'END_CAP_STYLE': 0,
                'JOIN_STYLE': 0,
                'MITER_LIMIT': 2,
                'DISSOLVE': False,
                'OUTPUT': 'memory:'
            }, context=context, feedback=feedback)['OUTPUT']


       * # How to return buffered_layer as my algorithm's OUTPUT ????  *

        return {self.OUTPUT: dest_id}
...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20190318/a9be9ea3/attachment.html>


More information about the Qgis-user mailing list