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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a plugin for QGIS using python, But need some help. I want to 1.Zoom to a layer and 2. make the layer visible(switch off or on).

But I have managed to zoom to an active layer by setting the active layer in the map canvas by using.

import qgis
canvas = qgis.utils.iface.mapCanvas()
acl = canvas.layer(1)
> True

But this only work as long as the layer is viable(obviously since I use canvas). So I need another way to "point" to a layer and then zoom to and switch off.


share|improve this question
up vote 4 down vote accepted

You can use a combination of QgsMapCanvas.setExtent(), QgsMapLayerRegistry (Use QgsMapLayerRegistry.instance() to get access, since it's a singleton) to find your layer and QgsMapLayer.extent() to get its extent.

canvas = iface.mapCanvas()
vl = QgsMapLayerRegistry.instance().mapLayer( 'YourLayerId' )
canvas.setExtent( vl.extent() )
share|improve this answer
Thanks, it seems to be what I was looking for.However I'm not mush of a programmer and I am struggling some what to get this to work. If you have an example that will be great, but thanks again for the help. – Jcstay Jul 19 '13 at 10:31
I added a minimal example. I need to know more about your setup to be able to extend it, so if you have specific questions, the example can be extended. E.g. based on your problem you could also get the reference to the vectorlayer (simly called vl in the example) in a different way. Also I'm not sure about the "switch off" part. I assume it's about visibility, but you mention it at a place where you said before it's already invisible... – Matthias Kuhn Jul 19 '13 at 13:57
Thanks I managed to get it working and it works great. – Jcstay Jul 24 '13 at 10:42

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.