4

I have written a small python plugin to refresh a join on a point layer in QGIS.

point_layer=None
for lyr1 in QgsMapLayerRegistry.instance().mapLayers().values():
  if lyr1.name() == "point_layer":
    point_layer = lyr1

text_table=None
for lyr2 in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr2.name() == "text_table":
  text_table = lyr2

POINT_ID='ID'
TEXT_ID='ID'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = text_table.id()
QgsVectorLayer.removeJoin(point_layer, text_table.id())
joinObject.joinFieldName = TEXT_ID
joinObject.targetFieldName = POINT_ID
joinObject.memoryCache = True
point_layer.addJoin(joinObject)

I tried to get a subset of fields to be used from joined layer text_layer.

joinObject.joinFieldNamesSubset = 'TYPE'

There is no error, but it joins all the fields, not only the field 'TYPE' (attribute of layer text_layer).

How can I set the attributes I want to join? And where do I have to call joinFieldNamesSubset?

1 Answer 1

1

Try using the following line instead:

joinObject.setJoinFieldNamesSubset(['TYPE'])

And as it is a list, you can specify which fields you only want to have joined:

joinObject.setJoinFieldNamesSubset(['Field_1', 'Field_2', 'Field_3'])
2
  • 1
    Note: values of field subsets have to be in order with the attributes of the joined layer. Otherwise fields and values doesn't match (e.g. joinObject.setJoinFieldNamesSubset(['Field_3', 'Field_2', 'Field_1']) doesn't work for me).
    – Stefan
    Jun 6, 2016 at 16:04
  • @Stefan - That's a good point, thanks for mentioning that =)
    – Joseph
    Jun 7, 2016 at 8:59

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.