5

I want to use "Join Attribute by Location (summary)" processing algorithm in Python console of QGIS 3.10. After design my algorithm process (join sum of field name "abc" form layer 2 (point layer) to layer 2 (polygon layer), I exported it as script algorithm as follow:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing


class Model(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('layer1', 'Layer 1', types=[QgsProcessing.TypeVectorPolygon], defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('layer2', 'Layer 2', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Output', 'Output', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Join attributes by location (summary)
        alg_params = {
            'DISCARD_NONMATCHING': False,
            'INPUT': parameters['layer1'],
            'JOIN': parameters['layer2'],
            'JOIN_FIELDS': '[abc]',
            'PREDICATE': [1],
            'SUMMARIES': [5],
            'OUTPUT': parameters['Output']
        }
        outputs['JoinAttributesByLocationSummary'] = processing.run('qgis:joinbylocationsummary', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Output'] = outputs['JoinAttributesByLocationSummary']['OUTPUT']
        return results

    def name(self):
        return 'model'

    def displayName(self):
        return 'model'

    def group(self):
        return ''

    def groupId(self):
        return ''

    def createInstance(self):
        return Model()

Now I want to copy this code in Python console of QGIS and use it, but with run this code in console, I don't receive any result. How can I use this code?

1 Answer 1

2

Once you run that code in the console, the name Model refers to the class of your algorithm. Instantiate the algortihm:

alg = Model()

Then, execute its dialog:

processing.execAlgorithmDialog(alg)

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.