@@ -38,6 +38,7 @@ def nx_graph_from_osmnx(
3838 xy : bool = True ,
3939 custom_filter : Optional [str ] = None ,
4040 additional_metadata_keys : Optional [set ] = None ,
41+ filter_to_largest_component : bool = True ,
4142) -> nx .MultiDiGraph :
4243 """
4344 Build a networkx graph from OSM data
@@ -48,6 +49,8 @@ def nx_graph_from_osmnx(
4849 xy: whether to use xy coordinates or lat/lon
4950 custom_filter: a custom filter to pass to osmnx
5051 additional_metadata_keys: additional keys to preserve in metadata
52+ filter_to_largest_component: if True, keep only the largest strongly connected component;
53+ if False, keep all components (may result in routing failures between disconnected components)
5154
5255 Returns:
5356 a networkx graph of the OSM network
@@ -68,6 +71,7 @@ def nx_graph_from_osmnx(
6871 network_type ,
6972 xy = xy ,
7073 additional_metadata_keys = additional_metadata_keys ,
74+ filter_to_largest_component = filter_to_largest_component ,
7175 )
7276
7377
@@ -76,6 +80,7 @@ def parse_osmnx_graph(
7680 network_type : NetworkType ,
7781 xy : bool = True ,
7882 additional_metadata_keys : Optional [set ] = None ,
83+ filter_to_largest_component : bool = True ,
7984) -> nx .MultiDiGraph :
8085 """
8186 Parse the raw osmnx graph into a graph that we can use with our NxMap
@@ -85,6 +90,8 @@ def parse_osmnx_graph(
8590 xy: whether to use xy coordinates or lat/lon
8691 network_type: the network type to use for the graph
8792 additional_metadata_keys: additional keys to preserve in metadata
93+ filter_to_largest_component: if True, keep only the largest strongly connected component;
94+ if False, keep all components (may result in routing failures between disconnected components)
8895
8996 Returns:
9097 a cleaned networkx graph of the OSM network
@@ -107,15 +114,16 @@ def parse_osmnx_graph(
107114 nx .set_edge_attributes (g , kilometers , "kilometers" )
108115
109116 # this makes sure there are no graph 'dead-ends'
110- sg_components = nx .strongly_connected_components (g )
117+ if filter_to_largest_component :
118+ sg_components = nx .strongly_connected_components (g )
111119
112- if not sg_components :
113- raise MapException (
114- "road network has no strongly connected components and is not routable; "
115- "check polygon boundaries."
116- )
120+ if not sg_components :
121+ raise MapException (
122+ "road network has no strongly connected components and is not routable; "
123+ "check polygon boundaries."
124+ )
117125
118- g = nx .MultiDiGraph (g .subgraph (max (sg_components , key = len )))
126+ g = nx .MultiDiGraph (g .subgraph (max (sg_components , key = len )))
119127
120128 for u , v , d in g .edges (data = True ):
121129 if "geometry" not in d :
0 commit comments