Skip to content

Commit

Permalink
rename function to consolidate_edges
Browse files Browse the repository at this point in the history
  • Loading branch information
piaulous committed Sep 6, 2023
1 parent 85a2b1b commit 656f85b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions ding0/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
build_graph_from_ways, create_buffer_polygons, graph_nodes_outside_buffer_polys, \
compose_graph, get_fully_conn_graph, split_conn_graph, get_outer_conn_graph, \
handle_detour_edges, add_edge_geometry_entry, remove_unloaded_deadends, \
flatten_graph_components_to_lines, subdivide_graph_edges, simplify_graph_adv, \
consolidate_edges, subdivide_graph_edges, simplify_graph_adv, \
create_simple_synthetic_graph

from ding0.grid.lv_grid.clustering import get_cluster_numbers, distance_restricted_clustering
Expand Down Expand Up @@ -803,7 +803,7 @@ def import_lv_load_areas_and_build_new_lv_districts(
# Process outer graph
outer_graph = get_outer_conn_graph(outer_graph, inner_node_list)
outer_graph = add_edge_geometry_entry(outer_graph)
outer_graph = flatten_graph_components_to_lines(outer_graph, inner_node_list)
outer_graph = consolidate_edges(outer_graph, inner_node_list)
outer_graph = handle_detour_edges(outer_graph, level="mv", mode='remove')

# Compose graph
Expand Down
32 changes: 15 additions & 17 deletions ding0/grid/lv_grid/graph_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,49 +520,47 @@ def simplify_graph_adv(G, street_load_nodes, strict=True, remove_rings=True):
logger.debug(msg)
return G

def flatten_graph_components_to_lines(G, inner_node_list):
def consolidate_edges(graph, inner_node_list):
"""
Build single edges based on outter graph component to connect
isolated components/ nodes in inner graph/ not buffered area.
"""
# TODO: add edge tags 'highway' and 'osmid' to shortest path edge

components = list(nx.weakly_connected_components(G))
sp_path = lambda p1, p2: nx.shortest_path(G, p1, p2, weight='length') if nx.has_path(G, p1, p2) else None
conn_comps = list(nx.weakly_connected_components(graph))
sp_path = lambda p1, p2: nx.shortest_path(graph, p1, p2, weight='length') \
if nx.has_path(graph, p1, p2) else None

nodes_to_remove = []
edges_to_add = []
common_nodes = set(G.nodes()) & set(inner_node_list)
common_nodes = set(graph.nodes()) & set(inner_node_list)

for comp in components:
conn_nodes = list(comp & common_nodes)
if len(comp) > 2 and len(conn_nodes) == 1:
G.remove_nodes_from(comp) # removes unwanted islands / loops
for cc in conn_comps:
conn_nodes = list(cc & common_nodes)
if len(cc) > 2 and len(conn_nodes) == 1:
graph.remove_nodes_from(cc) # removes unwanted islands / loops

else:
endpoints = combinations(conn_nodes, 2)
paths = [sp_path(n[0], n[1]) for n in endpoints]
for path in paths:
geoms = []
for u, v in zip(path[:-1], path[1:]):
geom = G.edges[u,v,0]['geometry']
# deprecated due to add_edge_geometry_entry(G)
# try: geom = G.edges[u,v,0]['geometry']
# except: geom = LineString([Point((G.nodes[node]["x"], G.nodes[node]["y"])) for node in [u,v]])
geom = graph.edges[u,v,0]['geometry']
geoms.append(geom)

merged_line = linemerge(MultiLineString(geoms))
edges_to_add.append([path[0], path[-1], merged_line])
nodes_to_remove.append(list(set(comp) - set(conn_nodes)))
nodes_to_remove.append(list(set(cc) - set(conn_nodes)))

for nodes in nodes_to_remove:
G.remove_nodes_from(nodes)
graph.remove_nodes_from(nodes)

for edge in edges_to_add:
G.add_edge(edge[0], edge[1], 0, geometry=edge[2], length=edge[2].length)
G.add_edge(edge[1], edge[0], 0, geometry=edge[2], length=edge[2].length)
graph.add_edge(edge[0], edge[1], 0, geometry=edge[2], length=edge[2].length)
graph.add_edge(edge[1], edge[0], 0, geometry=edge[2], length=edge[2].length)

return G
return graph

def handle_detour_edges(graph, level="mv", mode='remove'):
"""
Expand Down

0 comments on commit 656f85b

Please sign in to comment.