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 do this, we need to perform the following steps:

  1. First, we need to import the Qt GUI libraries and the MapComposer class:
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    import MapComposer
    
  2. Next, we create a layer with the shapefile, setting the path to the shapefile in order to match your system:
    lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/hancock/hancock.shp", "Hancock", "ogr")
    
  3. Now, we add this layer to the map:
    reg = QgsMapLayerRegistry.instance()
    reg.addMapLayer(lyr)
    
  4. Next, we access the map renderer:
    mr = iface.mapCanvas().mapRenderer()
    
  5. Then, we create a MapComposer object, passing in the map layer registry and the map renderer:
    qc = MapComposer.MapComposer(qmlr=reg, qmr=mr)
    
  6. Now, we create a new label object:
    qc.label = QgsComposerLabel(qc.c)
    
  7. We can set the label text to any string:
    qc.label.setText("Hancock County")
    
  8. We can automatically set the size of the label container to fit the string we used:
    qc.label.adjustSizeToText()
    
  9. Now, we add a frame around the label box:
    qc.label.setFrameEnabled(True)
    
  10. Then, we set the position of the label on the page, which is at the top-left corner of the map:
    qc.label.setItemPosition(qc.x,qc.y-10)
    
  11. Next, we add the label the map composition now that it is configured:
    qc.c.addItem(qc.label)
    
  12. Finally, we save the composition image:
    qc.output("/Users/joellawhead/qgis_data/map.jpg", "jpg")
    
  13. Verify that your output image has a text label in a frame at the top-left corner of the map.

How it works...

In this case, we created a very simple label based on defaults. However, labels can be customized to change the font, size, color, and style for print-quality compositions. Also, note that the x,y values used to place items in a composition start in the upper-left corner of the page. As you move an item down the page, the y value increases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset