-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculating_dist_btw_customers.py
53 lines (47 loc) · 2.19 KB
/
calculating_dist_btw_customers.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## 거리 구하는 함수
import numbers
import math
class GeoUtil:
"""
Geographical Utils
"""
@staticmethod
def degree2radius(degree):
return degree * (math.pi/180)
@staticmethod
def get_harversion_distance(x1, y1, x2, y2, round_decimal_digits=5):
"""
경위도 (x1,y1)과 (x2,y2) 점의 거리를 반환
Harversion Formula 이용하여 2개의 경위도간 거래를 구함(단위:Km)
"""
if x1 is None or y1 is None or x2 is None or y2 is None:
return None
assert isinstance(x1, numbers.Number) and -180 <= x1 and x1 <= 180
assert isinstance(y1, numbers.Number) and -90 <= y1 and y1 <= 90
assert isinstance(x2, numbers.Number) and -180 <= x2 and x2 <= 180
assert isinstance(y2, numbers.Number) and -90 <= y2 and y2 <= 90
R = 6371 # 지구의 반경(단위: km)
dLon = GeoUtil.degree2radius(x2-x1)
dLat = GeoUtil.degree2radius(y2-y1)
a = math.sin(dLat/2) * math.sin(dLat/2) \
+ (math.cos(GeoUtil.degree2radius(y1)) \
*math.cos(GeoUtil.degree2radius(y2)) \
*math.sin(dLon/2) * math.sin(dLon/2))
b = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
return round(R * b, round_decimal_digits)
@staticmethod
def get_euclidean_distance(x1, y1, x2, y2, round_decimal_digits=5):
"""
유클리안 Formula 이용하여 (x1,y1)과 (x2,y2) 점의 거리를 반환
"""
if x1 is None or y1 is None or x2 is None or y2 is None:
return None
assert isinstance(x1, numbers.Number) and -180 <= x1 and x1 <= 180
assert isinstance(y1, numbers.Number) and -90 <= y1 and y1 <= 90
assert isinstance(x2, numbers.Number) and -180 <= x2 and x2 <= 180
assert isinstance(y2, numbers.Number) and -90 <= y2 and y2 <= 90
dLon = abs(x2-x1) # 경도 차이
if dLon >= 180: # 반대편으로 갈 수 있는 경우
dLon -= 360 # 반대편 각을 구한다
dLat = y2-y1 # 위도 차이
return round(math.sqrt(pow(dLon,2)+pow(dLat,2)),round_decimal_digits)