OSMxを使い最短経路の算出
東京スカイツリーから
江戸東京博物館への最短経路を算出する
まずはスカイツリーと江戸東京博物館の座標をセットする
skytree = (35.7100,139.8180) museum = (35.6963,139.7967)
次に一番近い node つまり交差点を取得
始点はスカイツリーなので
start = ox.get_nearest_node(G,skytree)
としたがエラー
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[15], line 1 ----> 1 start = ox.get_nearest_node(G,skytree) AttributeError: module 'osmnx' has no attribute 'get_nearest_node'
これもバージョンの違いが原因
OSMnx 2.x では廃止され、ox.distance サブモジュールに移動
引数は (X=経度, Y=緯度) の順番で渡す必要があります
普通の Point(lon, lat) と同じ並び
start = ox.distance.nearest_nodes(G, X=skytree[1], Y=skytree[0])
とすればOK
同様に終点も
end = ox.distance.nearest_nodes(G,museum[1],museum[0])
次に最短経路
shortest_path = ox.shortest_path(G,start,end)
Foliumで表示できないので調べた
plot_route_folium
https://github.com/gboeing/osmnx/issues/1252
によれば
release (currently 1.9.4), then run your code. The latest v1 release will remain compatible with the v1 API, but any deprecated functionality you’re using will issue a FutureWarning with simple guidance to make it v2 compatible.
Yes, the folium module was deprecated with a warning for removal back in 2023, and finally removed in v2.0.0. The good news: interactive web mapping with folium remains easy to do with OSMnx in ~2 lines of code! See this example in the Examples Gallery for a demonstration.
となっていて
foliumモジュールは2023年に削除の警告とともに非推奨となり、最終的にv2.0.0で削除されました。
とのこと
https://github.com/gboeing/osmnx-examples/blob/main/notebooks/11-interactive-web-mapping.ipynb
のサンプルが参考になりそう
OSMnx 2.0.6 には Folium 専用関数は無い → 自分で PolyLine を追加すればOK
再度GPTでコード生成
route = nx.shortest_path(G, orig, dest, weight="length")
というように
weight="length")
があるので調べた
✅ weight の意味
networkx.shortest_path の weight 引数は、**「どのエッジ属性を最短経路計算に使うか」**を指定するものです。
OSMnx で作成した G(道路グラフ)には、各エッジ(道路の区間)に様々な属性があります:
* “length” : 区間の長さ(メートル単位) ← デフォルトでよく使う
* “travel_time” : 区間の推定走行時間(秒単位)※ ox.add_edge_speeds / ox.add_edge_travel_times を実行した場合のみ
* その他、highway(道路種別)や lanes(車線数)など
✅ 例
1. 最短距離ルート(距離を最小化)
route = nx.shortest_path(G, orig, dest, weight="length")
2. 最短時間ルート(所要時間を最小化)
まず速度と所要時間をエッジに付与してから:
import osmnx as ox G = ox.add_edge_speeds(G) # 各道路に推定速度 (km/h) を追加 G = ox.add_edge_travel_times(G) # 各道路に travel_time (秒) を追加 route = nx.shortest_path(G, orig, dest, weight="travel_time")
✅ まとめ
* weight=”length” → 「最短距離ルート」
* weight=”travel_time” → 「最短時間ルート」
* 他のエッジ属性を指定すれば、それに基づいた経路探索ができる