-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
475 lines (403 loc) · 14.6 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 13 12:08:32 2024
@author: Alex
"""
import loguru
from datetime import datetime
import time
import requests
import csv
from pyproj import Proj, Transformer
from bs4 import BeautifulSoup
import os
import threading
import json
# 失敗重試次數
MAX_RETRIES = 2
# API網站
API = 'https://urban.kinmen.gov.tw/kmgis/map/AcHandler.ashx'
# LOG檔命名規則
LOG_PATH = f'Logs/{datetime.today().strftime("%Y%m%d")}.log'
# 每次處理兩個鄉鎮
BATCH_SIZE = 2
def Twd97ToWGS84(ox, oy):
"""
定義TWD97轉換為WGS84座標系統的函數
Parameters
----------
ox : float
TWD97座標的X值.
oy : float
TWD97座標的Y值.
Returns
-------
pointDest : tuple
包含WGS84座標的元組.
"""
p1 = Proj('EPSG:3825')
p2 = Proj('EPSG:4326')
transformer = Transformer.from_proj(p1, p2)
pointDest = transformer.transform(ox, oy)
return pointDest
def getTowns():
"""
取得鄉鎮名稱的函數
Returns
-------
towns : list
包含鄉鎮名稱的列表.
"""
loguru.logger.info('取得鄉鎮')
response = requests.get('https://urban.kinmen.gov.tw/kmgis/map/publicMap')
# 使用Beautiful Soup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 找到指定ID的select元素
select_element = soup.find('select', id='selZoom3Town')
options = select_element.find_all('option')
towns = [option.get('value') for option in options if option.get('value')]
loguru.logger.debug(towns)
return towns
def searchRoads(txt='', retry_count=0):
loguru.logger.info(f'搜尋路 查詢條件:{txt}')
response = requests.post(API, data={'CMD': 'GETROADLIST', 'VAL': txt})
return check_status(response, 'ROAD', retry_count, lambda count: searchRoads(txt, count))
def getMarkMainClassList(retry_count=0):
loguru.logger.info('取得主分類')
response = requests.post(API, data={'CMD': 'GETMARKMAINCLASSLIST'})
return check_status(response, ['Type', 'TypeId'], retry_count, lambda count: getMarkMainClassList(count))
def getMarkSubClassList(main, retry_count=0):
loguru.logger.info(f'取得次分類 主分類:{main}')
response = requests.post(API, data={'CMD': 'GETMARKSUBCLASSLIST', 'VAL': main})
return check_status(response, ['SubType', 'SubTypeId'], retry_count, lambda count: getMarkSubClassList(main, count))
def getMarkList(main, sub, retry_count=0):
loguru.logger.info(f'取得地標 主分類:{main} 次分類:{sub}')
response = requests.post(API, data={'CMD': 'GETMARKLIST', 'VAL': main, 'CODE': sub})
try:
# 使用.json()方法解析JSON數據
json_data = response.json()
except Exception as e:
loguru.logger.debug(f'Error decoding JSON: {e}.')
loguru.logger.debug(f'Response text: {response.text}')
if retry_count < MAX_RETRIES:
loguru.logger.info('10秒後再試一次')
time.sleep(10)
return getMarkList(main, sub, retry_count + 1)
else:
loguru.logger.info('已達最大重試次數,放棄嘗試')
loguru.logger.error(f'mainType: {main} subType: {sub}')
return None
return json_data if json_data else None
def getSubLmCode(type, retry_count=0):
# type 1~15
loguru.logger.info(f'取得小類 大類:{type}')
response = requests.post(API, data={'CMD': 'GETSUBLMCODE', 'VAL': type})
return check_status(response, 'SubType', retry_count, lambda count: getSubLmCode(type, count))
def getLandMarkList(type, subtype, retry_count=0):
loguru.logger.info(f'取得地標 大類:{type} 小類:{subtype}')
response = requests.post(
API, data={'CMD': 'GETLANDMARKLIST', 'VAL': type, 'CODE': subtype})
try:
# 使用.json()方法解析JSON數據
json_data = response.json()
except Exception as e:
loguru.logger.debug(f'Error decoding JSON: {e}.')
loguru.logger.debug(f'Response text: {response.text}')
if retry_count < MAX_RETRIES:
loguru.logger.info('10秒後再試一次')
time.sleep(10)
return getLandMarkList(type, subtype, retry_count + 1)
else:
loguru.logger.info('已達最大重試次數,放棄嘗試')
loguru.logger.error(f'type: {type} subtype: {subtype}')
return None
return json_data if json_data else None
def getRoads(town, retry_count=0):
"""
取得道路名稱的函數
Parameters
----------
town : str
鄉鎮名稱.
retry_count : int, optional
失敗重試次數. The default is 0.
Returns
-------
result : list
包含道路名稱的列表.
"""
loguru.logger.info(f'取得路 鄉鎮:{town}')
# 定義POST請求的URL和payload(要發送的數據)
# 發送POST請求
response = session.post(API, data={'CMD': 'GETADDRROAD', 'VAL': town})
return check_status(response, 'ROAD', retry_count, lambda count: getRoads(town, count))
def getLanes(town, road, retry_count=0):
"""
取得巷名稱的函數
Parameters
----------
town : str
鄉鎮名稱.
road : str
道路名稱.
retry_count : int, optional
失敗重試次數. The default is 0.
Returns
-------
result : list
包含巷名稱的列表.
"""
loguru.logger.info(f'取得巷 鄉鎮:{town} 門牌:{road}')
# 定義POST請求的URL和payload(要發送的數據)
# 發送POST請求
response = session.post(
API, data={'CMD': 'GETADDRLANE', 'VAL': town, 'CODE': road})
return check_status(response, 'LANE', retry_count, lambda count: getLanes(town, road, count))
def getAlleys(town, road, lane, retry_count=0):
"""
取得弄名稱的函數
Parameters
----------
town : str
鄉鎮名稱.
road : str
道路名稱.
lane : str
巷名稱.
retry_count : int, optional
失敗重試次數. The default is 0.
Returns
-------
result : list
包含弄名稱的列表.
"""
loguru.logger.info(f'取得弄 鄉鎮:{town} 門牌:{road} 巷:{lane}')
# 定義POST請求的URL和payload(要發送的數據)
# 發送POST請求
response = session.post(
API, data={'CMD': 'GETADDRALLEY', 'VAL': town, 'CODE': road, 'OTHER': lane})
return check_status(response, 'ALLEY', retry_count, lambda count: getAlleys(town, road, lane, count))
def getDoors(town, road, lane, alley, retry_count=0):
"""
取得號碼的函數
Parameters
----------
town : str
鄉鎮名稱.
road : str
道路名稱.
lane : str
巷名稱.
alley : str
弄名稱.
retry_count : int, optional
失敗重試次數. The default is 0.
Returns
-------
result : list
包含號碼的列表.
"""
loguru.logger.info(f'取得號 鄉鎮:{town} 門牌:{road} 巷:{lane} 弄:{alley}')
# 定義POST請求的URL和payload(要發送的數據)
# 發送POST請求
response = session.post(API, data={
'CMD': 'GETADDRDOOR', 'VAL': town, 'CODE': road, 'OTHER': lane, 'MORE': alley})
return check_status(response, 'NUMBER', retry_count, lambda count: getDoors(town, road, lane, alley, count))
def getXY(town, road, lane, alley, door, retry_count=0):
"""
取得座標的函數
Parameters
----------
town : str
鄉鎮名稱.
road : str
道路名稱.
lane : str
巷名稱.
alley : str
弄名稱.
door : str
號碼.
retry_count : int, optional
重試次數. The default is 0.
Returns
-------
result : dict or None
包含座標信息的字典,如果失敗則返回None.
"""
loguru.logger.info(f'取得座標 鄉鎮:{town} 門牌:{road} 巷:{lane} 弄:{alley} 號:{door}')
# 定義POST請求的URL和payload(要發送的數據)
# 發送POST請求
response = session.post(API, data={
'CMD': 'GETXY', 'VAL': town, 'CODE': road, 'OTHER': lane, 'MORE': alley, 'THEN': door})
try:
# 使用.json()方法解析JSON數據
json_data = response.json()
except Exception as e:
loguru.logger.debug(f'Error decoding JSON: {e}.')
loguru.logger.debug(f'Response text: {response.text}')
if retry_count < MAX_RETRIES:
loguru.logger.info('10秒後再試一次')
time.sleep(10)
return getXY(town, road, lane, alley, door, retry_count + 1)
else:
loguru.logger.info('已達最大重試次數,放棄嘗試')
loguru.logger.error(
f'town: {town} road: {road} lane: {lane} alley: {alley} door: {door}')
return None
return json_data[0] if json_data else None
def check_status(response, key, retry_count, callback):
"""
檢查請求狀態的函數
Parameters
----------
response : requests.Response
HTTP響應對象.
key : str or list
JSON數據中要提取的鍵.
retry_count : int
失敗重試次數.
callback : function
失敗時要調用的回調函數.
Returns
-------
result : list
根據鍵提取的值列表或空列表.
"""
try:
# 使用.json()方法解析JSON數據
json_data = response.json()
if type(key) == list:
obj = {}
for d in json_data:
obj.update({d[key[1]]: d[key[0]]})
loguru.logger.debug(obj)
return obj
else:
arr = [obj[key] for obj in json_data]
loguru.logger.debug(arr)
return arr
except Exception as e:
loguru.logger.debug(f'Error decoding JSON: {e}.')
loguru.logger.debug(f'Response text: {response.text}')
if retry_count < MAX_RETRIES:
loguru.logger.info('10秒後再試一次')
time.sleep(10)
# 根據回調函數的不同來調用相應的函數
return callback(retry_count + 1)
else:
loguru.logger.info('已達最大重試次數,放棄嘗試')
return {} if type(key) == list else []
def read_data(file_name, start, end):
"""
從文件中讀取所有處理的位置
Parameters
----------
file_name : str
檔案名稱.
start : int
欄位開始位置.
end : int
欄位結束位置.
Returns
-------
csv_file : set
包含已處理位置的集合.
"""
csv_file = set()
if os.path.exists(file_name):
with open(file_name, 'r', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
csv_file.add(','.join(row[start:end]))
return csv_file
def svae_data(file_name, field_names, obj):
"""
將當前處理的位置保存到文件中
Parameters
----------
file_name : str
檔案名稱.
field_names : list
欄位名稱.
obj : dict
資料集.
Returns
-------
None.
"""
with open(file_name, 'a', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=field_names)
# 如果文件不存在,寫入表頭
if file.tell() == 0:
writer.writeheader()
writer.writerow(obj)
loguru.logger.debug(obj)
def csv_to_json(file_name, output_name):
loguru.logger.info(f'資料集轉成JSON input:{file_name} ouput:{output_name}')
data = []
with open(file_name, 'r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
data.append(row)
with open(rf'data/{output_name}', 'w', encoding='utf-8') as json_file:
json.dump({"data": data}, json_file, ensure_ascii=False, indent=4)
def process_towns(town):
roads = getRoads(town)
for road in roads:
lanes = getLanes(town, road)
for lane in lanes:
alleys = getAlleys(town, road, lane)
for alley in alleys:
doors = getDoors(town, road, lane, alley)
for door in doors:
# 如果已處理過此位置,則跳過
position = ','.join([town, road, lane, alley, door])
if position in processed_positions:
loguru.logger.info(f'{door} 已下載跳過')
continue
obj = getXY(town, road, lane, alley, door)
if obj:
xy = Twd97ToWGS84(obj['X'], obj['Y'])
# 要寫入的欄位名稱
field_names = ['ADDR', 'LATITUDE', 'LONGITUDE', 'TOWN', 'ROAD', 'LANE', 'ALLEY', 'DOOR']
my_dict = {'ADDR': obj['FULL_ADDR'], 'LATITUDE': xy[0], 'LONGITUDE': xy[1],
'TOWN': town, 'ROAD': road, 'LANE': lane, 'ALLEY': alley, 'DOOR': door}
svae_data('address.csv', field_names, my_dict)
if __name__ == '__main__':
loguru.logger.add(LOG_PATH, rotation='1 day', level='ERROR')
loguru.logger.info('Start get coordinates')
session = requests.Session()
# 讀取所有已處理的位置
processed_positions = read_data('address.csv', 3, 8)
towns = getTowns()
for i in range(0, len(towns), BATCH_SIZE):
batch_towns = towns[i:i + BATCH_SIZE]
threads = []
for town in batch_towns:
thread = threading.Thread(target=process_towns, args=(town,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
csv_to_json('address.csv', 'address.json')
processed_positions = read_data('mark.csv', 3, 5)
mains = getMarkMainClassList()
for main in mains:
subs = getMarkSubClassList(main)
for sub in subs.keys():
# 如果已處理過此位置,則跳過
position = ','.join([mains[main], subs[sub]])
if position in processed_positions:
loguru.logger.info(f'main: {mains[main]} sub:{subs[sub]} 已下載跳過')
continue
mark_list = getMarkList(main, sub)
if mark_list:
for mark in mark_list:
xy = Twd97ToWGS84(mark['X'], mark['Y'])
field_names = ['NAME', 'LATITUDE', 'LONGITUDE', 'MAIN', 'SUB']
my_dict = {'NAME': mark['LMarkName'], 'LATITUDE': xy[0], 'LONGITUDE': xy[1],
'MAIN': mains[main], 'SUB': subs[sub]}
svae_data('mark.csv', field_names, my_dict)
csv_to_json('mark.csv', 'mark.json')
loguru.logger.info('Finish')