Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm having trouble figuring out how to iterate over map layers. I can access the currently highlighted layer in the table of contents via iface.activeLayer() but I want my code to operate on all layers.

I don't see anything in the API that provides easy access to this functionality and couldn't find a good online example but maybe I missed something?

share|improve this question
add comment

2 Answers


# substitute 'self' with 'qgis.utils.iface' when run from Python console
# 'self.iface = iface' would usually precede this command in your class 
layers = self.iface.legendInterface().layers()

for layer in layers:
    layerType = layer.type()
    if layerType == QgsMapLayer.VectorLayer:
        # do some stuff here

Culled from consolidatethread.py from QConsolidate plugin.

Description of QgsLegendInterface object returned from legendInterface().

Edit: added info on 'self' above.

share|improve this answer
Thanks! That did the trick. I guess I'm too used to the ESRI world where legend is the object in the map layout and table of contents refers to the list of layers. –  spencerrecneps May 26 '12 at 23:53
Note that there is a difference in what is returned between this and the mapCanvas() technique Nathan mentions. The legendInterface() object will return all layers, even if they are not shown in the map canvas, while mapCanvas() returns only those shown on the canvas. Using mapCanvas() is a nice, quick way to get a reference to visible layers. –  dakcarto May 27 '12 at 2:00
On another note, the latest versions of QGIS (>= 1.8) allow the user to define a rendering order of layers separately from the order listed in the legend (very nice!). The legendInterface() and mapCanvas() methods return the layer order for each respectively. –  dakcarto May 27 '12 at 2:07
add comment

There are also two other ways:

layers = self.iface.mapCanvas().layers()

will give you a list of layers


layer = QgsMapLayerRegistry.instance().mapLayers()

for name, layer in layers.iteritems():
     print name, layer.type()
share|improve this answer
Hi Nathan, for the QgsMapLayerRegistry technique, I'm finding I have to use 'layers = core.QgsMapLayerRegistry.instance().mapLayers()' to retrieve the reference to the layers (using 'from qgis import core'). –  dakcarto May 27 '12 at 2:13
or you can do from qgis.core import QgsMapLayerRegistry and it will let you do what I wrote directly. –  Nathan W May 27 '12 at 2:42
Hmmm, I still have to use QgsMapLayerRegistry.instance().mapLayers() or I get a 'TypeError: QgsMapLayerRegistry.mapLayers(): first argument of unbound method must have type 'QgsMapLayerRegistry', which is odd since doing the import that way should instantiate the registry (one would think). After looking at the API, it looks like the constructor is protected, and instance() is a static method to get the pointer. –  dakcarto May 27 '12 at 4:12
opps my bad. Fixed –  Nathan W May 27 '12 at 4:22
@NathanW I thought the mapCanvas() way only picked up the layers that were visible in the map window at that moment - wouldn't you potentially be missing a layer that is either turned off or whose geometry lies outside of the current view? (edit: never mind, just saw dakcarto's comment below confirming this) –  spencerrecneps May 28 '12 at 13:16
add comment

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.