Adding a grid to the map

An annotated reference grid is useful for map products used to locate features. This recipe teaches you how to add both reference lines on a map and annotations for the lines around the edges of the map.

Getting ready

You will need a shapefile for this map from https://geospatialpython.googlecode.com/svn/Mississippi.zip, and you need to extract it to your qgis_data directory, to a subdirectory named ms.

As with the previous recipes in this chapter, we will use the MapComposer library from https://geospatialpython.googlecode.com/svn/MapComposer.py to simplify the creation of the map composition.

Place the file in the .qgis2/python directory within your home directory.

How to do it...

In this recipe, the general steps are to create the map composition, establish the overall grid parameters, define the grid line placement, and then style the grid and annotations. To do this, we need to perform the following steps:

  1. First, we need to import all the GUI libraries and the MapComposer library:
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from qgis.core import *
    from qgis.gui import *
    import MapComposer
    
  2. Next, we create the map composition using the shapefile:
    lyr = QgsVectorLayer("/qgis_data/ms/mmississippi.shp", "Mississippi", "ogr")
    reg = QgsMapLayerRegistry.instance()
    reg.addMapLayer(lyr)
    mr = iface.mapCanvas().mapRenderer()
    qc = MapComposer.MapComposer(qmlr=reg, qmr=mr)
    
  3. Now, we are going to create some variables to shorten some unusually long method and object names:
    setGridAnnoPos = qc.composerMap.setGridAnnotationPosition
    setGridAnnoDir = qc.composerMap.setGridAnnotationDirection
    qcm = QgsComposerMap
    
  4. Then, we enable the grid, set the line spacing, and use solid lines for the grid:
    qc.composerMap.setGridEnabled(True)
    qc.composerMap.setGridIntervalX(.75)
    qc.composerMap.setGridIntervalY(.75)
    qc.composerMap.setGridStyle(qcm.Solid)
    
  5. Next, we enable the annotation numbers for coordinates and set the decimal precision to 0 for whole numbers:
    qc.composerMap.setShowGridAnnotation(True)
    qc.composerMap.setGridAnnotationPrecision(0)
    
  6. Now, we go around the map composition frame and define locations and directions for each set of grid lines, using our shorter variable names from the previous steps:
    setGridAnnoPos(qcm.OutsideMapFrame, qcm.Top)
    setGridAnnoDir(qcm.Horizontal, qcm.Top)
    setGridAnnoPos(qcm.OutsideMapFrame, qcm.Bottom)
    setGridAnnoDir(qcm.Horizontal, qcm.Bottom)
    setGridAnnoPos(qcm.OutsideMapFrame, qcm.Left)
    setGridAnnoDir(qcm.Vertical, qcm.Left)
    setGridAnnoPos(qcm.OutsideMapFrame, qcm.Right)
    setGridAnnoDir(qcm.Vertical, qcm.Right)
    
  7. Finally, we set some additional styling for the grid lines and annotations before adding the whole map to the overall composition:
    qc.composerMap.setAnnotationFrameDistance(1)
    qc.composerMap.setGridPenWidth(.2)
    qc.composerMap.setGridPenColor(QColor(0, 0, 0))
    qc.composerMap.setAnnotationFontColor(QColor(0, 0, 0))
    qc.c.addComposerMap(qc.composerMap)
    
  8. We output the composition to an image:
    qc.output("/qgis_data/map.jpg", "jpg")
    

Verify that your output image looks similar to the following:

How to do it...

How it works...

This recipe has a lot of steps because the grids are customizable. The order of operations is important as well. Notice that we do not add the map to the composition until the very end. Often, you will make what seem to be minor changes and the grid may not render. Hence, modify this recipe carefully.

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

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