An Open Postcode Geocoding API

April 30th, 2010

Because I needed one for various (non-profit) projects of my own, I’ve put together a small Postcode geocoding web service which collates partial data from various sources into what aims to be a full geocoder / reverse geocoder for UK postcodes.

Now, the term ‘geocoding’ can mean several very subtly different things depending on who you talk to. So to clarify, this is what the web service does.

  1. It takes a full UK postcode
  2. It returns a geographical location (lat/lng and optionally os/landranger grid references)
  3. It also attempts to reverse geocode the street address via the Google Maps API (experimental)

Isn’t that Illegal?

Not anymore. Ordnance Survey have open sourced huge chunks of their data. This includes their CodePoint file which contains the coordinates of every single UK postcode.

Usage

There’s nothing complicated involved in calling the service. Simply make an HTTP request in the following format, where SW1A 2AA is your postcode; or more accurately that of 10 Downing Street.

Get the data in XML format
http://geo.jamiethompson.co.uk/SW1A2AA.xml

<result>
  <status>200</status>
  <postcode>SW1A 2AA</postcode>
  <geo>
    <os_x>530047</os_x>
    <os_y>179951</os_y>
    <lat>51.50354</lat>
    <lng>-0.127696</lng>
    <landranger>TQ300799</landranger>
    <accuracy>1</accuracy>
  </geo>
  <address>
    <street>Downing St</street>
    <locality>London</locality>
    <district>Westminster</district>
    <county>Greater London</county>
  </address>
</result>

The data is also available as JSON
http://geo.jamiethompson.co.uk/SW1A2AA.json

{
   "status":200,
   "postcode":"SW1A 2AA",
   "geo":{
      "os_x":"530047",
      "os_y":"179951",
      "lat":"51.50354",
      "lng":"-0.127696",
      "landranger":"TQ300799",
      "accuracy":"1"
   },
   "address":{
      "street":"Downing St",
      "locality":"London",
      "district":"Westminster",
      "county":"Greater London"
   }
}

and additionally as JSONP by specifying a callback
http://geo.jamiethompson.co.uk/SW1A2AA.json?callback=doStuff

Things You Should be Aware of

The data returned should be pretty self documenting but there’s a few things you should be aware of.

Although, more often than not you will receive a full dataset. The only guaranteed values are lat and lng. All other values of geo are optional. Additionally to this, if the accuracy level of a result is 2 then the coordinates relate to the broader postcode area rather than the postcode itself. An accuracy 2 result for the postcode JE3 8FA* will return the centre point of the JE3 8 area.

* All channel island postcodes are returned at accuracy level 2

Northern Ireland (BTXX XXX) postcodes don’t return ordnance survey osgrid coords. Lat/lng values however are accurate to level 1.

A final word

This is an experimental web service. Please don’t hammer it for anything humongous or commercial. The reverse geocoding is experimental and cannot be relied upon for anything super-serious at the moment. If you need this data commercially please do yourself (and me) a favour and check out the reasonably priced services offered by the likes of AFD (which I can personally recommend) or PostcodeAnywhere

Posted in Projects, Web | Tagged with: , ,

7 Responses

  1. Full UK Postcode Database for Free – Jamie Thompson

    [...] New Project: Open Postcode Geocoding Web Service [...]

  2. Peter

    I was wondering if you could say what sources you’ve used to do this API? Did you use the OS OpenData that they supply for all the UK postcodes? What did you use to get the address details? Any pointers would be greatly appreciated.

  3. Jamie Thompson

    The coordinates aren’t yet coming from the OS OpenData, but they will be once I get around to importing it all. This was originally developed pre April 1st 2010 before OS opened everything up.

    The address details are gained by doing a reverse lookup on the coordinates using Google’s Geocoding Web Service.

  4. Alex

    Hi Jamie. Very useful site.

    I’m trying to find postcodes from UK addresses (sort of a reverse lookup). The easiest thing would be to have a table with a correspondence between UK addresses and postcodes as to extract both. Is there such a thing?

  5. Jamie Thompson

    I don’t think you’ll have much luck with that without purchasing the PAF file, or using a commercial api based on the PAF file like AFD or Postcode Anywhere.

    The Google Geocoding API does a pretty good job of geocoding free text addresses but only currently gives UK postcodes to the AAN N level.

    It all depends on how accurate you need the data

  6. David Jacobs

    Jamie.. I have a studio rental site.. and am trying to make a postcode search similar to hotblackdesiato.co.uk but at the mo some people have entered invalid postcodes.. so it gives an error message on the google map.. so i need a full database of uk postcodes to compare them to to see what invalid.. what do you reckon I should do

    this is what happens now

    http://bdmonster.com/rentmystudio.co.uk/all-studio.php

  7. PB

    Hey Jamie, nice blog!

    Not sure if it’s a false positive or something but clicking the link in David Jacobs comment brings up a virus warning. Might be worth taking it down..

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.