2

I´m trying to solve my problem with Join attributes by field value and for that I want to make a script on PyQGis to solve that.

Is there a location where I can found the script used in Join by field value on QGIS 3? To get it as a base and a guide.

I looked at GitHub and found only the Spatial Join script.

  • 1
    You can find the script here but it's written in C++ since it is part of the native toolset. – Joseph May 3 at 11:28
3

Since the QGIS version 3.6, you can export processing models as Python.

Just create a model with two input layers (geometry non required) and the "Join attributes by field" algorithm, save the model and export it as Python.

Result here :

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 MyModel(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('inputlayer1', 'input_layer_1', types=[QgsProcessing.TypeVector], defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('inputlayer2', 'input_layer_2', types=[QgsProcessing.TypeVector], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Output_layer', 'output_layer', optional=True, 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_field_value
        alg_params = {
            'DISCARD_NONMATCHING': False,
            'FIELD': 'join_field_layer_1',
            'FIELDS_TO_COPY': None,
            'FIELD_2': 'join_field_layer_2',
            'INPUT': parameters['inputlayer1'],
            'INPUT_2': parameters['inputlayer2'],
            'METHOD': 1,
            'PREFIX': '',
            'OUTPUT': parameters['Output_layer']
        }
        outputs['Join_attributes_by_field_value'] = processing.run('native:joinattributestable', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Output_layer'] = outputs['Join_attributes_by_field_value']['OUTPUT']
        return results

    def name(self):
        return 'my_model'

    def displayName(self):
        return 'my_model'

    def group(self):
        return ''

    def groupId(self):
        return ''

    def createInstance(self):
        return MyModel()

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.