easypeasy - making the web simple
login to free webmeail
daily news headlines
guides and articles
nik's blog
Saturday October 25 2014 Hi !
Bookmark and Share Bookmark and Share Subscribe

Free UK postcode data file Longitude Latitude SQL PHP code search Saturday, 06/08/2005 by nik

Free file downloads of UK postcode data which contains distance and longitude/latitude information. This allows you to search, plot and find distance between postcode areas. Plus php/sql source code and instructions to calculate distance between postcodes and do proximity searches and plot maps.

was this article helpful? [66]
related resources

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:

  1. First part of the UK postcode (area code)
  2. x-coordinate (metres)
  3. y-coordinate (metres)
  4. Latitude
  5. Longitude

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!

map of UK members of homeworkerzone

and a similar example from www.alphalocal.org.uk

live alpha map

 

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

Pythagoras theorem

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.

Accuracy

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:

  • The margin of error is less than 1%
  • The computation time is much quicker than more accurate calculations, and this is an important benefit.
  • (And will your users really notice or understand the difference?)

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:

This document describes two techniques. Based on using a database (in this case MySQL) and the postcode file we provide it shows you how to create a map using those postcodes (or a subset, such as a member database).

Secondly, it shows you how to construct a search by distance. I.e. for a given postcode, and a given radius, it shows you how to find the postcodes that are within the radius. This can then also be used to extract a list of members. So, for example, by selecting one member (and their postcode) you can then find all the other members that are within the specified radius.

Please note that when we talk about postcode here we are concerned only with the first part of the UK postcode - it's called the outcode. This generally covers a region or metropolitan area or district rather than individual street. Postcode files down to street level are generally quite expensive, although the techniques here would work with detailed data if you have access to it.

 

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.


permalink: http://postcodes.easypeasy.com
tags: UK postcode data, search postcodes, map postcodes, php code, sql, postcode distance.

related items [tags: postcode php sql map web]

back to main index | more articles in this category | add to/query this article?
rss feed for this category use this link to subscribe ONLY to this category, otherwise use the other "subscribe" links

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

search the easypeasy shop

Pick up an obscenely cheap IPOD now from easypeasy.
It's the law!
dirt cheap ipods





Easy Guides
in web & email...
[effective email]


subscribe via feedburner | subscribe by Email

 


© 2014 easypeasy | Search | network | privacy | terms | feedback | about | FAQs | links | printable version