[QGIS-Developer] Procesing pyqgis with alg decorator

matteo matteo.ghetta at gmail.com
Mon Jul 15 05:36:47 PDT 2019


Hi all,

I'm trying a super simple script in Processing with the alg decorator
syntax.

Basically I'm not doing anything, but a weird error is thrown when I try
to append vector layer features to a list:

NameError: free variable 'self' referenced before assignment in
enclosing scope
Traceback (most recent call last):
  File
"/home/matteo/dev/build-QGIS-Desktop-Default/output/python/plugins/processing/gui/AlgorithmDialog.py",
line 244, in on_complete
    self.feedback.pushInfo(self.tr('Execution completed in {0:0.2f}
seconds').format(time.time() - start_time))
NameError: free variable 'self' referenced before assignment in
enclosing scope


And this the super small script:


from qgis.processing import alg
from qgis.core import QgsFeature, QgsFeatureSink

@alg(name="myalg", label=alg.tr("My Algorithm"), group="examplescripts",
group_label=alg.tr("Example Scripts"))
@alg.input(type=alg.SOURCE, name="INPUT", label="Input layer")
@alg.input(type=alg.SINK, name="OUTPUT", label="Output layer")

def myalg(instance, parameters, context, feedback, inputs):
    """
    Description goes here. (Don't delete this! Removing this comment
will cause errors.)
    """
    source = instance.parameterAsSource(parameters, "INPUT", context)

    (sink, dest_id) = instance.parameterAsSink(
        parameters,
        "OUTPUT",
        context,
        source.fields(),
        source.wkbType(),
        source.sourceCrs()
    )

    l = []

    features = source.getFeatures()

    for current, feature in enumerate(features):

        l.append(feature)

        out_feature = QgsFeature(feature)
        sink.addFeature(out_feature, QgsFeatureSink.FastInsert)

    return {"OUTPUT": dest_id}

Some ideas?

Cheers and thanks

Matteo






More information about the QGIS-Developer mailing list