Skip to content

Instantly share code, notes, and snippets.

@apruden
Last active February 11, 2016 03:11
Show Gist options
  • Save apruden/852056ddafeb0210cdcd to your computer and use it in GitHub Desktop.
Save apruden/852056ddafeb0210cdcd to your computer and use it in GitHub Desktop.
geo location distance
import sys, math
def get_bin(lat, lng):
'''bin position'''
lat = max(0, lat + 70) if lat < 0 else min(140, lat + 70)
lng = lng + 180
return int(2 * lat) + 280 * int(2 * lng)
def calculate_distance(lat1, lng1, lat2, lng2):
'''Distance between two points on the earth.'''
earth_radius = 6371.0009
dLat = math.radians(lat2 - lat1)
dLng = math.radians(lng2 - lng1)
sindLat = math.sin(dLat / 2)
sindLng = math.sin(dLng / 2)
a = math.pow(sindLat, 2) + math.pow(sindLng, 2) * math.cos(math.radians(lat1)) * math.cos(math.radians(lat2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
dist = earth_radius * c
return dist
if __name__ == '__main__':
print calculate_distance(0,0,0.5,0)
print calculate_distance(0,0,0,0.5)
print calculate_distance(70,0,70,0.5)
print get_bin(45.46536, -73.66585)
print get_bin(45.4719057, -73.6349034)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment