1

I'm working a lot with GDAL VRT raster layers in QGIS 3.14, often changing the file content while viewing the rasters in map canvas. When I zoom or refresh the canvas in QGIS 2.18, all modified VRT raster layers are refreshed as well. In QGIS 3.14 it looks like VRT layers are cached. I can refresh the virtual raster layers only with the help of the indispensible "changeDataSource" plugin.

What do I need to refresh a VRT layer with Python?

1 Answer 1

0

The only possible method I found to solve this problem is to write the Layer XML to a DOM document and reread it afterwards. I grabbed this idea from "changeDataSource" plugin.

from qgis.PyQt.QtXml import QDomDocument
from qgis.core import QgsReadWriteContext
...
XMLDocument = QDomDocument("style")
XMLMapLayers = XMLDocument.createElement("maplayers")
XMLMapLayer = XMLDocument.createElement("maplayer")
context = QgsReadWriteContext()
layer.writeLayerXml(XMLMapLayer,XMLDocument, context)
layer.readLayerXml(XMLMapLayer, context)
layer.reload()
iface.actionDraw().trigger()
iface.mapCanvas().refresh()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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