-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglmtf.py
94 lines (72 loc) · 2.77 KB
/
glmtf.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
import googlemaps
import re
from pprint import pprint
# from time import sleep
# EXCEPTION
class informationWrong(Exception):pass
# GOOGLE API
class googleUClientCreater:
def __init__(self,key):
self.gmaps = googlemaps.Client(key=key)
def getInformation(self,center):
res = self.gmaps.reverse_geocode(center,language='zh-TW')
res_str = str(res)
try:
political_1 = re.findall("'(..)里", res_str)[0] + '里'
political_2 = re.findall("'(..)區", res_str)[0] + '區'
print(political_2,political_1)
return {'區': political_2,'里': political_1 }
except:
political_1 = None
political_2 = None
print(res)
return {'區': political_2,'里': political_1 }
def stat_code_compute(x):
scale_km = 0.5
lat = x[6]
lon = x[7]
lat_to_km = lat*110.574
lon_to_km = lon*(111.320*cos(lat))
state_code_x = int(lat_to_km//scale_km)
state_code_y = int(lon_to_km//scale_km)
state_code = str(state_code_x)+str(state_code_y)
return state_code
if __name__ == '__main__':
key = 'AIzaSyAgMGyDKheHh4IfHWbzaw6-hvdgoab8nPI'
gmaps = googlemaps.Client(key=key)
# REGULAR
center = (25.0329694, 121.5654177) # 25.057621 121.523086
print(googleUClientCreater(key).getInformation(center))
# print(res['區'])
# print(res['里'])
# ERROR
error_center = (24.99457376960226, 121.52962267265384)
print(googleUClientCreater(key).getInformation(error_center))
# ERROR II
error_center = (25.039792356250114, 121.61506875374727)
print(googleUClientCreater(key).getInformation(error_center))
error_center_shanghai = (31.24104300988,121.41872241448998)
print(googleUClientCreater(key).getInformation(error_center_shanghai))
# EERROR III
error_center = (24.999095628267042, 121.01489550495403)
print(googleUClientCreater(key).getInformation(error_center))
er_cr_ls = gmaps.reverse_geocode(error_center_shanghai,language='zh-TW')
pprint(er_cr_ls)
# ORIGIN RESPONSE
er_cr_ls = gmaps.reverse_geocode(error_center_shanghai,language='zh-TW')
pprint(er_cr_ls)
political_1 = er_cr_ls[1]['address_components'][1]['long_name']
political_2 = er_cr_ls[1]['address_components'][1]['long_name']
print(political_1,political_2)
def test(political_1,political_2):
if '區' not in political_1 and '理' not in political_2:
return False
else:
return True
test(political_1,political_2)
# REGEX PARSER
# er_cr_ls = gmaps.reverse_geocode(error_center,language='zh-TW')
# print(type(er_cr_ls))
# er_cr_ls_str = str(er_cr_ls)
# print(type(er_cr_ls_str))
# re.findall('''long_name': '(.+)里', 'short_name': ''',er_cr_ls_str)