Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

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 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


# 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 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

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

for QGIS 2.6 here is the code to identify each layer and group them

        #make the desired groups for layers
        toc = self.iface.legendInterface()
        root = QgsProject.instance().layerTreeRoot()
        group1 = root.insertGroup(0, "Group Point")
        group2 = root.insertGroup(1, "Group Line")
        group3 = root.insertGroup(2, "Group Polygon")
        #get the list of layers  from registry
        layers = QgsMapLayerRegistry.instance().mapLayers()
        #segregate layers into groups 
        for name, layer in layers.iteritems():
            # check the layer geometry type 
                           if layer.geometryType() == QGis.Point:
                toc.moveLayer(layer, 0)

            if layer.geometryType() == QGis.Line:
                toc.moveLayer(layer, 1)

            if layer.geometryType() == QGis.Polygon:
                toc.moveLayer(layer, 2)

Note:After grouping i have to remove the duplicate layer so i used root.removeLayer(lyr)

share|improve this answer
+1 Great to see updated code for latest software versions! – Joseph Dec 9 '14 at 15:25

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.