Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating shortest paths from points that originates mid-edge

Tags:

networkx

osmnx

I have a set of origin-destination coordinates that I want to calculate the shortest paths between them.

My origin-destination coordinates are sometimes located in the middle of a long straight-line road. However, the shortest path calculated by OSMnx/networkx will not consider that mid-edge to nearest-node path.

Is there any ready function in OSMnx or networkx that I can use to find shortest path that originates/ends in the middle of the road?

If there is no such function, I am thinking of using the following steps.

  1. Get nearest edges of origin and destination
  2. Get nodes of those nearest edges: let's say (a,b) for origin, and (c,d) for destination
  3. Calculate distance of 4 possible combinations: a->c, a->d, b->c, b->d
  4. Project origin/destination onto their nearest edges: let's call them o1 and e1
  5. Calculate distance o1->a, o1->b, e1->c, e1->d
  6. Add (5) distance to (3): to get
    • o1->a->c->e1
    • o1->a->d->e1
    • o1->b->c->e1
    • o1->b->d->e1
  7. Select path with smallest distance
like image 412
ShizukuF Avatar asked Nov 19 '25 02:11

ShizukuF


1 Answers

OSMnx produces a networkx graph object for routing/analysis. As you note, networkx shortest path calculation takes an origin and a destination node, so trying to calculate a shortest graph path from an edge midpoint won't work.

A couple things you could try:

  1. try to set simplify=False when you create the graph to retain as many nodes in the middle of streets as possible.
  2. if that doesn't work, you could try to subdivide edges (with greater than some threshold length) into 50 meter chunks or somesuch to discretize them with more nodes.

See also: https://stackoverflow.com/a/55601732/7321942

like image 191
gboeing Avatar answered Nov 22 '25 03:11

gboeing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!