Grids and Graticules in QGIS

Quantum GIS does not yet have a feature to add correctly labeled map grids or graticules.

[Update: Recent versions of QGIS, since 1.4, have a built in Grid creator as part of the Print Composer, so the juggling act described below is no longer necessary, unless you need two grids in different coordinate systems.]

However there is a way to manufacture a decent looking map grid, with the accepted labeling of grid coordinates around the edges. To do this you need:

  1. The full extent to be covered by a grid
  2. The intervals between grid lines that you will need
  3. OpenOffice for editing a dbf file.

Begin by choosing the project coordinate system for which you want to make the grid. Zoom to your full extent, and start up the “Vector Grid” tool from the Tools->Research menu. Enter values into each of the extent items, X Min, X Max, Y Min and Y Max so that the grid will cover the full extent that you’ll require. You should choose rounded values so that the grid lines are created at even X and Y values. Also enter a reasonable minimum value for X Parameter – which will be the spacing between lines. You most likely want to keep the ratio between X and Y locked at 1:1; i.e. same spacing in both directions. Choose to “Output grid as lines”, click Browse to choose an output shapefile name for the grid, and click OK.

You’ll now have a line shapefile which contains horizontal and vertical lines, your grid. Each entry in the resulting shapefile’s attribute table has a value in the COORD column, indicating the line’s X coordinate (for vertical lines) or the line’s Y coordinate (for horizontal lines). By examining the numbers it’s easy to find which are horizontal and which vertical grid lines. These COORD values will be used to label the grid lines. However, the problem now is to position the labels along the edge of the map, and get the Y coordinates of the horizontal lines to display vertically.

Most GIS users know that a shapefile’s attribute table is just a dbf file. This file can be edited in OpenOffice Calc. Open the grid shapefile’s dbf in OpenOffice (you’ll have to remove the grid from QGIS first), and add four new columns: ANGLE, X_OFFSET, Y_OFFSET, INTERVAL. Each of these new columns will be used in labeling the grid shapefile to get the grid values to display just right.

First, in the Angle column, enter a value of 0 for all the lines that are vertical lines, and enter 90 for all the horizontal lines. In a moment we’ll apply this angle to the labeling so that the horizontal line coordinate values will be rotated by 90 degrees.

In the two OFFSET columns we need to enter values to shift the labels to the edges of the map. Here’s how it’s done:  First for the vertical grid lines enter values in the X_OFFSET column equal to the COORD value (copy-paste from the COORD column). Then in the Y_OFFSET column put a value equal to the minimum (southern-most) value of your full extent. So each of the rows of vertical lines will have a different value in the X_OFFSET, but all will have the same Y_OFFSET.

Now for the horizontal lines, it’s the same idea but reversed: All the horizontal lines will get a value equal to the maximum easting of your extent in the X_OFFSET column, while in the Y_OFFSET you copy-paste for each row the COORD value for that row.

Move on to the INTERVAL column. The values in this column will be used to hide grid lines that are too close. For example, we might create grid lines at a spacing of 1000 or 5000 meters for large scale maps, but when we zoom out to a small scale, the grid will be too dense, and we need to use only those lines at 10,000 meter or even 50,000 meter spacing. So use a combination of the spreadsheet functions =IF and =MOD to get the interval values we need. Here’s the syntax:

  =IF( MOD(B2,10000)=0, 10000, 5000 )

Enter this equation in the first row (beneath INTERVAL) and copy down to all the other rows. This creates values of either 10000 or 5000 for each row, and these can be used in QGIS to show either the full grid at 5000 meter spacing, or a sparcer 10,000 meter grid. The above equation can, of course, be extended with “nested” IF’s  to create values of 1000,5000,10000… etc. or any other series of scales you need.

Before saving the dbf, you’ll want to format all cells with no decimal places (for map CRS having meters as units). Then save and close OpenOffice.

Now for the the magic. Load the grid shapefile back into your map, and open its Layer Properties. In the Symbology tab choose Unique Value, and select the INTERVAL column as the Classification field, and click Classify. Now you can use the “Delete Class” button to remove interval lines that you don’t want and leave only those at a suitable spacing for your map scale. Set the style to a thin dark gray line.

Now go over to the label tab, enable labeling, and select the COORD column for labels. On the left click “Data Defined Position” and for the X Coordinate select the column X_OFFSET, and for Y Coordinate select Y_OFFSET. Also, on the left click “Data Defined Alignment” and for the Angle choose the field ANGLE. Finally click OK to examine the results. You should have grid labels at the bottom and right of the grid. You’ll probably need to reopen the layer’s property window and select a small font suitable for the grid labels. Also, if necessary, you can go back into Openoffice and fine-tune the numbers a bit to shift the labels to your liking by making small changes in the OFFSET fields.

Yes, it’s a clunky procedure, but until there’s something better, this gets the job done…

Southern Israel map
Southern Israel with Grid

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>