0
1

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.

MacBook環境でOpenStreetMapを動かすだけの備忘録

Last updated at Posted at 2023-10-22

環境

Mac(OS Monterey ver.12.5.1), Apple M1, memory32GB

OpenStreetMapとは

https://www.openstreetmap.org/
https://openstreetmap.jp/

OpenStreetMap(OSM)は、誰でも自由に地図を使えるよう、みんなでオープンデータの地理情報を作るプロジェクトです。
プロジェクトには、誰でも自由に参加して、誰でも自由に地図を編集して、誰でも自由に地図を利用することが出来ます

準備

こちらの記事を参考にローカルPCで動かさせて頂くだけの記事です
anacondaではなく、アプリケーション-->ユーティリティ-->ターミナルから開きます

https://qiita.com/nujust/items/63387a51d3f2379d3433

事前にインストール

pip3 install cartopy
pip3 install contextily

実行

以下記事を参考に実行する
以下記事では東京駅(の緯度経度)に、赤い丸をプロットしている。

https://qiita.com/nujust/items/63387a51d3f2379d3433

(以下を参考に日本語をMeiryoで表示したかったが今回は実行優先した)

https://www.kimoton.com/entry/20201115/1605426414

python3 test.py
test.py
import cartopy.crs as ccrs
import contextily as cx
import matplotlib.pyplot as plt
import pyproj
from matplotlib.offsetbox import AnchoredText


def main():
   # 日本語フォントの設定
#    plt.rcParams["font.family"] = "ボールド・イタリック" #"Meiryo"

   # 対象地域の適当な座標系を設定
   crs = ccrs.epsg(6677)  # 平面直角座標系 9系(JGD2011)

   # 座標系に対応したオブジェクト
   fig = plt.figure(figsize=(8, 8))  # 実際の出力サイズは描画領域で決まるため、ここでは最大サイズとして指定。
   ax = fig.add_subplot(1, 1, 1, projection=crs)  # projectionへのcrsの指定により地理空間に投影

   # 描画領域の設定(東京駅周辺の座標を確認して入力)
   ax.set_xlim(-8000.0, -4000.0)
   ax.set_ylim(-37000.0, -34000.0)

   # ベースマップ(OpenStreetMap)の追加
   cx.add_basemap(ax, crs=crs, zoom=16, source="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")

   # 図形(ポイント)の描画
   lat, lon = 35.6810912, 139.7671861  # 東京駅の緯度経度
   transformer = pyproj.Transformer.from_crs("EPSG:4326", crs)
   y, x = transformer.transform(lat, lon)
   plt.scatter(x, y, s=200, c="red", alpha=0.5, label="Tokyo Station")

   # 凡例の描画
   ax.legend(loc="upper right", prop={"size": 20})

   # OpenStreetMapのクレジットを右下隅に表示
   text_box = AnchoredText("© OpenStreetMap contributors", loc="lower right", borderpad=0, frameon=True)
   plt.setp(text_box.patch, facecolor="white", alpha=0.6, linewidth=0)
   ax.add_artist(text_box)

   # プロットエリアの枠を非表示
   ax.set_axis_off()

   # プロットエリア以外の部分をなくし、地図部分のみをファイルに出力
   fig.tight_layout(pad=0)
   fig.savefig("output.png", bbox_inches="tight", pad_inches=0)
if __name__ == "__main__":
   main()

実行結果(output.png)
output.png

参考

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?