Thursday May 23 2013 Hi !
This is a very handy file of UK postcode information, especially if you are developing a web application that relies on postcode information.
The file contains UK mainland "outcodes" (i.e. area codes - the first part of the postcode) which is good for narrowing things down to a couple of kilometres.
The list is formatted as plain text, with each record on a separate line. The first line is a heading line, so you may need to remove this to import to certain applications. However, applications such as MS Excel will handle this no problem.
Each record consists of five fields, each field being separated by a comma. You should be able to import this data into applications that support "comma separated variables". The five fields are:
The co-ordinates are in "metres" from a notional origin at the bottom left of the UK. [I get asked many questions about this. What I mean is, the 0,0 co-ordinate is somewhere in the sea off the coast of cornwall.]. While this may not seem immediately useful, actually it means you can easily calculate distances between places using Pythagoras theorem. This is much easier to do that to try and calculate distances using Longitude and Latitude.
[For those interested in a full discussion of co-ordinate systems there is an excellent guide by Ordnance Survey ]
I have now also added a second file to the bundle - it's the same as the first file but also contains an additional column with the postal town name associated with the outcode. You could use this to plot town-names on a map, or to confirm nearby towns when a user inputs a postcode.
get the files here: postcodes.zip
This file bundle now also contains SQL batch commands in a .sql file to directly import the data into your SQL database (e.g. mySQL)!
Below is an example of how I have used the data to provide mapping information of membership location at www.homeworkerzone.co.uk (in red) alongside BT Openzone hotspot locations (in grey). This is very easy to do in PHP. (You can get the code at the bottom of this article).
You will notice map is generated in real-time by a php script (code available for download at the bottom). Check the properties of the image, and you will see it is a php script that produces a png image on-demand. This means the image is always up-to-date and never needs changing or adjusting manually!
and a similar example from www.alphalocal.org.uk
Finally, if you want to get a free map of the outline of the UK on which to plot your data (again, easy in PHP) then visit either the ordnance survey outline maps page or the ordnance survey get-a-map site, where they very kindly provide a free service to download UK maps for use on your own website. (They are copyright, so I can't distribute them here).
This page is about trying to help you solve the particular mapping problem you have so I'm not going to go into the in's and out's of Pythagoras and how and why works.
However, as a refresher for those dusty memory banks, Pythagoras allows you to calculate the straight line distance between two points, using a very simple formula. Basically the distance-squared is equal to the sum of the squares of the x and y distances between your points. It's hard to put in writing, so formulaically:
Let's assume we have points A and B, and you have their X and Y co-ordinates (in metres) from the above postcode file. I.e. you have Xa, Ya and Xb, Yb. Now what pythagoras says is that:
distance = square-root ( (Xb-Xa)2 + (Yb - Ya)2 )
This will give you the distance in metres. In actual fact it doesn't matter which way round you have the b's an a's because the square is always positive.
The observant among you will quickly note that the straight line distance calculated above is not exactly the straight line distance in the real world, because in the real world the surface of the Earth is curved. So, theoretically the distance you would travel is probably slightly longer.
It's beyond the scope of this article to go into the detail, but suffice to say the above pythagoras calculation is a good enough approximation for the following reasons:
There are some articles on the web if you do want detailed explanations of the above.
Download Code Examples (php and sql)
ON PROMOTION - ON PROMOTION!
I've now written a 6 page document that provides PHP source code and SQL statements to solve two application scenarios when using the above data.
The first is to be able to plot the map above from database tables that contain postcodes of the things you want to plot (e.g. member locations, restaurants, business facilities, or anything you have postcode data for in your database).
The second is the problem of finding a set of postcodes (and optionally the things that are located at them - e.g. members, restaurants etc.) within a certain radius of a given postcode. So, say for example, you live at postcode NW1 and you want to find all the "whatevers" that are within 5km of that postcode - this code will do it for you (as long as you have a list of "whatevers" and their postcode in your database).
If you've ever thought about this problem, you'll realise it consists of several steps. Combining these steps into one stonking piece of SQL query that does it all in one go is no mean feat - but my code shows you how to do it!
You can see this in action at Alphalocal. (click on, or near one of the coloured dots).
I've had some VERY positive feedback about the document and code - with some people writing to say it's saved them a week of work! (It's even got me a couple of programming contracts!) It's been used by students for projects, webmasters for searching, and even a salesman for planning journeys to retailers! These people have all used words like "fantastic" and "delighted"
Here's the introduction from the document:
IMPORTANT - make sure you have a working email address before downloading - I have had problems with users whose email account is full or not working. You cannot download if your account is not working properly.
(please note, to be able to make use of this, you will need to have a basic understanding of php, sql, and be able to run a mySQL database on your website, and load data into it. The document will give you the SQL code - in php format - that you need. It's compatible with Dreamweaver. However, you WILL need to edit the code, such as table and database names, to match your current setup, so don't download it if you think you can't do that.)
Download the php source code and instructions in pdf format (there is a small paypal charge to cover bandwidth costs because this page and download has become so popular. I've recently had to raise the price slightly, which I do apologise for. Your best bet is to get it now in case I have to raise the price again! Please note: when paying by eCheque, download links are not delivered until the eCheque has cleared. See this FAQ)
SPECIAL OFFER - For a limited time I'm offering this for £1.49 - that's almost 70% discount off non-promotion price of £3.99!
You may also be interested in my two other website guides: "mastering search engine success" - which is about getting good search rankings and traffic to your website, and also "Banned from Google; what now?" which explains how your site can get banned from google, and how to get back in if it happens. I have a full article explaining these.
related items [tags: postcode php sql map web]
More about the web category
hints, tips, guidance and best-practice on using the web, making the most of the web, setting up your own websites, safe surfing etc.
More about the author
nik is webmaster and site administrator
Pick up an obscenely
cheap IPOD now from easypeasy.
|subscribe via feedburner | subscribe by Email||