Accessing the Map Canvas, Changing Map Units, and Iterating over Layers



This section of the preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook takes a look at how to control an object through the Map Canvas, change map units, and how to loop through map layers. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts. 

Accessing the Map Canvas

Maps in QGIS are controlled through the Map Canvas. In this recipe, we’ll access the canvas and then check one of its properties to ensure we have control of the object.

Getting ready

The only thing you need to do for this recipe is open QGIS and select Python Console from the Plugins menu.

How to do it…

We will assign the map canvas to a variable named canvas. Then we’ll check the size property of the canvas to get its size in pixels.

  1. Enter the following line into the QGIS Python Console:

canvas = qgis.utils.iface.mapCanvas()

  1. Now to ensure we have properly accessed the canvas, check its size in pixels using the following line of code:


  1. Verify QGIS returns a QSize object containing the canvas pixel size similar to the following format:

PyQt4.QtCore.QSize(698, 138)

How it works…

Everything in QGIS centersaround the canvas. The canvas is part of the QGIS interface or iface API. Anything you see on the screen when using QGIS is also generated through the iface API. Note that the iface object is only available to scripts and plugins. When you are building a standalone application, you must initialize your own QgsMapCanvas object.

Change the Map Units

Changing the units of measure on a map, or map units, is a very common operation depending on the purpose of your map or the standards of your organization or country. In this recipe we’ll read and then change the map units used by QGIS for your project.

Getting ready

The only preparation for this recipe is to open QGIS and select Python Console from the Plugins menu.

How to do it…

In the following steps, we’ll access the map canvas, check the map unit type, and then alter it to a different setting.

  1. First access the map canvas:

canvas = iface.mapCanvas()

  1. Now get the map units type. By default it should be the number 2:


  1. Now let’s set the map units to meters using the built-in enumerator:


How it works…

QGIS has seven different map units available which are enumerated in the following order:

  • 0 Meters
  • 1 Feet
  • 2 Degrees
  • 3 UnknownUnit
  • 4 DecimalDegrees
  • 5 DegreesMinutesSeconds
  • 6 DegreesDecimalMinutes
  • 7 NauticalMiles

It is important to note that changing the map units just changes the unit of measure for the the measurement tool and display in the status bar, it does not change the underlying map projection. You’ll notice this difference if you try to run an operation in the processing toolbox that depends on projected data in meters if the data is unprojected. The most common use case for changing map units is to switch between imperial and metric units depending on the user’s preference.

Iterating over Layers

For many GIS operations you need to loop through the map layers to look for specific information or to apply a change to all layers. In this recipe, we’ll loop through the layers and get information about them.

Getting ready

We’ll need two layers in the same map projection to perform this recipe. You can download the first layer at the following URL as a zip file:

You can download the second zipped layer here:

Unzip both of these layers into a directory named ms within your qgis_datadirectory.

How to do it…

We will add the layers to the map through the map registry. Then we will iterate through the map layers and print each layer’s title.

  1. First, let’s open the polygon and point layer using the QGIS Python Console:

lyr_1 = QgsVectorLayer(“/Users/joellawhead/qgis_data/ms/mississippi.shp”, “Mississippi”, “ogr”)

lyr_2 = QgsVectorLayer(“/Users/joellawhead/qgis_data/ms/MSCities_Geo_Pts.shp”, “Cities”, “ogr”)

  1. Next we get the map layer registry instance:

registry = QgsMapLayerRegistry.instance()

  1. Now add the vector layers to the map:

registry.addMapLayers([lyr_2, lyr_1])

  1. Then we retrieve the layers as an interator:

layers = registry.mapLayers()

  1. Finally we loop through the layers and print the titles:

for l in layers:


  1. Verify you can read the layer titles in the Python Consolesimilar to the following format:



How it works…

Layers in QGIS are independent of the map canvas until you add them to the map layers registry. They have an id as soon as they are created.When added to the map, they become part of the canvaswhere they pick up titles, symbols, and many other attributes in which you can use the map layers registry to iterate through them and access them to change the way the look or add and extract data.

QGIS Python Programming CookBook

<– Previous: Creating Dynamic Maps in QGIS     |     Next: Symbolizing Layers –>

Photo of author
About the author

Free weekly newsletter

Fill out your e-mail address to receive our newsletter!