I have a QGIS layer that represents a set of links and nodes to show the streets of a city and the traffic speeds on each street. I've been trying to write a script so that if I change the speeds in the CSV file (links3.csv) the speeds will automatically update in QGIS. However when I run the code I currently have, then change the file, the entire program freezes and I have to force quit it.

import qgis.utils
from PyQt4.QtCore import QFileSystemWatcher

myLayer = qgis.utils.iface.activeLayer()
def refreshLayer():
    myLayer.setCacheImage( None )

watcher = QFileSystemWatcher()
watcher.addPath( '/Users/username/Traffic Speeds/links3.csv' )
watcher.fileChanged.connect( refreshLayer )

What am I doing wrong? I got the code to automatically refresh the layer from here

  • Can you confirm that watcher* is eyeing the proper file? I experienced a similar issue, part of the problem was an incomplete filepath. – Katalpa Feb 17 '15 at 18:02
  • 1
    I just tried re-writing the path and now it works! I think I had an extra space or something. Thank you so much! – swooders Feb 17 '15 at 18:12
  • Nice work! You might add an answer to your question so as to help other future users out. – Katalpa Feb 17 '15 at 18:26

Double check the filepath. In this case, there might be a small mistake in the filepath that causes the script to stop working.

Not the answer you're looking for? Browse other questions tagged or ask your own question.