Monday 28 May 2018

qgis - How to test for previously joined layers before run a code


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?




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...