2

I've just started to use the new (3.6.1) processing script format (see https://github.com/qgis/QGIS-Enhancement-Proposals/issues/134 or https://anitagraser.com/2019/03/02/easy-processing-scripts-comeback-in-qgis-3-6/) and tried to put 2 processing algorithmen in it (centroid and buffer), but without success. The script stopped with a :( QGIS crashed. May be someone can help me to get the right idea to connect various processing steps within the new format. Here is my script:

from qgis.processing import alg
import processing
@alg(name="centroids_and_buffer", label=alg.tr("Centroids and buffer"), 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 testalg(instance, parameters, context, feedback, inputs):
    """
    Description goes here. (Don't delete this! Removing this comment will cause errors.)
    """
    source = instance.parameterAsVectorLayer(parameters, "INPUT", context)
    output = instance.parameterAsOutputLayer(parameters, "OUTPUT", context)

    centroids = processing.run("native:centroids", {'INPUT': source,'ALL_PARTS': True, 'OUTPUT': 'memory:'})
    #now the buffer
    alg_params = {'DISSOLVE': False,
        'DISTANCE': 0.001,
        'END_CAP_STYLE': 0,
        'INPUT': centroids['OUTPUT'],
        'JOIN_STYLE': 0,
        'MITER_LIMIT': 2,
        'SEGMENTS': 5,
        'OUTPUT': 'memory:'}
    buffer= processing.run("native:buffer", alg_params)
    print(buffer['OUTPUT'])
    #output = buffer['OUTPUT']
    return {"OUTPUT": buffer['OUTPUT']}
2
  • Try remove print(buffer['OUTPUT']). Using print() from scripts tends to crash QGIS 3.
    – Joseph
    Commented Apr 12, 2019 at 11:09
  • Thanks Joseph, but it is still crashing.
    – eurojam
    Commented Apr 12, 2019 at 12:13

1 Answer 1

2

Thanks to the [email protected] I could solve the problem. There is a brief description how to proceed with the new @alg decorator at: https://docs.qgis.org/testing/en/docs/user_manual/processing/scripts.html#the-alg-decorator. Finally the script looks like this:

from qgis.processing import alg
import processing

@alg(name="centroids_and_buffer", label=alg.tr("Centroids and buffer"), group="examplescripts", group_label=alg.tr("Example Scripts"))
@alg.input(type=alg.SOURCE, name="INPUT", label="Input layer")
@alg.input(type=alg.VECTOR_LAYER_DEST, name='BUFFER_OUTPUT', label='Buffer output')

def centroids_and_buffer(instance, parameters, context, feedback, inputs):
    """
    Description goes here. (Don't delete this! Removing this comment will cause errors.)
    """
    source = instance.parameterAsVectorLayer(parameters, "INPUT", context)
    if feedback.isCanceled():
        return {}

    centroids = processing.run("native:centroids", 
            {'INPUT': parameters['INPUT'],
            'ALL_PARTS': True, 
            'OUTPUT': 'memory:'},
            is_child_algorithm=True,
            context=context,
            feedback=feedback)

    if feedback.isCanceled():
        return {}   

    alg_params = {'DISSOLVE': False,
            'DISTANCE': 0.01,
            'END_CAP_STYLE': 0,
            'INPUT': centroids['OUTPUT'],
            'JOIN_STYLE': 0,
            'MITER_LIMIT': 2,
            'SEGMENTS': 5,
            'OUTPUT': parameters['BUFFER_OUTPUT']}

    buffer = processing.run("native:buffer", alg_params,
            is_child_algorithm=True,
            context=context,
            feedback=feedback)

    if feedback.isCanceled():
        return {}    

    return {'BUFFER_OUTPUT': buffer['OUTPUT']}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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