Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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, to a subdirectory named hancock:

https://geospatialpython.googlecode.com/svn/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://geospatialpython.googlecode.com/svn/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 composition as an image. To...