6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Leafmapを使用してデータを地図上に可視化

Posted at

はじめに

地図アプリを作成する時にJavaScriptのLeafletを使用することが多いですが、
Pythonでもライブラリがあります。
PythonだとFoliumが有名だと思いますが、
今回、LeafmapというPythonのライブラリを使って地図上にデータを可視化するコードを書いていきます

Leafmapの特徴

  • インタラクティブな地図の作成が容易
  • 豊富なベースマップの選択
  • JupyterやGoogle Colaboratoryとの互換性

・Leafmap
https://leafmap.org/

Leafmapで作成したアプリのデモです
68747470733a2f2f7765746c616e64732e696f2f66696c652f696d616765732f6c6561666d61705f64656d6f2e676966.gif

Leafmap vs Folium

FoliumもPythonでの地図可視化に人気のあるライブラリですが、Leafmapはより多くのベースマップの選択や、簡単なインターフェースを提供しています。
また、特定のデータセットに対する可視化機能もLeafmapの強みとなっています。

環境構築

pip install leafmap

地図を作成

make_map.py
import leafmap

m = leafmap.Map()
m.to_html('./map.html')

"python make_map.py"を実行するとローカルに地図が作成されると思います。
開いてみます
screencapture-127-0-0-1-5500-map-html-2023-09-22-14_55_04.png

パラメータを何も設定していないため地図の中心がイギリスになっています。

center属性で日本の緯度・経度を設定すると、htmlを開いた時に日本が中心に表示されます。

m = leafmap.Map(center=(30, 135))

ベースマップを変更

m.add_basemap("OpenTopoMap")

screencapture-127-0-0-1-5500-map-html-2023-09-22-15_01_52.png

XYZタイルレイヤーを追加

m.add_tile_layer(
    url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
    name="Google Satellite",
    attribution="Google",
)

screencapture-127-0-0-1-5500-map-html-2023-09-22-15_06_10.png

ヒートマップ

人口のヒートマップを作成します

in_csv = "https://raw.githubusercontent.com/opengeos/leafmap/master/examples/data/world_cities.csv"
m.add_heatmap(
    in_csv,
    latitude="latitude",
    longitude='longitude',
    value="pop_max",
    name="Heat map",
    radius=20,
)
colors = ['blue', 'lime', 'red']
vmin = 0
vmax = 10000
m.add_colorbar(colors=colors, vmin=vmin, vmax=vmax)
m.add_title("World Population Heat Map", font_size="20px", align="center")

screencapture-127-0-0-1-5500-map-html-2023-09-22-15_22_17.png

おわりに

geojsonファイルを読み込んだデータを地図に可視化することも可能なので、
Foliumと同様にgeopandasでread_fileしたDataFrameをLeafmapの関数のパラメータに設定すると地図上にラスター/ベクターデータとして出力可能です。
GISに興味のあるエンジニアの方でも非エンジニアの方でもやってみてください!

備考

・Leaflet
https://leafletjs.com/

・Folium
https://python-visualization.github.io/folium/latest/

一部の画像は下記より引用させていただいています
https://leafmap.org/notebooks/00_key_features/#set-control-visibility

データはLeafmapのGitHubにあるデータを使用することにします
https://github.com/opengeos/leafmap/tree/master/examples/data

6
7
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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?