-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdistance-between-two-latlon
More file actions
32 lines (24 loc) · 925 Bytes
/
distance-between-two-latlon
File metadata and controls
32 lines (24 loc) · 925 Bytes
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
from math import radians, sin, cos, sqrt, atan2
def distance_between_lat(pointA=[], pointB=[], m=True):
"""
This function calculate distances from two longitude and latitude coordinates and
return distance in Miles or Kilometers if third parameter is false.
distance_between_lat([51.2296756,212.01221287],[523.4063974,17.82510681],False)
Result: 4053.2581873083705
"""
R = 6373.0
M = 0.621371
# 1 kilo * 0.621371 = 1 mile.
lat_ax = radians(pointA[0])
lat_ay = radians(pointA[1])
lat_bx = radians(pointB[0])
lat_by = radians(pointB[1])
distance_lon = lat_by - lat_ay
distance_lat = lat_bx - lat_ax
a = sin(distance_lat / 2)**2 + cos(lat_ax) * cos(lat_bx) * sin(distance_lon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
if(m):
distance = R * c * M
else:
distance = R * c
print("Result:", distance)