1

My question is in two parts :

1) How do I modify my pre-existing QGIS 2.18 script to work in QGIS 3? (i'm still learning Python)

2) How do I modify it to join only specific fields (i.e. i dont want to join all 20+ columns)

The script I used in QGIS 2.18 to create joins between a shp file and csv.

import processing
    shpField='nd_id'
    csvField='pt_nd_id'
    result = processing.runandload('qgis:joinattributestable', shp, csv, shpField, csvField, None)

However this script doesn't work in QGIS 3 due to changes in processing.runandload which is now processing.runAndLoadResults and as far as I can tell there might be some changes to joinattributestable(?)

i've already looked at QGIS 3 - Python : QgsVectorLayerJoinInfo issue and this Performing specific table joins in QGIS via python? and based on these i've cooked up the following : (NB: I'm trying to join between 2 shpfiles here)

QgsProject().instance().mapLayersByName('t_alpha')[0]
QgsProject().instance().mapLayersByName('t_beta')[0]

lien = QgsVectorLayerJoinInfo()
lien.setJoinFieldName('ID_alpha')
lien.setTargetFieldName('ID_beta')
lien.setJoinLayerId(layerToJoin.id())
lien.setUsingMemoryCache(True)
lien.setJoinLayer(layerToJoin)
target.addJoin(lien)

But this codes returns

...IndexError: list index out of range

Any advice/ clues?

  • After which line does the error occur? – Joseph Jan 25 at 10:34
  • it doesn't seem to like when I call the layers in the first two lines 'File "<input>", line 1, in <module> File "<string>", line 2, in <module> IndexError: list index out of range' – lhowarth Jan 25 at 10:51
  • 1
    Are you sure there are two layers already loaded before you ran your script and that they are called "t_alpha" and "t_beta"? – Joseph Jan 25 at 10:56
  • 1
    The first problem I can see is that you haven't actually assigned your layers to the layerToJoin and target variables. – Ben W Jan 25 at 12:50
  • @Joseph I loaded the vecteur layers into QGIS using data manager and source type= folder - which added a prefix "GRA_" to the layers - which made my layer names >10 characters... when I reloaded the shp file "normally" - theres no prefix and no index error. NB it also works if you just rename the layer through properties – lhowarth Jan 28 at 8:07
1

Your first problem is that you forgot to assign your join and target layers to your layerToJoin and target variables.

As for the second part of your question, you can specify a subset of fields to take from your join layer by calling the setJoinFieldNamesSubset() method on your QgsVectorLayerJoinInfo() object, passing a list of the field names you want to include.

Try out the code snippet below (just change the subset field names to actual fields from your join layer, and adding more fields if you want):

layerToJoin = QgsProject().instance().mapLayersByName('t_alpha')[0]
target = QgsProject().instance().mapLayersByName('t_beta')[0]
myJoin = QgsVectorLayerJoinInfo()
myJoin.setJoinFieldName('ID_alpha')
myJoin.setTargetFieldName('ID_beta')
myJoin.setJoinLayerId(layerToJoin.id())
myJoin.setUsingMemoryCache(True)
myJoin.setJoinLayer(layerToJoin)
myJoin.setJoinFieldNamesSubset(['Field_I_want_1', 'Field_I_want_2'])
target.addJoin(myJoin)
  • Thanks for the help! I updated the code to correctly assign the layers (target and join). The join is created, and the joined fields appear on the table, but they're empty... it doesn't seem to transfer the data... field are types Qstring text - on the target and join tables – lhowarth Jan 28 at 9:18
  • Do the columns you are using as your join fields contain the same values in each layer? Could you post a screen shot of the attribute table for each layer showing the contents of your join field column? – Ben W Jan 28 at 9:54
  • Aah i spoke too soon.. In fact, the code works perfectly... sorry it's monday morning (there are >6k of entities but the join only concerns <1k and I checked over table the table too quickly) – lhowarth Jan 28 at 11:01
  • Great! Glad to hear it's working. – Ben W Jan 28 at 11:30

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.