3

I wish to join one field from a shapefile to another shapefile within the QGIS graphical modeler. I am aware that there is a 'Join attributes table' algorithm but this joins all of the fields from the shapefile.

Outside of the graphical modeler, I can use the Joins process in the layer properties but I would much rather be able to use this function within my models.

0

1 Answer 1

5

You can achieve this by creating a custom script in the Processing Toolbox which you can then add to your modeler. To create one:

Processing Toolbox > Scripts > Tools > Create new script

Then add the following code:

##Join_layers=name
##Target_layer=vector
##Join_layer=vector
##Target_field=Field Target_layer
##Join_field=Field Join_layer

from qgis.core import QgsVectorJoinInfo

layer_1 = processing.getObject(Target_layer) 
layer_2 = processing.getObject(Join_layer) 

field_1=Target_field
field_2=Join_field
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = layer_2.id()
joinObject.joinFieldName = Join_field
joinObject.targetFieldName = Target_field
layer_1.addJoin(joinObject)

Make sure it is saved in C:\Users\You\.qgis2\processing\scripts for it to be available in the Processing Toolbox (you may need to close the script for the toolbox to update itself).

When you run the script, it should look like this:

Join script

where the user can choose the layers and fields to join. This has been tested in the QGIS modeler using QGIS 2.12.3-Lyon.

0

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.