Adding labels to a map for printing
The QgsComposition
object allows you to place arbitrary text anywhere in the composition. In this recipe, we'll demonstrate how to add a label to a map composition.
Getting ready
You will need to download the following zipped shapefile and extract it to your qgis_data
directory in a subdirectory named hancock
:
https://github.com/GeospatialPython/Learn/raw/master/hancock.zip
In addition to the shapefile, you will also need the MapComposer
class. This class encapsulates the boilerplate composer code in a reusable way, to make adding other elements easier. You can download it from https://github.com/GeospatialPython/Learn/raw/master/MapComposer.py.
This file must be accessible from the QGIS Python console by ensuring that it is in the Python path directory. Place the file in the .qgis2/python
directory within your home directory.
How to do it...
To add a label to a composition, we'll first build the map composition, create a label, and then save the...