Georeferencing vector data using QGIS and ogr2ogr

This post will cover:

  1. Digitising rasters using QGIS.
  2. Loading data to PostGIS using SPIT.
  3. Reprojecting raster data using QGIS.
  4. Lines within a polygon selection in PostGIS.
  5. Georeferencing vector files using ogr2ogr, search for “The georeferencing:” to skip the other steps.

Georeferencing vector data has long been very difficult using the open-source GIS stack. That has all changed with the release of GDAL 1.10.0. Now georeferencing can be done using the vector translator ogr2ogr.

About half a decade ago I wrote my undergraduate dissertation on: “The influence of the Blaeu Atlas of Scotland on subsequent maps of Scotland”. While I was very proud of the work at the time, and the grade was good, the markers comments could be paraphrased as: A very good literature review on the Blaeu Atlas, however somewhat weak on analysis”. After a Master’s degree I could not agree more. At the time I lacked the knowledge and experience of working with projections. The release of GDAL 1.10.0 gives the perfect opportunity to return and correct those mistakes.

One of my favourite aspects of the Blaeu analysis was a comparison of the Blaeu 1654 Atlas of Scotland coastline to the coastline of subsequent maps of Scotland. I originally did this in GIMP and manually resized the outlines to close approximations. The end result looked good, but it lack real scientific rigour.

Original comparison:


A huge thanks to the National Library of Scotland for their work in digitising the Blaeu Atlas of Scotland. A great online version and overview can be found at:

With the outline used today found at:

Originally the maps were provided as .jpeg files with a simple viewer used for zooming. This meant that editing the URL would allow one to retrieve a very large version of the of the map directly. Now the maps are served using a a very nice javascript map viewer and digital copies can be purchased.

First we need to create a vector outline from the raster image. This is a simple case of adding the .jpeg image in as a raster file. When prompted for a CRS I chose EPSG:4030 “Unknown datum based upon the WGS 84 ellipsoid” this is just used for meta-data at this point. What you choose does not matter. We just want a vector file in cartesian co-ordinates. We create a new shapefile layer and trace outline. My co-ordinate capture will be very rough. If you are working with more modern maps, you should be as careful as required:

Screenshot from 2014-01-20 20:43:20 

Whenever I needed a break I would end the line. Then I could start a new line snapped to the end of the last one. In the end you can select all of the line segments and “merge selected features” from the advanced digitising toolbar. This would likely have been enough, but I was concerned there would be two overlapping nodes and that I would have a multi-part line. So I exploded the lines into points using the Vector>Geometry Tools>Extract nodes tool. Then Using the handy Points2One plugin (thanks Pavol Kapusta) I could stitch the nodes back together into a single line. If you’re going to do something might as well do it correctly. In the end we have 5116 nodes:

Screenshot from 2014-01-20 23:45:09

With the Blaeu outline captured we need a basemap to georeference it to. In the end we want to re-projectiong it into the WGS 84 / World Mercator projection (EPSG:3395). Mercator will provide a good base of what the original mapper would have been trying to capture. In the UK we can turn to the Ordnance Survey for a basemap, however that basemap needs to be reprojected to EPSG:3395. This is simply done in within QGIS by selecting the “Enable on-the-fly CRS transformation” to EPSG:3395 from Project>project Properties>CRS tab.

From the Ordnance Survey (OS) we have a few open datasets to choose from:

  • OS BoundaryLine (High Water Polyline)
  • OS BoundaryLine (european electoral regions)

We can see how they line up with the raster products:

  • OS MiniScale
  • OS 250K

We can see that the High Water Polyline follows MiniScale much better than the electoral regions:

Screenshot from 2014-01-21 20:13:23Screenshot from 2014-01-21 20:13:41

It doesn’t line up perfectly with the 250k raster (250k is probably too accurate for this study):

Screenshot from 2014-01-21 20:19:07

But on the whole it will serve for this purpose, in addition Scotland can be easily extracted:

Screenshot from 2014-01-21 21:12:47Screenshot from 2014-01-21 21:04:42

Now we have the High Water Polyline, which looks good, but we only want mainland Scotland. Easy right?

Well… slighlty more complex. Unfortunately even the Scottish high water lines consisted of 16030 line segments. Select by location in QGIS using the European Electoral Regions (polygon) took too long (hours rather than minutes). So I had to resort to PostGIS to do the selection.

SPIT to PostGIS:

Screenshot from 2014-01-21 21:51:16

For setting up a PostGIS database see my previous post: Setting up PostgreSQL and PostGIS on Linux Mint.

The query in PostGIS/PostgreSQL to select all of the lines within a polygon (we use the OS boundary line electoral regions):


Query returned successfully: 4994 rows affected, 764779 ms execution time.

The 12 minute run time says a lot about my poor computer.

Then the lines are joined together, the geometries simplified, and multipart converted to singlepart. This allows us to get rid of the final islands. With the result back in QGIS:

Screenshot from 2014-01-22 18:03:40


Screenshot from 2014-01-23 00:04:17

Final result projected into WGS 84 / World Mercator projection (EPSG:3395), which really shows the distortion caused by the British National Grid (EPSG:27700):

Screenshot from 2014-01-23 00:12:45

We now have a cartesian vector file ready for georeferencing, an OS raster background map to use for georeferencing (OS MiniScale) and an OS vector outline BoundaryLine (High Water Polyline) to use for the final comparison.

The georeferencing:

We open up two QGIS projects. In one we have our cartesian vector file. In the other we have our basemap:

Screenshot from 2014-01-23 00:16:01

Using the co-ordinate capture tool, we capture the same point from both projects. Basically we are just capturing co-ordinate pairs from the two maps. The “Copy to clipboard” feature comes in very handy here.

Screenshot from 2014-01-21 18:32:34

These captured co-ordinates should be pasted into a text file. Something like this:

Of note here is that the co-ordinate capture tool captured the co-ordinate twice for me. How it works is that it captures one in the original CRS and the selected one. It is also important to keep your co-ordinates in order. I captured the cartesian co-ordinates on the first row and the WGS84 ones on the second row. Also I have attached memorable names to the points, in case there is an issue, the culprit point can be easily be identified.

For my project 23 points were captured:

Screenshot from 2014-01-23 00:48:57

After the points are captured we switch to ogr2ogr to perform the actual reprojection and to convert the OS outline into World Mercator.

Scotland Mainland Outline from Boundary Line High Water Mark to World Mercator (while I’m converting I will convert it to a GeoJSON for viewing in leaflet (this will be covered by a later post) and strip out any attributes to decrease the file size):

The key here is the -gcp tag. For each control point (co-ordinate pair) we have one tag:

Turns into:

So: -gcp ungeoref_x ungeoref_y georef_x georef_y

One -gcp tag for each point.

Means that it will use the a first order polynomial transformation, so affine. For myself the less distortion the better.

The end result is great, it really shows how advanced the Blaeu Atlas was for a 1654 map:


If you are interested in buying a historically amazing map:

Blaeu Atlas Maior of 1665 – Including the atlas of Scotland.

Also please visit the NLS, without who this would have been impossible, they also have great changing exhibits:

National Library of Scotland

NLS Exhibitions

Leave a Reply

Your email address will not be published.