Friday 3 February 2017

How to use QGIS Graphical Modeller to join specific fields?


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 utilise this function within my models.




Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...