osmnx icon indicating copy to clipboard operation
osmnx copied to clipboard

Add function to merge parallel roads

Open EwoutH opened this issue 2 years ago • 3 comments

Contributing guidelines

  • [X] I understand the contributing guidelines

Documentation

  • [X] My proposal is not addressed by the documentation or examples

Existing issues

  • [X] Nothing similar appears in an existing issue

What problem does your feature proposal solve?

OSMnx currently has two simplifications options:

  • simplify_graph
  • consolidate_intersections

I think there is a use case for a third one: merge_parallel_edges. It does exactly what it says: If you have multiple parallel edges – both starting and ending at the same node – it merges it to a single edge. This can be very useful for more mesoscopic models that don't need the extensive detail of for example multiple parallel highways.

What is your proposed solution?

Introduce a merge_parallel_edges function to simplify MultiDiGraphs into DiGraphs by merging parallel edges.

Some considerations:

  • NetworkX doesn’t have a built-in function for converting to DiGraph (that gives any control over which edges to keep or how to merge)
  • Some attributes should be nicely aggerated – for example, lane count can be summed
  • You might want to have an argument to exclude some edges from merging – for example if the max_speed is different (then you won’t end up with a clean DiGraph though)
  • In most cases, you want to merge parallel edges after consolidating intersections

What alternatives have you considered?

Manual pre-OSMnx manipulation of graph data to merge edges is inefficient and prone to errors. It also doesn't handle merging attributes cleanly.

Additional context

A basic API outline:

def merge_parallel_edges(G, attribute_merge_strategy={'lanes': 'sum'}, exclude_criteria=None):
    """
    Merges parallel edges in G. Allows attribute aggregation and exclusion based on criteria.
    """
    # Placeholder for implementation

CC @anastassiavybornova, @martinfleis, @jdmcbr and @jGaboardi

EwoutH avatar Apr 18 '24 18:04 EwoutH

Some relevant discussion https://github.com/gboeing/osmnx/pull/450#issuecomment-631042628. I also had a short email exchange with @gboeing at the time but it lead to no result apart from some explorations (available in https://github.com/martinfleis/network_simplification).

martinfleis avatar Apr 19 '24 07:04 martinfleis

Introduce a merge_parallel_edges function to simplify MultiDiGraphs into DiGraphs by merging parallel edges.

Note that the convert module's to_digraph function currently does similar: converting a MultiDiGraph to a DiGraph by choosing between parallel edges by minimizing some weight attribute. This does not aggregate attributes across the parallel edges.

gboeing avatar Apr 26 '24 17:04 gboeing

In light of my prior comment and @martinfleis comment, I'm inclined to either:

  1. close this issue because OSMnx already has the functionality to convert a MultiDiGraph to a DiGraph by choosing between parallel edges by minimizing some weight attribute, or

  2. add just a limited enhancement to give the existing convert.to_digraph the ability to aggregate attributes across the parallel edges if this is truly useful for general use cases

As noted above, in prior conversation with @martinfleis, it is nontrivial to address this problem well (from both topological and geometric perspectives) and OSMnx may not be the right home (though I'm potentially open to it) for such functionality if a robust and generalizable algorithm is eventually developed.

gboeing avatar May 27 '24 17:05 gboeing

No one seemed enthused about option 2, so I guess I will go with option 1 for now and close this.

gboeing avatar Jul 05 '24 17:07 gboeing