Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How to automatically reloaded raster layer if source is changed? (The path is the same but inside source file are changes)

I use:


but layer not refresh.

# # #

The maps are repainting but there are still the same image (source file is changed on disc)

I use:

  layers = qgis.utils.iface.legendInterface().layers()
  for layer in layers:
share|improve this question
Is rendering cache disabled in Settings => Options => Rendering? If this is the problem you can either disable it or call setCacheImage( None ) on the layer to dismiss the current cache. – Matthias Kuhn Apr 26 '13 at 7:12

2 Answers 2

I suppose your question does not include change detection, as your sample only concerns QgsMapCanvas.refresh()

Instead you have to call QgsRasterLayer.triggerRepaint()

If your layer is called myLayer:

myLayer.setCacheImage( None )

The same method exists for vector layers as well.

For low overhead file change notification I'd propose looking into Qt's QFileSystemWatcher, which makes use of inotify on linux and similar techniques on other platforms.

from PyQt4.QtCore import QFileSystemWatcher

def refreshLayer():
    myLayer.setCacheImage( None )

watcher = QFileSystemWatcher()
watcher.addPath( '/path/to/your/raster' )
watcher.fileChanged.connect( refreshLayer )

Of course this can be combined with an MD5 check as proposed by nickves.

share|improve this answer

You can check if the file hash has changed between intervals


def md5checksum(fp):
        import hash
    with open(fp, 'rb') as fh:
        m = hashlib.md5()
        while True:
            data =
            if not data:
        return m.hexdigest()

import time
import copy

a,b = str(),str()
while True:
    a =  md5checksum(fp) # file
    if a != b:  # the file has changed, do what you want
        b = copy.copy(a) #shallow copy, otherwise a and b will point at the same object
        sleep.time(1) #wait for 1 sec, then recheck

It's a bit hackish, but the underline idea is valid

(The md5 hash check found here)

share|improve this answer
This would have pretty large overhead for large layers. – Nathan W Jun 28 '13 at 14:03
true, it could for large datasets, but it's pure python – nickves Jun 28 '13 at 16:16
I would just use os.stat – Nathan W Jul 3 '13 at 11:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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