3

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
ShizukuF
  • 31
  • 3
  • "However, the shortest path calculated by OSMnx will discard that mid-edge to nearest-node path." -- If that behaviour is not documented in the documentation, then that constitutes enough to warrant a bug report on their [github](https://github.com/gboeing/osmnx). The maintainer seems super-responsive (1.6 K stars, 210 closed issues, 4 open issues -- what a legend). Otherwise, your workaround seems solid. – Paul Brodersen Apr 09 '19 at 17:23
  • Thank you for your comment. The maintainer already replied below. I'll try with his suggestion, else I'll go with my workaround. – ShizukuF Apr 10 '19 at 02:41

1 Answers1

1

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you for your answers. I already set simplify=False, but as I noted that my origins is in the middle of the straight-line road. I will try with your suggestion no. 2. – ShizukuF Apr 10 '19 at 02:40