Bug report #20536

Redrawing raster layer after file changed in QGIS?

Added by Ivan Ivanov over 5 years ago. Updated about 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:PyQGIS Console
Affected QGIS version:3.6.1 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:28356

Description

A raster layer file is loaded in QGIS. Then the contents of the file are changed on the filesystem. Forced redraw is applied but QGIS does not draw the correct data. Also, zoom in/zoom out draws different raster data on each zoom level of the same file contents.

I guess there is some deep caching, which is not cleaned. However, I was unable to find a way to empty the cache, even though there was a way in QGIS 2.

This script can be used to reproduce the bug:

import gdal, ogr, os, osr
import numpy as np

filename = 'test2.tif'

if not 'GLOBAL_COUNTER' in globals():
    GLOBAL_COUNTER = 0

def array2raster(filename, rasterOrigin, pixelWidth, pixelHeight, array):
    cols = array.shape[1]
    rows = array.shape[0]
    originX = rasterOrigin[0]
    originY = rasterOrigin[1]
    driver = gdal.GetDriverByName('GTiff')
    outRaster = driver.Create(filename, cols, rows, 1, gdal.GDT_Byte)
    outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
    outband = outRaster.GetRasterBand(1)
    outband.WriteArray(array)
    outRasterSRS = osr.SpatialReference()
    outRasterSRS.ImportFromEPSG(4326)
    outRaster.SetProjection(outRasterSRS.ExportToWkt())
    outband.FlushCache()

def generateLayer(filename):
    rasterOrigin = (0, 0)
    pixelWidth = 100
    pixelHeight = 100

    array = np.full((10, 10), 1)

    idx = GLOBAL_COUNTER % 10

    array[idx][idx] = 2

    print(idx, array[idx])

    array2raster(filename, rasterOrigin, pixelWidth, pixelHeight, array)

def layerRefresh(raster):
    raster.dataProvider().reloadData()
    raster.triggerRepaint()
    iface.mapCanvas().refresh()
    iface.layerTreeView().refreshLayerSymbology(raster.id())

generateLayer(filename)

if len(QgsProject.instance().mapLayersByName("testRasterLayer")) == 0:
    iface.addRasterLayer(filename, "testRasterLayer")

QgsProject.instance().reloadAllLayers()
layerRefresh(QgsProject.instance().mapLayersByName("testRasterLayer")[0])

GLOBAL_COUNTER += 1

There is an issue thread on StackExchange too:
https://gis.stackexchange.com/questions/300930/redrawing-raster-layer-after-file-changed-in-qgis

Associated revisions

Revision 88212b00
Added by Mathieu Pellerin - nIRV about 5 years ago

[gdal] Implement the data provider reloadData() function (fixes #20536)

Revision 581b48da
Added by Mathieu Pellerin - nIRV about 5 years ago

[gdal] Implement the data provider reloadData() function (fixes #20536)

Revision e174577a
Added by Mathieu Pellerin - nIRV about 5 years ago

[gdal] Implement the data provider reloadData() function (fixes #20536)

Revision fdb82fc8
Added by Mathieu Pellerin - nIRV about 5 years ago

[gdal] Implement the data provider reloadData() function (fixes #20536)

Revision 8d0566f0
Added by Mathieu Pellerin - nIRV about 5 years ago

[gdal] Implement the data provider reloadData() function (fixes #20536)

History

#1 Updated by Anton Myrholm about 5 years ago

Hinders the work of my plugin that downloads raster data based on current extent and redraws it, now has to create new layers instead for each change..

#2 Updated by Dietmar Gehring about 5 years ago

This is still an issue in 3.6.1. Makes georeferencing of raster images a PITA if you have to add a new layer and change the draw style every time you change the reference points.

#3 Updated by Giovanni Manghi about 5 years ago

  • Operating System deleted (Ubuntu 18.04, Ubuntu 18.10, Windows 10)
  • Affected QGIS version changed from 3.4.1 to 3.6.1

#4 Updated by Mathieu Pellerin - nIRV about 5 years ago

  • % Done changed from 0 to 100
  • Status changed from Open to Closed

Also available in: Atom PDF