Skip to content

Instantly share code, notes, and snippets.

@popey456963
Last active May 20, 2016 20:28
Show Gist options
  • Save popey456963/9f6bf9e7eab67bea6e210a43077f9180 to your computer and use it in GitHub Desktop.
Save popey456963/9f6bf9e7eab67bea6e210a43077f9180 to your computer and use it in GitHub Desktop.
"""
Author: Popey456963
Date: 20/05/2016 20:30
Desc: Postcodes
"""
from math import radians, cos, sin, asin, sqrt
AVG_EARTH_RADIUS = 6371
def haversine(point1, point2):
lat1, lng1 = point1
lat2, lng2 = point2
lat1, lng1, lat2, lng2 = map(radians, (lat1, lng1, lat2, lng2))
lat = lat2 - lat1
lng = lng2 - lng1
d = sin(lat * 0.5) ** 2 + cos(lat1) * cos(lat2) * sin(lng * 0.5) ** 2
h = 2 * AVG_EARTH_RADIUS * asin(sqrt(d))
return h
postcode = input("<<< (Postcode) ")
postcode2 = input("<<< (Postcode 2) ")
point1 = ()
point2 = ()
with open("latlong.csv") as f:
lines = [i.strip().split(",")[1:] for i in f.readlines()]
lines.pop(0)
for i in lines:
if i[0] == postcode:
point1 = (float(i[1]), float(i[2]))
if i[0] == postcode:
point2 = (float(i[1]), float(i[2]))
print(haversine(point1, point2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment