@@ -110,8 +110,16 @@ def linkbikenet(
110110 wcc = [H .subgraph (c ).copy () for c in sorted (nx .connected_components (H ), key = lambda c : sum (
111111 [l [- 1 ] for l in H .subgraph (c ).copy ().edges .data ('length' )]), reverse = True )]
112112
113+ # Nodes belonging to the original largest component
114+ main_component = set (wcc [0 ])
115+
116+ # Mark all edges in the original largest component as step 0
117+ for u , v in H .subgraph (main_component ).edges ():
118+ H [u ][v ]["lcc_step" ] = 0
119+
113120 to_iterate = len (wcc ) - 1
114121 closest_pairs = []
122+ step = 1
115123
116124 # check which strategy was chosen and execute the corresponding algorithm
117125 print ("Calculating links..." )
@@ -120,24 +128,69 @@ def linkbikenet(
120128 wcc = [H .subgraph (c ).copy () for c in sorted (nx .connected_components (H ), key = lambda c : sum (
121129 [l [- 1 ] for l in H .subgraph (c ).copy ().edges .data ('length' )]), reverse = True )]
122130 pair = pair_between_largest_components (wcc )
131+ # Determine which components contain u and v
132+ component_u = next (c for c in wcc if pair [0 ] in c )
133+ component_v = next (c for c in wcc if pair [1 ] in c )
134+ u_in_main = pair [0 ] in main_component
135+ v_in_main = pair [1 ] in main_component
123136 closest_pairs .append (pair )
124- H .add_edge (pair [0 ], pair [1 ], length = 0 )
137+ H .add_edge (pair [0 ], pair [1 ], length = 0 , lcc_step = None )
138+ if u_in_main and not v_in_main :
139+ mark_joined_component (H , component_v , step )
140+ main_component .update (component_v )
141+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
142+
143+ elif v_in_main and not u_in_main :
144+ mark_joined_component (H , component_u , step )
145+ main_component .update (component_u )
146+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
147+ step += 1
125148
126149 elif connection_strategy == "largest_closest" :
127150 for i in range (to_iterate ):
128151 wcc = [H .subgraph (c ).copy () for c in sorted (nx .connected_components (H ), key = lambda c : sum (
129152 [l [- 1 ] for l in H .subgraph (c ).copy ().edges .data ('length' )]), reverse = True )]
130153 pair = pair_between_largest_and_closest_components (wcc )
154+ # Determine which components contain u and v
155+ component_u = next (c for c in wcc if pair [0 ] in c )
156+ component_v = next (c for c in wcc if pair [1 ] in c )
157+ u_in_main = pair [0 ] in main_component
158+ v_in_main = pair [1 ] in main_component
131159 closest_pairs .append (pair )
132- H .add_edge (pair [0 ], pair [1 ], length = 0 )
160+ H .add_edge (pair [0 ], pair [1 ], length = 0 , lcc_step = None )
161+ if u_in_main and not v_in_main :
162+ mark_joined_component (H , component_v , step )
163+ main_component .update (component_v )
164+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
165+
166+ elif v_in_main and not u_in_main :
167+ mark_joined_component (H , component_u , step )
168+ main_component .update (component_u )
169+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
170+ step += 1
133171
134172 elif connection_strategy == "closest" :
135173 for i in range (to_iterate ):
136174 wcc = [H .subgraph (c ).copy () for c in sorted (nx .connected_components (H ), key = lambda c : sum (
137175 [l [- 1 ] for l in H .subgraph (c ).copy ().edges .data ('length' )]), reverse = True )]
138176 pair = pair_between_closest_components (wcc )
177+ # Determine which components contain u and v
178+ component_u = next (c for c in wcc if pair [0 ] in c )
179+ component_v = next (c for c in wcc if pair [1 ] in c )
180+ u_in_main = pair [0 ] in main_component
181+ v_in_main = pair [1 ] in main_component
139182 closest_pairs .append (pair )
140- H .add_edge (pair [0 ], pair [1 ], length = 0 )
183+ H .add_edge (pair [0 ], pair [1 ], length = 0 , lcc_step = None )
184+ if u_in_main and not v_in_main :
185+ mark_joined_component (H , component_v , step )
186+ main_component .update (component_v )
187+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
188+
189+ elif v_in_main and not u_in_main :
190+ mark_joined_component (H , component_u , step )
191+ main_component .update (component_u )
192+ H [pair [0 ]][pair [1 ]]["lcc_step" ] = step
193+ step += 1
141194
142195 # find paths between node pairs so we can generate geometries
143196 paths = []
@@ -148,6 +201,9 @@ def linkbikenet(
148201 continue
149202 paths .append (path )
150203
204+ H .remove_edges_from (closest_pairs )
205+ edges_pbi_gdf = graph_edges_to_gdf (H )
206+
151207 edges_gdf = graph_edges_to_gdf (G )
152208 df = pd .DataFrame ()
153209 df ['nodelist' ] = paths
@@ -175,7 +231,10 @@ def linkbikenet(
175231 gdf ['network_length' ] = network_lengths
176232 gdf ['lcc_length' ] = lcc_lengths
177233
178- edges_pbi_gdf = edges_gdf [edges_gdf ["pbi" ] == 1 ]
234+ gdf ['lcc_share' ] = gdf ['lcc_length' ] / gdf ['network_length' ]
235+ gdf ['lcc_gain' ] = gdf ['lcc_length' ].diff ().fillna (0 )
236+
237+ #edges_pbi_gdf = edges_gdf[edges_gdf["pbi"] == 1]
179238
180239 # Back to unprojected (potentially). No more calculations after here.
181240 gdf .to_crs (epsg = 4326 , inplace = True )
0 commit comments