Running script when layer is updated

pyqgisupdate

I have a system where a process is continuously pushing new earthquake data into a PostGIS database, The data are displayed in QGIS having a layer that is updating each 60 sec. This works fine to display the data, but I would like to do a bit more. Is it somehow possible to connect a Python script to the layer so that it is run each time the layer is updated?

I would only need to run it when the layer is changed (i.e. new data are coming in) but it would not cause any problem if it is run each time the layer is updated (i.e. each 60 sec)

Edit: Following the pointer from @Matt I ran the following code in the Python Console:

bull=QgsProject.instance().mapLayersByName('Bullentin')[0]
def onUpdate():
    iface.messageBar().pushMessage("For info", "updated", level=Qgis.Info, duration=3)
    print("updated")

bull.layerModified.connect(onUpdate)

I expected to get a messagebar and a printed message each time my layer was updated, but nothing is happening, so something still is wrong.

(The layer is updated in three ways: The postgisdatabase behind the layer may be updated (I do not expect that to trigger anything), then the layer is being automatically updated each 60 sec – (I expected that to trigger onUpdate to run) and I have manually updated the layer (Update SQL layer – Update) In neither case was the onUpdate triggered.

Best Answer

You can connect a function to the QgsVectorLayer.layerModified() signal.

See this answer where I perform a spatial join when a layer is edited. You can swap out the function for your own procedure.

Related Question