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 × Wikidataで日本の美術館を地図にプロットしてみた

Posted at

はじめに

「日本にはどんな美術館があるの?」
「地図で一覧できたら便利なのに…」

そんな時、役立つのがWikidata(ウィキデータ)とPythonの可視化ツールです。
この記事では、WikidataのSPARQLエンドポイントを使って、日本全国の美術館の緯度・経度を取得し、地図上にマッピングしてみます。

プログラミング言語はPython、地図描画にはfoliumライブラリを使用。
地理データの扱いにも慣れてくる実用的な入門にもなります!


使用する技術

ツール/ライブラリ 用途
SPARQLWrapper Wikidataにクエリを投げる
pandas データ整形(今回軽め)
folium 地図上へのプロット
Wikidata API 美術館の位置データを取得

Pythonコード

# 必要なライブラリのインストール(初回のみ)
!pip install SPARQLWrapper
from SPARQLWrapper import SPARQLWrapper, JSON
import pandas as pd
import folium

# SPARQLクエリ:日本の美術館の名前と緯度経度を取得
query = """
SELECT ?museumLabel ?coord WHERE {
  ?museum wdt:P31 wd:Q33506;         # インスタンスが「美術館」
          wdt:P17 wd:Q17;            # 国が日本
          wdt:P625 ?coord.           # 座標データ
  SERVICE wikibase:label { bd:serviceParam wikibase:language "ja". }
}
LIMIT 100
"""

# クエリの送信
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

# 結果の整形
museums = []
for result in results["results"]["bindings"]:
    name = result["museumLabel"]["value"]
    coord = result["coord"]["value"].replace("Point(", "").replace(")", "")
    lon, lat = map(float, coord.split(" "))
    museums.append((name, lat, lon))

# 地図を生成(中心は東京駅)
m = folium.Map(location=[35.681236, 139.767125], zoom_start=5)
for name, lat, lon in museums:
    folium.Marker(location=[lat, lon], popup=name).add_to(m)

# 地図を表示(Jupyter Notebook / Google Colabで表示されます)
m

実行結果

image.png

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?