震度データベース検索
import datetime import re import pandas as pd import requests import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns import folium def dms_to_deg(dms): data = re.split("[°.′]", dms) # 東西南北記号を取り出し data_dir = data.pop() # リスト補完 data.extend([0, 0, 0]) deg = int(data[0]) + int(data[1]) / 60.0 + int(data[2]) / 3600.0 return deg # 解像度 mpl.rcParams["figure.dpi"] = 200 sns.set() import japanize_matplotlib # スクレイピング # 開始日 # 全期間 dt_start = datetime.date(1922, 1, 1) # dt_start = datetime.date(2017, 1, 1) dt_start_str = dt_start.strftime("%Y/%m/%d") print(dt_start_str) # 終了日 # 前々日 dt_end = datetime.date.today() - datetime.timedelta(days=2) dt_end_str = dt_end.strftime("%Y/%m/%d") print(dt_end_str) url = "https://www.data.jma.go.jp/svd/eqdb/data/shindo/index.php" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko", "Referer": "https://www.data.jma.go.jp/svd/eqdb/data/shindo/index.php", } payload = { "ymdF": dt_start_str, "hmsF": "00:00", "ymdT": dt_end_str, "hmsT": "23:59", "MaxI": "I1", "MinM": "F00", "MaxM": "T95", "DepF": 0, "DepT": 999, "EpiN[]": "瀬戸内海中部", "Pref[]": 62, "City[]": 62000, "Obs[]": 9999999, "Int": 1, "Search": "", "Sort": "S0", "Comp": "C0", "DetailFlg": 1, } r = requests.post(url, headers=headers, data=payload) r.raise_for_status() # 分析 dfs = pd.read_html(r.content, index_col=0) df = dfs[5] print(df) # 日付に変換 df["datetime"] = pd.to_datetime(df["地震の発生日時"]) # DMSからDEGに変換 df["lat"] = df["緯度"].apply(dms_to_deg) df["lon"] = df["経度"].apply(dms_to_deg) # kmを除去後、数字に変換 df["深さ"] = df["深さ"].str.rstrip("km").astype(int) # Mを除去後、数字に変換 df["M"] = df["M"].str.lstrip("M").astype(float) # ジュールに変換 df["J"] = 10 ** (df["M"] * 1.5 + 4.8) print(df) # グラフ表示 # plt.figure(figsize=(9, 5)) plt.plot_date(df["datetime"], df["J"]) plt.xlabel("date") plt.ylabel("J") plt.xticks(rotation=30) plt.show() # 地図表示 imabari_map = folium.Map(location=[34.035278, 133.052222], zoom_start=16) for i, r in df.iterrows(): folium.CircleMarker( location=[r["lat"], r["lon"]], popup=r["地震の発生日時"], radius=r["J"] / 2000000000 ).add_to(imabari_map) imabari_map