LoginSignup
11
10

More than 3 years have passed since last update.

Python + staticmap で OpenStreetMap や地理院地図の画像を取得する

Last updated at Posted at 2020-11-01

概要

  • Python 用ライブラリの staticmap を使って OpenStreetMap や地理院地図の画像を取得する

今回の環境

  • macOS Catalina + Python 3.9.0 + staticmap 0.5.4

staticmap のインストール

staticmap パッケージをインストールする。

$ pip install staticmap

依存ライブラリとして Pillow と requests もインストールされる。

staticmap デフォルトの地図画像を取得する

ソースコード。

from staticmap import StaticMap

# 地図オブジェクトを生成
# 画像の横幅(ピクセル)と画像の縦幅(ピクセル)を指定
map = StaticMap(800, 600)

# 地図を描画した Pillow の PIL.Image オブジェクトを取得
# ズームレベルと経度・緯度を指定
image = map.render(zoom=17, center=[136.882090, 35.170560])

# 地図画像を保存
image.save('komoot.png')

実行結果。

komoot.png

OpenStreetMap の地図画像を取得する

ソースコード。

from staticmap import StaticMap

# 地図オブジェクトを生成
# 画像の横幅と縦幅と OpenStreetMap 標準タイルレイヤーのタイルURLを指定
map = StaticMap(800, 600, url_template='http://a.tile.openstreetmap.org/{z}/{x}/{y}.png')

# 地図を描画した Pillow の PIL.Image オブジェクトを取得
# ズームレベルと経度・緯度を指定
image = map.render(zoom=17, center=[136.882090, 35.170560])

# 地図画像を保存
image.save('osm.png')

実行結果。

osm.png

地理院地図の地図画像を取得する

ソースコード。

from staticmap import StaticMap

# 地図オブジェクトを生成
# 画像の横幅と縦幅と国土地理院の地理院タイルURLを指定
map = StaticMap(800, 600, url_template='https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png')

# 地図を描画した Pillow の PIL.Image オブジェクトを取得
# ズームレベルと経度・緯度のリストを指定
image = map.render(zoom=14, center=[136.882090, 35.170560])

# 地図画像を保存
image.save('chiriin.png')

実行結果。

chiriin.png

参考資料

11
10
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
11
10