3

I have a csv file that I am joining to a layer, then creating labels based on the joined layer. When creating the delimited text layer, I include the option to watch the layer. At the time of creation, the labels are correct.

When the csv file is updated with new values in the columns, the labels are not updated. I have even tried closing the QGIS project and reopening and they still aren't updated.

Short of deleting the delimited text layer and re-adding a new delimited text layer and rejoining to my layer and redefining the label format, how can I automatically update the labels based on an updated delimited text layer csv file?

  • Have you tried un-checking the 'Cache join layer in virtual memory' check box when you create the join? – Liam G Jan 31 '16 at 12:01
1

As @Jakob already mentioned, the JOIN needs to be recreated in order for the attributes, and ergo the labels, to be updated. You could do this automatically using the Python Console and use the following code adapted from this post: Join table field with shapefile programatically (via PyQgis)? (credit to @gcarrillo):

# "csv" is the first layer loaded into the ToC (csv file), "shp" is the shapefile
shp = iface.activeLayer()
csv = QgsMapLayerRegistry.instance().mapLayers().values()[0]

# Set JOIN properties by changing 'ID' to relevant field names
shpField='ID'
csvField='ID'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField

def update_join():
    shp.removeJoin(csv.id())
    shp.addJoin(joinObject)
    shp.triggerRepaint()

Then type update_join() in the console to run. Essentially, after setting the JOIN parameters, the join is removed, added again and the shapelayer is refreshed.

  • Thanks very much for the code snippet. It is a good starting place for me. I have a followup question. Does same if I join a sqllite table? I'm assuming that is possible. – Robotuner Feb 1 '16 at 19:44
  • @Robotuner - Yup, should do. Just tested it and it works for that too :) – Joseph Feb 2 '16 at 10:42
2

You can't. You need to recreate the join, even when you have a watch on the CSV file, when you make changes to the csv file. The watch is working on the CSV file, but just not propagated to the join. I believe this is a WAD (work as designed). You could report a bug on the QGIS issue tracker http://hub.qgis.org/issues?page=3.

If your into PostGIS you could join the two layers there. Then I pretty sure that the dynamic PostGIS view interaction with QGIS will update the map, after a map refresh.

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.