OpenData – UK Postcodes
The UK government has been publishing UK postcode data for nearly a year now. It is available for free here from the Ordnance Survey and is branded as Code-Point® Open.
Essentially the data provides 1.7 million UK postcodes, with their British National Grid Reference, local authority and ward listed for each. The most interesting data is the grid reference, supplied as a full Northing and Easting value. These can be used to calculate distances between postcodes, as well as being able to convert them into Latitude and Longitude data for displaying on maps such as Google’s offering.
The data does lack Northern Ireland and the Channel Island data at present, which can be a problem if you wish to use the data to cover the whole of the UK. The Northern Ireland grid system has a different origin and projection, so care must be taken if those postcodes and data are merged in from other sources. See here for details of The Irish Grid system.
There is a wiki that covers this data, but it is incomplete and has not been updated in nearly half a year, which is a bit strange. I suspect updates are being lost somewhere, because I cannot believe nobody wants to update it (there are technical inaccuracies in the description of the data that someone should have noticed by now). I have made a few additions to the wiki page, so we will see what happens next. If it sticks, I would encourage others to go in and add what you know and discover in the data.
So, what does this data look like? In short, taking into account the areas it does not cover, pretty much complete. I have loaded the current dataset into MySQL, then plotted each postcode as a pixel in an image. Some pixels contain more than one closely positioned postcodes, and a more sophisticated plot would show this, but a simple plot of pixels gives us an idea of what it looks like.
Click the image to look at a much larger version with lots of detail. I particularly love how you can see the valleys in South Wales.
This simple PHP script generated the above image. All the table needs to contain are the Eastings and Northings from the Ordnance Survey data. It is surprising how it manages to get through 1.7 million postcodes in about ten seconds:
<?php
$width = 3200;
$height = $width * 1.5;
$db_name = 'opendata_postcodes';
$db_user = 'username';
$db_pass = 'password';
$easting_max = 655448;
$northing_max = 1213660;
$max_points = 2000000;
$db = new PDO('mysql:host=localhost;dbname='.$db_name.';charset=UTF-8', $db_user, $db_pass);
$sql = 'SELECT easting, northing FROM postcode LIMIT :limit';
$stmt = $db->prepare($sql);
$stmt->bindParam(':limit', $max_points, PDO::PARAM_INT);
$stmt->execute();
$img = imagecreate($width, $height);
$background = imagecolorallocate($img, 230, 230, 255);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
$x = round(($width*$row['easting'])/$easting_max);
$y = $height - round(($height*$row['northing'])/$northing_max);
imagesetpixel($img, $x, $y, 300);
}
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
Here is the same image with some crude colours introduced to show the density of postcodes.

