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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 4, 2015 at 12:46
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.

answered Mar 29, 2016 at 14:55
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.