Skip to content

Instantly share code, notes, and snippets.

@kgjenkins
Last active November 30, 2022 09:38
Show Gist options
  • Save kgjenkins/29f8dd4e7da3cba3e0acbdc72844748a to your computer and use it in GitHub Desktop.
Save kgjenkins/29f8dd4e7da3cba3e0acbdc72844748a to your computer and use it in GitHub Desktop.
QGIS Python to automatically update layer from data source

The following Python code will, every 30 seconds, automatically update the data from the source for any QGIS layer with 'autoUpdate' in the layer name. Each time it updates, it will put the timestamp in the layer name, although you could leave that part out.

This is particularly useful for remote data sources like json-over-http or even local CSV files that might be changed by another program while the QGIS project is open.

Just paste the code into the QGIS Python console.

import threading
import datetime
import re

def autoUpdateLayers():
  threading.Timer(30.0, autoUpdateLayers).start()
  for layer in QgsProject.instance().mapLayers().values():
    if 'autoUpdate' in layer.name():
      print('autoUpdating layer: '+layer.name())
      layer.dataProvider().forceReload()
      layer.setName(
        re.sub(
          'autoUpdate.*',
          'autoUpdated ' + datetime.datetime.now().strftime('%c'),
          layer.name()
        )
      )

autoUpdateLayers()

CC0 "No Rights Reserved" So feel free to use, modify, or do whatever you want with this code.

@sigurdbengtson
Copy link

Yeah I only saw the date for your latest post after commenting. But thanks for the answer!

It worked as a nice quick solution for me to use with some layers i access via a postgres database. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment