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

11 thoughts on “Grids and Graticules in QGIS

  1. Actually, the Composer in qgis does have a grid/graticule tool. This may have been new in qgis-1.4.0, which was not yet released when you posted this. It does a quite good job. I believe some of your newer posts show it in action.

  2. Indeed the Grid tab in the new Print Composer is a great addition, which we were anticipating for a long time. Kudos to Marco Hugentobler.

  3. thanks very much for the instructions to do grids trought out the vectors tools…i did not labelling yet…but my print composer do not have graticule creator!
    im working on Qgis Capiapó. So how can i get this plug-in???

  4. This post is quite old. There’s no need for any plugin now. The print composer has the ability to add a grid with labeling directy on the map.

    Micha

  5. Micha Silver :
    This post is quite old. There’s no need for any plugin now. The print composer has the ability to add a grid with labeling directy on the map.

    Micha

    This is true. It’s cool that they are improving on the print composer. Thanks for the info!

  6. Useful post – it is still valid as per poster’s update at the top. If your map is projected but you want a lat-long graticule, which is a common use case, you still have to follow this approach. Although you can now do all the attribute editing with the field calculator in QGIS, without having to edit the dbf in a spreadsheet.

  7. Not sure I agree with posts #4 or #5 – why wouldn’t I want to see graticules in the primary map view? I’d rather not have to go to print composer in order to see them when I’m playing with other data…

  8. Jeff Meyer :

    Not sure I agree with posts #4 or #5 – why wouldn’t I want to see graticules in the primary map view? I’d rather not have to go to print composer in order to see them when I’m playing with other data…

    In most GIS software I’ve seen there’s a clear separation between dealing with the GIS data layers, and creating the final cartographic output. Grids, legend, titles, etc are all cartographic elements that are usually handled separately, after the layers are arranged on the map canvas.

  9. QGIS has progressed phenominally. It is wonderful to work on QGIS and make excellent maps. Only thing lacking in print composer is putting a geographic grid on projected maps. GRASS GIS has a feature known as geogrid in psmap. If such a feature is added to QGIS print composer it will be wonderful.

  10. S.V.Hegde :

    QGIS has progressed phenominally. It is wonderful to work on QGIS and make excellent maps. Only thing lacking in print composer is putting a geographic grid on projected maps. GRASS GIS has a feature known as geogrid in psmap. If such a feature is added to QGIS print composer it will be wonderful.

    Well, the post above is very old. Today, if your map is projected into a Lat/Long coordinate system, then the Grid tab in the print composer will indeed display graticules on the final map.

    Micha

  11. The post is old and so is the problem but without solution.
    I think you have missed the point. If I project in Lat/Long I will surely be able to put grid with Lat/Long grid labels. But if I project it in UTM the grid label will be in meters. I want UTM projection, grid labels in degree, minutes, seconds and also I want scale bar in the from say 1cm = 1 km etc. I need this for some of my applications. This is possible in GRASS GIS, Arc GIS but I dont think possible in QGIS.

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>