An Open Postcode Geocoding API

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.
- It takes a full UK postcode
- It returns a geographical location (lat/lng and optionally os/landranger grid references)
- 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: api, geocoding, postcode


April 30th, 2010 at 8:20 pm
[...] New Project: Open Postcode Geocoding Web Service [...]
May 19th, 2010 at 5:43 pm
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.
May 19th, 2010 at 6:04 pm
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.
May 20th, 2010 at 9:29 pm
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?
May 21st, 2010 at 9:17 am
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
June 16th, 2010 at 2:08 pm
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
July 12th, 2010 at 10:57 pm
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..
October 14th, 2010 at 11:03 pm
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
October 14th, 2010 at 11:09 pm
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
November 21st, 2010 at 7:48 pm
Is therea UK API to take lat/long ie wgs84 and return accurate UK post codes?
February 18th, 2011 at 4:34 pm
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!
March 2nd, 2011 at 1:40 pm
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
March 29th, 2011 at 9:00 am
http://signechimes.insanejournal.com
August 7th, 2011 at 7:27 pm
[...] 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 [...]
October 5th, 2011 at 8:39 pm
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
November 19th, 2011 at 4:44 pm
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.
November 19th, 2011 at 8:04 pm
Interesting break down of the whole postcode system http://postcodepal.com/?page=matrix
Maybe this will help somebody here?
December 27th, 2011 at 3:12 pm
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.