-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.py~
31 lines (25 loc) · 1.12 KB
/
distance.py~
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
import math
def degreesToRadians(degrees): #function to convert degrees to radians, required for conversion of gps coordinates into distance
return degrees * math.pi / 180;
def distanceInmBetweenEarthCoordinates(lat1, lon1, lat2, lon2): #function to calculate distance between two gps coordinates
#earthRadiusm = 6378137;#radius of the earth in meters
earthRadiusm=6371000
dLat = degreesToRadians(lat2-lat1)
dLon = degreesToRadians(lon2-lon1)
latt1 = degreesToRadians(lat1)
latt2 = degreesToRadians(lat2)
a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(latt1) * math.cos(latt2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
return earthRadiusm * c
def calculate_dist(waypoint):
dist=[]
total_dist=0
for i in range(len(waypoint)-1):
try:
dist.append(distanceInmBetweenEarthCoordinates(waypoint[i]['lat'],waypoint[i]['lon'],waypoint[i+1]['lat'],waypoint[i+1]['lon']))
#print(i,dist[i])
total_dist += dist[i]
except Exception as e:
#print(e)
pass
return total_dist