-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_location.py
103 lines (85 loc) · 2.65 KB
/
new_location.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
from dotenv import load_dotenv
import requests
import json
# 현재 접속 위치 위도, 경도 값 가져오기
load_dotenv()
google_maps_api_key = os.environ.get('google_maps_api_key')
url = f'https://www.googleapis.com/geolocation/v1/geolocate?key={google_maps_api_key}'
response = json.loads(requests.post(url).text)
location = response['location']
lat = round(location['lat'], 10)
lng = round(location['lng'], 10)
# lat_lng = [lat, lng]
# print(lat_lng)
nx_lat = lat
ny_lng = lng
# 위도, 경도 값 지역 코드로 변환
import math
NX = 149 ## X축 격자점 수
NY = 253 ## Y축 격자점 수
Re = 6371.00877 ## 지도반경
grid = 5.0 ## 격자간격 (km)
slat1 = 30.0 ## 표준위도 1
slat2 = 60.0 ## 표준위도 2
olon = 126.0 ## 기준점 경도
olat = 38.0 ## 기준점 위도
xo = 210 / grid ## 기준점 X좌표
yo = 675 / grid ## 기준점 Y좌표
first = 0
if first == 0 :
PI = math.asin(1.0) * 2.0
DEGRAD = PI/ 180.0
RADDEG = 180.0 / PI
re = Re / grid
slat1 = slat1 * DEGRAD
slat2 = slat2 * DEGRAD
olon = olon * DEGRAD
olat = olat * DEGRAD
sn = math.tan(PI * 0.25 + slat2 * 0.5) / math.tan(PI * 0.25 + slat1 * 0.5)
sn = math.log(math.cos(slat1) / math.cos(slat2)) / math.log(sn)
sf = math.tan(PI * 0.25 + slat1 * 0.5)
sf = math.pow(sf, sn) * math.cos(slat1) / sn
ro = math.tan(PI * 0.25 + olat * 0.5)
ro = re * sf / math.pow(ro, sn)
first = 1
#위도, 경도 -> 지역코드로 변환
def mapToGrid(lat, lon, code = 0 ):
ra = math.tan(PI * 0.25 + lat * DEGRAD * 0.5)
ra = re * sf / pow(ra, sn)
theta = lon * DEGRAD - olon
if theta > PI :
theta -= 2.0 * PI
if theta < -PI :
theta += 2.0 * PI
theta *= sn
x = (ra * math.sin(theta)) + xo
y = (ro - ra * math.cos(theta)) + yo
x = int(x + 1.5)
y = int(y + 1.5)
return x, y
# 지역코드 -> 위도, 경도 변환
def gridToMap(x, y, code = 1):
x = x - 1
y = y - 1
xn = x - xo
yn = ro - y + yo
ra = math.sqrt(xn * xn + yn * yn)
if sn < 0.0 :
ra = -ra
alat = math.pow((re * sf / ra), (1.0 / sn))
alat = 2.0 * math.atan(alat) - PI * 0.5
if math.fabs(xn) <= 0.0 :
theta = 0.0
else :
if math.fabs(yn) <= 0.0 :
theta = PI * 0.5
if xn < 0.0 :
theta = -theta
else :
theta = math.atan2(xn, yn)
alon = theta / sn + olon
lat = alat * RADDEG
lon = alon * RADDEG
return lat, lon
n_nx, n_ny = mapToGrid(nx_lat, ny_lng) # 위도 경도 값(float) 지역코드로 변환 (x,y 좌표)