5

Regarding my plugin pyqgis 2.2, i can load a vector layer succesfully. But when i try to zoom to the layer extent it does not work.

How to do in order to zoom to the layer extent?


I have just tried your proposition.I pasted your piece of code into my plugin. But unfortunatly, when i load a vector layer. I try to zoom to the layer extent (thanks to right click -> zoom to layer extent). I have no error message but there 's no change zoom to the extent of vector layer.

You can read the python.py (github plugin link) here :

https://github.com/picardie-nature/clicnat-qgis/blob/master/plugin.py

Any thoughts?

closed as off-topic by PolyGeo Jan 30 '18 at 12:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

8

Something like below should work.

vLayer = iface.activeLayer()
canvas = iface.mapCanvas()
extent = vLayer.extent()
canvas.setExtent(extent)
0

One way to do it would be to select all features in the layer, zoom to selected and then deselect everything:

use this code:

vlayer.selectAll()
mCanvas = iface.mapCanvas()
mCanvas.zoomToSelected()
vlayer.removeSelection()
  • 1
    No don't do this. It will be bad for preformance. – Nathan W Aug 1 '14 at 9:08
  • I think this result is better as when I use OTF transformation, the top rated answer cannot zoom to the extent in transformed CRS. – natsuapo Apr 12 '18 at 13:57
  • This will decrease performance for large dataset. – Ahsan Mukhtar Jun 28 '18 at 10:15

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