0

I've a function that join two added layers. The code works fine, but now I need to test for previously joined layers before run this function. The function code is right below:

def jointables(dict): # this dict provides [targetLayer:layerToJoin]
  for k, v in dict.items():
    target = QgsProject.instance().mapLayer(k)
    layerToJoin = QgsProject.instance().mapLayer(v)
    fieldToJoin = QgsProject.instance()
    symb = QgsVectorLayerJoinInfo()
    symb.setJoinFieldName('id_feature')
    symb.setTargetFieldName('id')
    symb.setJoinLayerId(layerToJoin.id())
    symb.setEditable(True)
    symb.setDynamicFormEnabled(True)
    symb.setUpsertOnEdit(True)
    symb.setPrefix('')
    symb.setJoinLayer(layerToJoin)
    target.addJoin(symb)

I've tried some things like this:

for k, v in dict.items():
    if QgsProject.instance().mapLayer(k).addJoin(QgsVectorLayerJoinInfo()) == True:
        break
    else:
        continue

or

for k, v in dict.items():
    if QgsVectorLayerJoinInfo().isEditable() == True:
       break
    else:
       continue

among others. I've missing something. Both conditionals has the same value True, doesnt matter if already exists an previously joined layer or not. How can I solve this?

4
  • Why do u use an addJoin in your first example ? Why do u use an isEditable in your second example ? What has that to do with joined layers ?
    – Snaileater
    Mar 11, 2019 at 12:05
  • I'm new in pyqgis and this was a way find by me to test if there was a previously join, but its wrong. And answering your final question the intention is don't do another join if it already exists. My plugin is functioning but if I press 100 times the plugin button there will be a 100 joins, and the point is: must have only one. Mar 12, 2019 at 19:19
  • No problem ... so ... has my answer been of any use ?
    – Snaileater
    Mar 12, 2019 at 19:27
  • Yes it enlightened my way, but I've to test every layer individually. Now I've to figure out how to do it individually. Mar 12, 2019 at 20:56

1 Answer 1

1

If you want to check whether a QgsVectorLayer has any join you can try the following :

VL.vectorJoins()

vectorJoins() returns a list of QgsVectorLayerJoinInfo for each join set on the VL layer.

So testing for example that len(VL.vectorJoins())==0 means there is no join on the layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.