-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance_transform.py
More file actions
29 lines (25 loc) · 843 Bytes
/
distance_transform.py
File metadata and controls
29 lines (25 loc) · 843 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
# !/usr/bin/env python3
# -*- coding: utf-8 -*
from math import sqrt, sin, cos, radians, asin, fabs
EARTH_RADIUS = 6378.1
n_mile = 1.852
def get_distance_hav(lat0, lng0, lat1, lng1):
'''
Using haversine equation to calculate the distance between two points
transfer the latitude and longitude into radian representation.
:param lat0:
:param lng0:
:param lat1:
:param lng1:
:return: two points distance using nautical miles to represent
'''
lat0 = radians(float(lat0))
lat1 = radians(float(lat1))
lng0 = radians(float(lng0))
lng1 = radians(float(lng1))
dlng = fabs(lng0 - lng1)
dlat = fabs(lat0 - lat1)
h = sin(dlat / 2.) ** 2 + cos(lat0) * cos(lat1) * sin(dlng / 2.) ** 2
distance = 2 * EARTH_RADIUS * asin(sqrt(h))
distance1 = distance / n_mile
return distance1