-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsunami_api.py
More file actions
89 lines (79 loc) · 4.58 KB
/
Copy pathtsunami_api.py
File metadata and controls
89 lines (79 loc) · 4.58 KB
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
import math
import re
import ssl
import warnings
from urllib3.exceptions import InsecureRequestWarning
from urllib3.util import create_urllib3_context
from urllib3 import PoolManager
from utils import persistent_cache, wgs84_to_tokyo
import pandas as pd
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
@persistent_cache("cache/tsunami_cache.csv")
def get_tsunami_data(lat_wgs: float, lon_wgs: float, repeat=True) -> dict[str, float]:
"""
Retrieve tsunami data from the Kochi Prefecture Tsunami API (https://bousaimap.pref.kochi.lg.jp/kochi/req/getxmlinfo.asp) for given latitude and longitude.
:param lat_wgs: latitude as WGS84 (API uses older Tokyo Datum internally, it will be converted automatically)
:param lon_wgs: longitude as WGS84 (API uses older Tokyo Datum internally, it will be converted automatically)
:param repeat: Retry by slightly adjusting coordinates, in case of no data or incomplete data, sometimes a request
can hit a projection voxel and no data is returned for this border.
:return: {"time_min": float, "time_max": float, "tide_min": float, "tide_max": float, "success": bool} -- Success indicates if data was found for the location. Cached indicates if the result was retrieved from cache. The cache value can be used to limit/not limit API calls if the value was cached.
"""
lat, lon = wgs84_to_tokyo(lat_wgs, lon_wgs)
url = f"https://bousaimap.pref.kochi.lg.jp/kochi/req/getxmlinfo.asp?mtl=411&mcx={lon}&mcy={lat}&mps={20000}"
ctx = create_urllib3_context(ciphers=":HIGH:!DH:!aNULL")
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
http = PoolManager(ssl_context=ctx)
response = http.request("GET", url, headers={}, body={})
response_str = response.data.decode('shift_jis').strip().replace("\n", "").replace("\r", "")
# --- No Data Response ---
if int(response_str.split("<response><result><count>")[1][0]) == 0:
if repeat:
return get_tsunami_data(lat - 0.0001, lon + 0.001, repeat=False)
else:
return {"time_min": -1.0, "time_max": -1.0, "tide_min": -1.0, "tide_max": -1.0, "success": False}
# --- Half Data Response ---
tide_reg = re.findall(r'浸水深([\d.]+)~([\d.]+)m', response_str)
if len(tide_reg) != 1:
if repeat:
return get_tsunami_data(lat - 0.0001, lon + 0.001, repeat=False)
else:
return {"time_min": -1.0, "time_max": -1.0, "tide_min": -1.0, "tide_max": -1.0, "success": False}
# --- Full Data Response ---
# Tide: 到達時間40~60分 or 到達時間60分以上
# 到達時間40~60分 aka. Regex: 到達時間([\d~]+)分 (both casees)
# Time: 浸水深1.0~2.0m (one case)
# 浸水深1.0~2.0m aka 浸水深([\d.]+)~([\d.]+)m
time = re.findall(r'到達時間([\d~]+)分', response_str)[0].split("~")
if len(time) == 2: # Range time case (40-60min)
time_min = float(time[0])
time_max = float(time[1])
else: # Single time case (60min+)
time_min = float(time[0])
time_max = math.inf
tide_min = float(tide_reg[0][0])
tide_max = float(tide_reg[0][1])
return {"time_min": time_min, "time_max": time_max, "tide_min": tide_min, "tide_max": tide_max, "success": True}
def clean_tsunami_cache():
"""
Cleans the tsunami cache by removing failed calls.
"""
df = pd.read_csv("cache/tsunami_cache.csv")
failed_calls = df[df["result"].str.contains('"success": false')]
df = df.drop(failed_calls.index)
df.to_csv("cache/tsunami_cache.csv", index=False)
def clean_tsunami_cache_zero_val():
"""
Cleans of zero_value tsunami cache entries, because IDK where they come from...
"""
df = pd.read_csv("cache/tsunami_cache.csv")
zero_val_calls = df[df["result"].str.contains('"time_min": 0.0')]
df = df.drop(zero_val_calls.index)
df.to_csv("cache/tsunami_cache.csv", index=False)
if __name__ == "__main__":
lat_lon_wgs = 33.555464, 133.542192
result = get_tsunami_data(lat_lon_wgs[0], lat_lon_wgs[1]) # Nagasaki coordinates
print(result["time_min"], result["time_max"], result["tide_min"], result["tide_max"], result["success"])
print(f"WGS84 Coordinates: {lat_lon_wgs[0]}, {lat_lon_wgs[1]}, Tokyo Datum Coordinates: {wgs84_to_tokyo(lat_lon_wgs[0], lat_lon_wgs[1])}")
# Link: https://bousaimap.pref.kochi.lg.jp/kochi/req/getxmlinfo.asp?mtl=411&mcx=33.552107&mcy=133.544781&mps=20000 -> Return Nothing, but API returns 0 :(
exit(1)