4

I am running a python script in QGIS 3.8.3-Zanzibar where I am changing the source of a GeoJson file.

I am calling layer.reload() but subsequent calls to layer.extent() still return the old extent. I can open the attribute table and zoom to the location and see the selected outline.

enter image description here

But the actual layer symbol does not display.

Things that I've tried:

  1. I turned off canvas caching to rule that out.
  2. I have tried these statements (in a variety of combinations):

    layer.reload()
    layer.triggerRepaint()
    layer.setDataSource( layer.source(), layer.name(), layer.providerType() )

I figure I'm missing something easy.

2
  • 2
    Try updating the extent using layer.updateExtents()
    – Joseph
    Commented Oct 9, 2019 at 9:24
  • 1
    yep...that's it. I was scanning the wrong layer type in the docs, but it's there in plain English! qgis.org/pyqgis/3.0/core/Vector/… @Joseph: make an answer, i'll vote/accept it. Commented Oct 9, 2019 at 11:26

2 Answers 2

5

This works for me:

if iface.mapCanvas().isCachingEnabled():
    lyr.triggerRepaint()
else:
    iface.mapCanvas().refresh()
4

To update the extents of a layer after making any changes to its data source, use the QgsVectorLayer::updateExtents() method:

layer.updateExtents()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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