대학원생 리암의 블로그

실제 도로 시각화 - OSMnx 본문

코딩

실제 도로 시각화 - OSMnx

liam0222 2024. 10. 2. 22:02

OSMnx는 Open Source Map networkx의 약자로 opensource map의 네트워크 정보를 networkx라는 파이썬 라이브러리를 통해 활용할 수 있게 해주는 library이다. 예제로 Piedmont의 데이터를 불러와서 두 점을 찍고 차로 이동할 수 있는 최단 거리를 계산하고 시각화 해보았다. 

import networkx as nx
import osmnx as ox

# you can make query an unambiguous dict to help the geocoder find it
place = {"city": "Piedmont", "state": "California", "country": "USA"}
G = ox.graph_from_place(place, network_type="drive", truncate_by_edge=True)

# impute missing edge speeds and calculate edge travel times with the speed module
G = ox.routing.add_edge_speeds(G)
G = ox.routing.add_edge_travel_times(G)

# get the nearest network nodes to two lat/lng points with the distance module
orig = ox.distance.nearest_nodes(G, X=-122.245846, Y=37.828903)
dest = ox.distance.nearest_nodes(G, X=-122.215006, Y=37.812303)

# find the shortest path between nodes, minimizing travel time, then plot it
route = ox.shortest_path(G, orig, dest, weight="travel_time")
fig, ax = ox.plot_graph_route(G, route, node_size=0)

 

결과물은 다음과 같다.

 

공식 홈페이지에 추가 예제 코드들이 많아서 추후에 더 공부해보면 좋을 것 같다. 

https://github.com/gboeing/osmnx-examples/tree/main/notebooks