ぴっかりん(@ra0kley)でX(旧Twitter)をやっています
オープンソースのJavaScript地図ライブラリ
「Mapbox GL JS」という地図ライブラリがライセンス変更を
契機にオープンソースフォークとして誕生
詳しく知りたい方は、以下のZenn本がオススメ!
pipやuv、pixiといったPythonのパッケージ管理ソフトを用いてインストール可能
→ 依存関係が複雑なため、仮想環境を新しく作るのがオススメ
→ gdalを一緒に使いたい場合は、pixiがオススメ
uvでインストールする場合
pixiでインストールする場合
以下のPythonスクリプトを実行すると、地図を表示するHTMLが保存される
(専修大学 生田キャンパス周辺を表示)
from maplibre import Map, MapOptions
# 最初に表示する場所やズームレベル、ピッチなどを設定
# 地図のスタイルとして、OpenStreetMapのライトテーマを指定
map_options = MapOptions(
center=(139.5544361, 35.6112653),
zoom=17,
hash=True,
style='https://tile.openstreetmap.jp/styles/osm-bright-ja/style.json',
pitch=60,
)
# 表示条件を地図に反映
m = Map(map_options)
# HTMLファイルとして保存
temp_file = './01_basic_usage.html'
with open(temp_file, "w") as f:
f.write(m.to_html())以下のようなHTMLが生成されます
(正直、中身は分かっていないです 350行くらいある。。。) 
ブラウザでHTMLを開くと・・・
MapLibre GL JSで作られたWeb地図が表示された!!
MVT形式のProject PLATEAUのデータを表示
Pythonスクリプト
from maplibre import Layer, LayerType, Map, MapOptions
# MapLibre GL JSで使用するスタイルの設定をdictで記載
style = {
"version": 8, # MapLibre Style Spec v8
"sources": {
"background-osm-raster": {
"type": "raster",
"tiles": [
"https://tile.openstreetmap.jp/styles/osm-bright-ja/{z}/{x}/{y}.png"
],
"tileSize": 256,
"attribution": "<a href='https://www.openstreetmap.org/copyright' target='_blank'>© OpenStreetMap contributors</a>",
},
"plateau-bldg": {
"type": "vector",
"tiles": [
"https://indigo-lab.github.io/plateau-lod2-mvt/{z}/{x}/{y}.pbf"
],
"minzoom": 10,
"maxzoom": 16,
"attribution": "<a href='https://github.com/indigo-lab/plateau-lod2-mvt'>plateau-lod2-mvt by indigo-lab</a> (<a href='https://www.mlit.go.jp/plateau/'>国土交通省 Project PLATEAU</a> のデータを加工して作成)",
},
},
"layers": [
{
"id": "background-osm-raster",
"type": "raster",
"source": "background-osm-raster",
},
{
"id": "bldg",
"type": "fill-extrusion",
"source": "plateau-bldg",
"source-layer": "bldg", # MVT内のレイヤ名
"paint": {
"fill-extrusion-height": ["*", ["get", "z"], 1], # 属性zを高さに
"fill-extrusion-color": "#797979",
"fill-extrusion-opacity": 0.7,
},
},
],
}
# 地図の初期表示のオプション(緯度経度やズームレベル、ピッチなど)
opts = MapOptions(
style=style,
center=(139.7024, 35.6598),
zoom=16,
pitch=60,
bearing=0,
)
# 地図オブジェクトの作成
m = Map(opts)
# HTMLに書き出す
save_path = "../html/demo_mvt_plateau.html"
with open(save_path, "w", encoding="utf-8") as f:
f.write(m.to_html())MVT形式のProject PLATEAUのデータを表示
作成されたWeb地図
PMTiles形式の登記所備付地図のデータを表示
Pythonスクリプト
from maplibre import Map, MapOptions
# MapLibre GL JSで使用するスタイルの設定をdictで記載
style = {
"version": 8,
"sources": {
"background-osm-raster": {
"type": "raster",
"tiles": [
"https://tile.openstreetmap.jp/styles/osm-bright-ja/{z}/{x}/{y}.png"
],
"tileSize": 256,
"attribution": "<a href='https://www.openstreetmap.org/copyright' target='_blank'>© OpenStreetMap contributors</a>",
},
"amx-a-pmtiles": {
"type": "vector",
"minzoom": 2,
"maxzoom": 16,
# pmtiles: スキーマを使用
"url": "pmtiles://https://habs.rad.naro.go.jp/spatial_data/amx/a.pmtiles",
"attribution": "<a href='https://www.moj.go.jp/MINJI/minji05_00494.html' target='_blank'>登記所備付地図データ(法務省)</a>",
},
},
"layers": [
{
"id": "background-osm-raster",
"type": "raster",
"source": "background-osm-raster",
},
# 登記所備付地図データ(筆ポリゴン)
{
"id": "amx-a-fude",
"type": "fill",
"source": "amx-a-pmtiles",
"source-layer": "fude",
"paint": {
"fill-color": "rgba(254, 217, 192, 1)",
"fill-outline-color": "rgba(255, 0, 0, 1)",
"fill-opacity": 0.4,
},
},
# 代表点レイヤ(ヒートマップ)
{
"id": "amx-a-daihyo",
"type": "heatmap",
"source": "amx-a-pmtiles",
"source-layer": "daihyo",
"paint": {
"heatmap-color": [
"interpolate", ["linear"], ["heatmap-density"],
0, "rgba(255, 255, 255, 0)",
0.5, "rgba(255, 255, 0, 0.5)",
1, "rgba(255, 0, 0, 0.5)"
],
"heatmap-radius": [
"interpolate", ["exponential", 10], ["zoom"],
2, 5,
14, 50
],
},
},
],
}
# 地図の初期表示のオプション(緯度経度やズームレベル、ピッチなど)
m = Map(MapOptions(
style=style,
center=(139.7024, 35.6598),
zoom=16,
pitch=0,
bearing=0,
))
# HTMLとして保存
with open("../html/demo_pmtiles_amx_project.html", "w", encoding="utf-8") as f:
f.write(m.to_html())PMTiles形式の登記所備付地図のデータを表示
作成されたWeb地図
leafmap という地理空間情報データの可視化や簡単な解析を容易にできるPythonライブラリにも
「Maplibre fot Python」が使われています
AIと連携した地図表示(AIエージェント)も作ることが可能!!
Zenn本のPython版を執筆したいと思います(宣言) 詳しくはそちらで!
Pythonでデータ処理を行っていた方も、この際にぜひフロントエンドに
触れてみてください!!
FOSS4G 2025 Japan