Processing script template for QGIS3

Processing has been overhauled significantly for QGIS 3.0. Besides speed-ups, one of the most obvious changes is the way to write Processing scripts. Instead of the old Processing-specific syntax, Processing scripts for QGIS3 are purely pythonic implementations of QgsProcessingAlgorithm.

Here’s a template that you can use to develop your own algorithms:

from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsField, QgsFeature, QgsFeatureSink, QgsFeatureRequest, QgsProcessing, QgsProcessingAlgorithm, QgsProcessingParameterFeatureSource, QgsProcessingParameterFeatureSink)
class ExAlgo(QgsProcessingAlgorithm):

    def __init__(self):

    def name(self):
        return "exalgo"
    def tr(self, text):
        return QCoreApplication.translate("exalgo", text)
    def displayName(self):
        return"Example script")

    def group(self):

    def groupId(self):
        return "examples"

    def shortHelpString(self):
        return"Example script without logic")

    def helpUrl(self):
        return ""
    def createInstance(self):
        return type(self)()
    def initAlgorithm(self, config=None):
  "Input layer"),
  "Output layer"),

    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()
            sink.addFeature(out_feat, QgsFeatureSink.FastInsert)

        return {self.OUTPUT: dest_id}

This script just copies the features of the input layer to the output layer without any modifications. Add your logic to the processAlgorithm() function to get started.

Use Create New Script from the Toolbox toolbar:

Paste the example script:

Once saved, the script will show up in the Processing toolbox:

  1. Ramamohan said:

    I find your articles very timely and easier to understand. I just read your review article on Profile tool plugin. Will you be able to write also on Qepanet plugin. Another area is snapping and topology options in QGIS 3.0. We find some of the options (like ‘enable topological editing’ option in 2.18) missing in 3.0. Are there any automatic snapping plugins available in 3.0 for perfect matching of boundaries of polygons ?

    • Thank you Ramamohan! I’ll look into that plugin. Snapping options are in the Project menu in QGIS3.

  2. Phil said:

    Thank you so much, Anita! This is exactly what I was looking for. Do you know of any other resources for writing processing scripts in QGIS3 specifically? I’m new to writing processing scripts in QGIS and I’m finding a fair amount of stuff for how to do it in QGIS2, but not much for QGIS3. Thanks again, you’ve already been a huge help!

  3. Tohan said:

    Thanks, this has been a huge help!
    I need to call an existing Algorithm to be applied to my current output, would you mind sharing some directions ?
    I understand this could be done through the modeler but my Algorithm really feels unfinished without it so it would really make more sense to have this external call included.

  4. Stefan Giese said:

    Dear Anita,
    very helpful – thanks for the starting point for processing scripts, since there is nearly no documentation at the moment about this.

    • Thanks Stefan. In the next version of QGIS, a template will be included by default.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: