1

I am trying to join layers programmatically following the answers in the GIS.SE question - Join table field with shapefile programatically (via PyQgis)? :

# Get input (csv) and target (Shapefile) layers
shp=iface.activeLayer()
csv=iface.mapCanvas().layers()[0]

# Set properties for the join
shpField='code'
csvField='codigo'
joinObject = QgsVectorJoinInfo()
#...

But I get an error about QgsVectorJoinInfo not defined. I tried (unsuccessfully) to import it with:

from qgis.core import QgsVectorJoinInfo

(because the documentation linked above says that QgsVectorJoinInfo is in the "core" library).

How should I import it properly or otherwise make the above code work?

(In general, how to tell from documentation which library to import?)

4

The class has been renamed from QgsVectorJoinInfo to QgsVectorLayerJoinInfo.

You now need to call each join function with their associated parameter:

#...
csvField = 'id'
shpField = 'ID'
joinObject = QgsVectorLayerJoinInfo()
joinObject.setJoinFieldName(csvField)
joinObject.setTargetFieldName(shpField)
joinObject.setJoinLayerId(csv.id())
joinObject.setUsingMemoryCache(True)
joinObject.setJoinLayer(csv)
shp.addJoin(joinObject)

Example:

Example

  • @user3142418 - Glad you found it helpful :) – Joseph Dec 6 '18 at 13:19

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.