1

I'm trying to do traffic flow simulation visualization using OSMnx to extract data from OpenStreetMaps using Python. I'm trying to do macroscopic traffic simulation regarding to agent-based congestion analysis. I tried the below code to find the shortest path and it works well.

import osmnx as ox
import networkx as nx
ox.config(log_console=True, use_cache=True)
# define the start and end locations in latlng
start_latlng = (37.78497,-122.43327)
end_latlng = (37.78071,-122.41445)
# location where you want to find your route
place     = 'San Francisco, California, United States'
# find shortest route based on the mode of travel
mode      = 'walk'        # 'drive', 'bike', 'walk'
# find shortest path based on distance or time
optimizer = 'time'        # 'length','time'
# create graph from OSM within the boundaries of some 
# geocodable place(s)
graph = ox.graph_from_place(place, network_type = mode)
# find the nearest node to the start location
orig_node = ox.get_nearest_node(graph, start_latlng)
# find the nearest node to the end location
dest_node = ox.get_nearest_node(graph, end_latlng)
#  find the shortest path
shortest_route = nx.shortest_path(graph,
                                  orig_node,
                                  dest_node,
                                  weight=optimizer)

But for traffic jam or congestion analysis I didn't find any documentation on how to include synthetic congestion data into OSMnx, like inserting more cars and visualize the simulation results using OSMnx maps in Python. Any help is appreciated, Thanks,

mg g
  • 21
  • 4

2 Answers2

0

The short answer is that OSMnx is not a traffic analysis or flow simulation package. You can either 1) update the graph edge weights to represent new impedance values given traffic conditions then re-solve paths, or 2) save your OSMnx network model and import it into a traffic simulation tool for further analysis.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thanks a lot @gboeing for your reply.. However, can we use geopanads for example to feed data into osmnx, please? And if yes, any documentation or example on this, plz? – mg g Aug 04 '22 at 11:57
  • Yes you can load GeoPandas GeoDataFrames into OSMnx as described in the [usage examples](https://github.com/gboeing/osmnx-examples) and [documentation](https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.utils_graph.graph_from_gdfs). – gboeing Aug 04 '22 at 18:05
0

As the OSMnx lead said, it's not a traffic simulator. However I've been playing with a few ideas here: https://github.com/donjpierce/traffic if that helps you get started.

I think the best way to do it is to retrieve a map from OSMnx, get all the (x,y) coordinates for every road, and use matplotlib to draw dots on the road where you want cars to be.

You can then update the location of every car (i.e. dot) by writing a physics simulation where each dot has a velocity and velocity vector pointing along the road. Update the position of all dots at some increment dt, save every frame as a PNG, and you will generally see cars "driving" along a road like you would expect. Congestion will occur naturally if you enforce a minimum distance D between cars so that dots don't collide / overlap.

donjpierce
  • 41
  • 3