Python Module for Distance between UK postcodes

Question:

I need to calculate distances between UK postcodes.
I don’t want to use a web api.
Does a python module/ library exist for this?
Or do I have to put together something of my own using data from the OrdnanceSurvey?

Thanks,

Asked By: slow_mondays

||

Answers:

1/You can use any rest geolocation api eg google maps, that would provide you accurate distance based on the pincodes.

2/ You can use any updated database which has post codes and latitude/longitude information, and use that information to calculate distance between the two points.

Helpful links:

i) http://blog.acmultimedia.co.uk/2008/03/uk-post-code-distance-calculator-using-phpmysql/

ii) Django – how can I find the distance between two locations?

Answered By: DhruvPathak

I don’t know of a directly usable module, but you could use GRASS or QGIS, both of which support python scripting so the functionality can be used as python modules. You would still need to figure out how to do it manually in either of these tools though, but that’s not really very difficult.

Answered By: aquavitae

See below for simple code using the postcodes module for python along with the google maps distance matrix API.

from postcodes import PostCoder
import requests
import json
import pprint

pc = PostCoder()
origin_postcode = str.upper(raw_input("Enter origin PostCode:"))
dest_postcode = str.upper(raw_input("Enter destination PostCode:"))

origin = str(pc.get('%s' % origin_postcode)['geo']['lat'])+','+str(pc.get('%s' % origin_postcode)['geo']['lng'])
dest = str(pc.get('%s' % dest_postcode)['geo']['lat'])+','+str(pc.get('%s' % dest_postcode)['geo']['lng'])

data = requests.get('https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%s&destinations=%s&key=AIzaSyDfTip2PrdaRkF1muCLP8REAk3FsLjmBrU' % (origin, dest))
new = json.loads(data.text)

miles = str(new['rows'][0]['elements'][0]['distance']['text']).replace('mi','miles')
duration = str((new['rows'][0]['elements'][0]['duration']['text']))

print 'nnThe distance from %s to %s is %s, taking approximately %s (driving).' % (origin_postcode, dest_postcode, miles, duration)
Answered By: Luke.py

This question is a bit old now but maybe worth pointing out the difference between polar coordinates (Long/Lat) used by Google and SatNavs and Eastings & Northings provided by the Ordnance Survey which shows how far a given postcode centroid is from a given datum point somewhere off to the South West of the Scilly Isles.

If you just want to work out distances it’s a one-liner using Pythagoras. EG Stonehenge to House of Parliament

>>> # Coordinates of Stonehenge and Westminster in E/Northings
... s = (412183, 142346)
... p = (530268, 179545)
... d = ((s[0] - p[0]) ** 2 + (s[1] - p[1]) ** 2) ** 0.5
... print (d)
123805.62517914927

I was looking for a library to convert from polar to Cartesian coordinates which is relatively tricky even on a perfect sphere which the world most certainly isn’t. I’m currently trying, and mostly failing, to get my head around this:
https://scipython.com/book/chapter-2-the-core-python-language-i/additional-problems/converting-between-an-os-grid-reference-and-longitudelatitude/

Answered By: inforightsjames
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.