0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonとfoliumで実践するオープンデータ可視化:気象庁震源リストの地図重畳

Posted at

はじめに

前回の記事「Pythonで地図可視化 ― foliumの概要・インストール・基本的な使い方」では、foliumを用いた地図作成の基礎と、マーカーやヒートマップの活用方法を紹介しました。今回はその応用として、気象庁の地震震源リストをもとに、地震分布を地図上で直感的に可視化する実践的な手法を解説します。

震源リストの準備

データの入手先

気象庁 震源リスト
気象庁の公式サイトから日々公開されている「震源リスト」を利用します。

CSV形式への加工

震源リストはテキスト形式(HTML内)で提供されているため、CSV化に加工します。
下記のPythonスクリプトで、元データから必要な項目を抽出し、CSVファイルに変換します。

準備

「震源リスト」をテキストファイルにしておきます(data.txt

d33486871ef7ed901b00-2.png

CSVデータに変換

import re
import csv

# 正規表現パターンで各行を解析
row_pattern = re.compile(
    r"(\d{4})\s+(\d{1,2})\s+(\d{1,2})\s+(\d{2}):(\d{2})\s+([\d.]+)\s+"
    r"(\d{1,3})°\s*(\d{1,2}\.\d+)'?N\s+(\d{1,3})°\s*(\d{1,2}\.\d+)'?E\s+(\d+)\s+([\d\-.]+)\s+(.+)"
)

# 緯度・経度を10進法(度)に変換
def dms_to_decimal(degree, minute):
    return float(degree) + float(minute) / 60

# ファイル名
input_filename = 'data.txt'
output_filename = 'eqev_list.csv'

# ヘッダ
header = [
    "Year", "Month", "Day", "Hour", "Minute", "Second",
    "Latitude", "Longitude", "Depth_km", "Magnitude", "Location"
]

with open(input_filename, encoding='utf-8') as infile, \
     open(output_filename, "w", newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(header)
    for line in infile:
        match = row_pattern.match(line.strip())
        if match:
            (
                year, month, day, hour, minute, second,
                lat_deg, lat_min, lon_deg, lon_min, depth, mag, loc
            ) = match.groups()
            # 緯度・経度を10進法に
            lat = dms_to_decimal(lat_deg, lat_min)
            lon = dms_to_decimal(lon_deg, lon_min)
            mag = "" if mag == "-" else mag
            row = [
                year, month, day, hour, minute, second,
                f"{lat:.5f}", f"{lon:.5f}", depth, mag, loc.strip()
            ]
            writer.writerow(row)

変換したCSVのサンプル(抜粋)
d33486871ef7ed901b00-3.png

併せて、緯度経度の形式も10進数に変換しています

地震データの地図可視化

プログラム

import pandas as pd
import folium

# 円のサイズ(マグニチュードに応じた段階設定)
def get_radius(mag):
    if pd.isna(mag):
        return 2  # 欠損値は最小
    thresholds = [1, 3, 5, 7]
    sizes = [4, 7, 10, 14, 18]
    for i, threshold in enumerate(thresholds):
        if mag <= threshold:
            return sizes[i]
    return sizes[-1]

# 円の色(深さに応じた段階設定)
def get_color(depth):
    if pd.isna(depth):
        return '#cccccc'  # 欠損値はグレー
    depth = float(depth)
    thresholds = [30, 100, 300]
    colors = ["#D4502E", "#D49E29", "#ECDA37", "#2F6296"]
    for i, threshold in enumerate(thresholds):
        if depth < threshold:
            return colors[i]
    return colors[-1]

# CSVファイルの読み込み
df = pd.read_csv('eqev_list.csv')

# 地図の中心を日本付近に設定
m = folium.Map(location=[35.0, 137.0], zoom_start=5)

# 各震源地を地図にプロット
for _, row in df.iterrows():
    lat = row['Latitude']
    lon = row['Longitude']
    mag = pd.to_numeric(row['Magnitude'], errors='coerce')
    depth = pd.to_numeric(row['Depth_km'], errors='coerce')
    folium.CircleMarker(
        location=[lat, lon],
        radius=get_radius(mag),
        fill=True,
        fill_color=get_color(depth),
        fill_opacity=0.7,
        color=None,
        weight=0
    ).add_to(m)

# 地図をHTMLファイルとして出力
m.save('earthquake_map.html')

結果イメージ

d33486871ef7ed901b00-1.png

まとめ

本記事では、気象庁が公開する震源リスト(オープンデータ)をPythonでCSV化し、foliumを活用して地図上に重畳表示する一連のフローを解説しました。
前回の記事で紹介したfoliumの基本操作を土台に、実際のオープンデータを活用した地図可視化までを具体的に示しています。

オープンデータの活用
公的機関が公開している信頼性の高いデータを、身近な業務や分析にすぐ取り入れられます。

柔軟なカスタマイズが可能
コードはシンプルで拡張しやすいので、他の地理データや可視化ニーズにも応用が効きます。

地図で“見える化”できる
データの分布や傾向を地図上で直感的に把握することができます。

本記事の内容が、オープンデータ活用や地理情報の可視化を検討されている方の参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?