Skip to main content
Tweeted twitter.com/StackGIS/status/1235218657061605382
Title edit.
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42

How to use python Using Python code from QGIS 3.x Graphical Modeler in console?

I want to use "Join Attribute by Location (summary)" processing algorithm in pythonPython 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 pythonPython 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?

How to use python code from QGIS 3.x Graphical Modeler in console?

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?

Using Python code from QGIS 3.x Graphical Modeler in console?

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?

Source Link
HMadadi
  • 1k
  • 11
  • 24

How to use python code from QGIS 3.x Graphical Modeler in console?

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?