Want to know the distance from 1 postcode to 1000 others

Soldato
Joined
31 May 2006
Posts
4,239
Location
127.0.0.1
Hi all

I have one post code which is my destination. And 1,000 others which are my sources. I want to know the distance between the destination post code and each of the source post codes. BUT!!! I need the distance to travel by road not as the crow flies. So it would give me:

N1 1AA to L1 1AA = 5.7miles
N7 2XX to L1 1AA = 12.8miles
etc.....

Any ideas?
 
Hmmm, you'd think by posting this question in the 'Programming' forum that I was expecting a programmatic response.

Well you could make a program that put the start and end post codes into google maps and get the results for all of them. That wouldn't be too hard.
 
Ok, how would you do that?

Well the url for google maps to put in a post code is like this:

Code:
http://maps.google.com/maps?f=d&source=s_d&saddr=cb1&daddr=ln2&hl=en

That goes from cb1 to ln2. You can change the post codes by changing saddr and daddr in the url.

Then you can get the source code of the page and find the text "99.9 mi" which happens to be in this

Code:
<div class="altroute-info">99.9 mi</div>
 
Most 3rd party map providers have functions that do this type of stuff really nicely.

http://www.telogis.com/ are an example, which my company used to mock up something for one of out customers. Is quite expensive though.

If you trying to do it for free though, your going to have to hack around screen scraping like yhack suggests.
 
I'm sure with Google you have to display the map as part of the API usage T&Cs (was a while ago I looked into it so they could have changed it). Also scraping the data from Google maps is messy and against T&Cs. However using a supplier will be more accurate as distances tend to be calculated by route.

If you just need 'as the crow flies' then you can buy in the postcode data, roughly £99 (plenty of suppliers other than royal mail), and then use the haversine/vincenty formula to calculate. This is what I’ve done with projects in the past as I found it less ‘messy’ that use the Google API at the time.
 
I'm sure with Google you have to display the map as part of the API usage T&Cs (was a while ago I looked into it so they could have changed it). Also scraping the data from Google maps is messy and against T&Cs. However using a supplier will be more accurate as distances tend to be calculated by route.

If you just need 'as the crow flies' then you can buy in the postcode data, roughly £99 (plenty of suppliers other than royal mail), and then use the haversine/vincenty formula to calculate. This is what I’ve done with projects in the past as I found it less ‘messy’ that use the Google API at the time.

You might be right with the Google Maps API T&C:

10.12 use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, the Street View API Documentation, or through written permission from Google (for example, you must not use geocodes obtained through the Service except in conjunction with a Google map, but the Street View API Documentation explicitly permits you to display Street View imagery without a corresponding Google map);

So I guess that rules that out (unless you get written permission from Google).

You could use OpenLayers/OpenStreetMap as an alternative?
 
If you just need 'as the crow flies' then you can buy in the postcode data, roughly £99 (plenty of suppliers other than royal mail), and then use the haversine/vincenty formula to calculate. This is what I’ve done with projects in the past as I found it less ‘messy’ that use the Google API at the time.

Does any know of any free or really cheap database that contains countrys, regions, citys and longitude and latitude information and perhaps even postcode data that I could use to calculate distances. I found one free database but its highly inaccurate in almost every table.
 
For anyone wondering how I eventually did this, I took yhack's suggestion of using the url and pulling the value from between the div declaration.

I used a VBscript to loop through a SQL server table, providing the postcodes to search between, generate the required url and then fired off wget to download the resulting page. Then the VBScript stripped out the value from the downloaded webpage and populated the SQL server table with the distance.

The only problem I encountered was that after the 338th request in 6 minutes, Google blocked me! Lol. Obviously I needed to add a delay in. The block was removed after a few hours and I continued with a 30 second delay for each request.

Thanks to everyone who contributed a (sensible) solution ;)
 
If it is for non-commercial purposes and just for mucking about, you can acquire the PAF database on various torrent sites.

Thanks for finding out the rate limit of Big G btw. It is always helpful to have an idea what the cut off is =). It definitely varies from service to service though as i once made 15,000 requests in an hour to google suggest.
 
Cant find the PFA database on any torrents sites. Im willing to pay for a database if and when my site goes live but just now IM just really looking for proof of concept. A world database of countrys, regions, citys and longitude and latitude of the citys is really what Im after at the moment.

I found the following sites offering this but can anybody confirm the accuracy of any of these?

http://www.worldcitiesdatabase.com

http://citydatabase.us

http://www.placedatabase.com/

citydatabase.us isthe cheapest but surly this wont be accurate?

I also found this : http://www.geopostcodes.com/

Which is probably the best iv seen but expensive. Its also cool how they protect their info from web scraping!
 
For anyone wondering how I eventually did this, I took yhack's suggestion of using the url and pulling the value from between the div declaration.

I used a VBscript to loop through a SQL server table, providing the postcodes to search between, generate the required url and then fired off wget to download the resulting page. Then the VBScript stripped out the value from the downloaded webpage and populated the SQL server table with the distance.

The only problem I encountered was that after the 338th request in 6 minutes, Google blocked me! Lol. Obviously I needed to add a delay in. The block was removed after a few hours and I continued with a 30 second delay for each request.

Thanks to everyone who contributed a (sensible) solution ;)

I'm intrigued as to what replies you thought were not sensible?
 
Back
Top Bottom