1

I'm searching how to join two layers using PyQGIS.

I found a few solutions on this forum, and I specifically tried to follow the solution given here : How to use QGIS Graphical Modeller to join specific fields?

The displayed interface which offers a choice in the target/join layer and target/join field is perfect !

This solution was given 7 years ago, many things have changed since.

Here the original 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)

First I had an error on this : QgsVectorJoinInfo

Here: https://qgis.org/api/classQgsVectorLayerJoinInfo.html, we can see it changed for QgsVectorLayerJoinInfo

So I tried replacing QgsVectorJoinInfo with QgsVectorLayerJoinInfo.

But now I have this error :

"NameError: name 'processing' is not defined"

Traceback (most recent call last): File "C:\PROGRA~1/QGIS32~1.3/apps/qgis/./python/plugins\processing\script\ScriptEditorDialog.py", line 228, in runAlgorithm exec(self.editor.text(), _locals) File "", line 9, in NameError: name 'processing' is not defined

Since I don't know what I'm doing, I'm not sure changing random thing as I find infomation on the internet will work.

I also checked this solution : Joining table field with shapefile using PyQGIS and others sites like https://python-forum.io

I never worked with Python, I have literally no idea of how I can make it work.

This issue is old now; maybe I'm not supposed doing it that way?

I'm open to a better solution if you know how to automatically join 2 layers, without using the "add vector join" option in the layer's properties and without creating a new layer.

Modified code :

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

from qgis import processing

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

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

The error is:

Traceback (most recent call last): File "C:\PROGRA~1/QGIS32~1.3/apps/qgis/./python/plugins\processing\script\ScriptEditorDialog.py", line 228, in runAlgorithm exec(self.editor.text(), _locals) File "", line 9, in AttributeError: module 'qgis.processing' has no attribute 'getObject'

Matt
  • 16,843
  • 3
  • 21
  • 52
Giene
  • 669
  • 2
  • 9
  • 1
    Please edit your question to include additional details. – user2856 Mar 03 '22 at 04:27
  • Is that standalone code? Check this out: https://gis.stackexchange.com/questions/279874/using-qgis-3-processing-algorithms-from-pyqgis-standalone-scripts-outside-of-gu/279937#279937 – Comrade Che Mar 03 '22 at 06:06
  • 1
    is there a reason to use python if that is not a language you know - may be looking at the ogr library in another language would be easier for you – Ian Turton Mar 03 '22 at 08:34
  • For now, since I didn't find a way to make it work with Python, I used the Graphical modeler. It always exports a new output of the joined layers. Still from the Graphical modeler, I use "Network plugin" to update fields of the first layer from the output layer (using get_feature). There are always small files, so it is pretty fast. At the end, I have few layers that I manually delete. This is the best way I found to make it work, and that's not bad finally. – Giene Mar 07 '22 at 07:02

0 Answers0