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: , ,

18 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..

  8. Jamie

    Hey,

    I’ve found Yahoo’s service is more accurate than google’s for reverse geocoding. You might want to try it out.

    http://where.yahooapis.com/geocode?q=51.50354,+-0.127696&gflags=R&appid=yourappidhere

  9. Jamie

    Actually, this one does it in one step:

    http://where.yahooapis.com/geocode?line3=SW1A2AA&gflags=LR&locale=en_GB&appid=yourappidhere

    Link to the guide for full parameter list:
    http://developer.yahoo.com/geo/placefinder/guide/index.html

  10. David Bovill

    Is therea UK API to take lat/long ie wgs84 and return accurate UK post codes?

  11. Jay

    Hey Guys,

    Im wondering if its possible to use this to do a postcode-address lookup?

    E.g. user enters a postcode and it displays a list of addresses that match?

    Thanks!

  12. Ivan

    Hi,

    I am also interested in the postcode lookup functionality.

    I just need the tag returned. Would you mind contacting me via email, so we can discuss?

    Thanks
    Ivan

  13. taiduolea

    http://signechimes.insanejournal.com

  14. Childcare.co.uk

    [...] close but it could be a little bit off as postcodes don't always match up exactly with locations. Here is more about a Postcode Geocoder The accuracy of coding the postcode to the lat/long will depend on the database being used. Some [...]

  15. Matt Roberts

    If it’s of use to anyone, I’ve done the reverse of this – i.e. you can find a postcode from a lat/lon location, complete with API – http://postcod.es

  16. Prats keeds

    I want a list of lat-long co-ordinates from a list of UK towns. The list is long with 1000 towns. Please help in the same and how am I going to execute the “xml” file on the article written to get lat/long of my towns list.

  17. Dean

    Interesting break down of the whole postcode system http://postcodepal.com/?page=matrix

    Maybe this will help somebody here?

  18. Les

    Hello

    I appreciate what you have done; I wish for this functionality for ecommerce however don’t see why I should have to burden the client.

    Anyway you could open source the code behind your API or at least let me have a look at it?

    Thanks.

Leave a Comment

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