3

In my script I make a copy of input layer and perform some actions on it. I can't figure out how to stop QGIS from returning the output layer on error. I've tried returning nothing, but if parameter was defined it gets returned no matter what. instance.removeParameter() also has no effect. Here is a minimal example of my code:

from qgis.processing import alg
from qgis.core import (QgsField,
                       QgsFeatureSink,
                       QgsFeature)
from PyQt5.QtCore import QVariant


@alg(name='somename', label='somelabel',
     group='somegroup', group_label='somegroup')
@alg.input(type=alg.SOURCE, name='INPUT', label='In')
@alg.input(type=alg.SINK, name='OUTPUT',
           label='Out')
def skryptTN(instance, parameters, context, feedback, inputs):
    """
    stuff
    """
    source = instance.parameterAsSource(parameters, 'INPUT', context)

    flds = source.fields()

    (sink, dest_id) = instance.parameterAsSink(parameters, 'OUTPUT', context,
                                               flds, source.wkbType(), source.sourceCrs())

    for in_feat in source.getFeatures():

        out_feat = QgsFeature()

        out_feat.setFields(flds)

        for i in source.fields():
            name = i.name()
            out_feat[name] = in_feat[name]

        out_feat.setGeometry(in_feat.geometry())

        if in_feat['field_1']:
            out_feat['field_1'] = 'something'
        else:
            feedback.reportError(f'Error because of reasons!')
            return # this is not stoping output feature sink from being returned

        sink.addFeature(out_feat, QgsFeatureSink.FastInsert)

    return {'OUTPUT': dest_id}

How to stop script from creating and returning output layer ?

2

Try to use

raise Exception('Error because of reasons!')

rather than or in addition to

feedback.reportError('')     

to prevent output layer from being created and returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.