I'm working with NYC taxi dataset. Street network are obtained using osmnx and red dots are taxi pickup GPS locations.
I did some map-matching. I projected the GPS locations to its nearest edge, and the projections are treated as nodes and added to the network (graph), its edges were updated accordingly. I get the following network. The yellow dots are the real nodes, and the purple dots are the projections of the GPS locations (also treated as nodes but differentiated by its attribute).
Next I want to find the shortest path between two purple dots. For instance I get the following.
The red line is obviously NOT the shortest path. It seems to me that the shortest_path function accounts for the direction of the edges (i.e. for an edge, it always goes from the starting point to the end point). Is there a way that I can get the true shortest path? In fact, I only want the shortest network distance between two points.
I guess I could duplicate each edge with its starting and end points reversed. But maybe there's a cleaner way to do this?
The current answer is incorrect because it allows travel the wrong way down a one-way street. In reality, you need to calculate two shortest paths: one from origin to destination and one from destination to origin. The shortest network distance in either direction is the minimum of these two.
(Note: if directionality doesn't matter because the mode, such as walking, does not follow street directionality constraints, then provide a
network_type
argument likewalk
when creating the graph to get a fully bidirectional graph.)Complete minimal working example: