-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhaversine.py
More file actions
58 lines (38 loc) · 1.36 KB
/
Copy pathhaversine.py
File metadata and controls
58 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# Haversine formula example in Python
# Author: Wayne Dyck
# CalcLat calculates the Latitude in Meters and CalcLong calculates the Longitude in meters
#file haversine.py
import math
def distance(lat1, lat2, lon1, lon2):
#lat1, lon1 = origin
#lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = radius * c
distance *= 1000
return distance
def calcLong(lon1, lon2, lat1, lat2):
radius = 6371 # km
dlat = 0
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
longitudeInMeters = d * 1000
return longitudeInMeters
def calcLat(lat1, lat2):
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = 0
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
latitudeInMeters = d * 1000
return latitudeInMeters