Adding a north arrow to the map

North arrows are another common cartographic element found even in ancient maps, which show the orientation of the map relative to either true, gird, or magnetic north. Sometimes, these symbols can be quite elaborate. However, QGIS provides a basic line arrow element that we will use in combination with a map label to make a basic north arrow.

Getting ready

You will need to download the following zipped shapefile and extract it to your qgis_data directory, to a subdirectory named ms:

https://geospatialpython.googlecode.com/svn/Mississippi.zip

In addition to the shapefile, you will also need the MapComposer class to simplify the code needed to add this one element. If you haven't already used it in a previous recipe, you can download it from https://geospatialpython.googlecode.com/svn/MapComposer.py.

This file must be accessible from the QGIS Python Console; for this, you need to ensure that it is in the Python path directory. Place the file in the .qgis2/python directory within your home directory.

How to do it...

In this recipe, we will create a map composition, draw an arrow to the right of the map, and then place a label with a capital letter N below the arrow. To do this, we need to perform the following steps:

  1. First, we import the Qt and MapComposer Python libraries:
    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 object:
    lyr = QgsVectorLayer("/qgis_data/ms/mississippi.shp", "Mississippi", "ogr")
    reg = QgsMapLayerRegistry.instance()
    reg.addMapLayer(lyr)
    mr = iface.mapCanvas().mapRenderer()  
    qc = MapComposer.MapComposer(qmlr=reg, qmr=mr)
    
  3. Now, we calculate the position of the arrow along the right-hand side of the map, set its position, and then add it to the composition:
    mcw = qc.composerMap.rect().width()
    mch = qc.composerMap.rect().height()
    ax =  qc.x + mcw + 10
    ay =  (qc.y + mch) - 10
    afy = ay - 20
    qc.arrow = QgsComposerArrow(QPointF(ax, ay), QPointF(ax,afy), qc.c)
    qc.c.addItem(qc.arrow)
    
  4. Then, we create a capital letter N label and add it to the composition just below the arrow:
    f = QFont()
    f.setBold(True)
    f.setFamily("Times New Roman")
    f.setPointSize(30)
    qc.labelNorth = QgsComposerLabel(qc.c)
    qc.labelNorth.setText("N")
    qc.labelNorth.setFont(f)
    qc.labelNorth.adjustSizeToText()
    qc.labelNorth.setFrameEnabled(False)
    qc.labelNorth.setItemPosition(ax - 5, ay)  
    qc.c.addItem(qc.labelNorth)
    
  5. Finally, we save 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...

The QGIS composer doesn't have a dedicated north arrow or compass rose object. However, it is quite simple to construct one, as demonstrated in the preceding section. The arrow is just a graphic. The direction of the arrow is controlled by the location of the start point and the end point listed, respectively, when you create the QgsComposerArrow object.

There's more...

You can extend this example to have an arrow point in multiple compass directions. You can also use an image of a more elaborate compass rose added to the composition. We'll demonstrate how to add images in the next recipe. Note that the arrow element can also be used to point to items on the map with an associated label.

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

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