// STEP 7
// for every group of merged nodes with more than 1 node in it, extend the
// edge geometries to reach the new node point
new_edges = utils_graph.graph_to_gdfs(H, nodes=False).reset_index()
for cluster_label, nodes_subset in groups:
// but only if there were multiple nodes merged together,
// otherwise it"s the same old edge as in original graph
if len(nodes_subset) > 1:
// get coords of merged nodes point centroid to prepend or
// append to the old edge geom"s coords
x = H.nodes[cluster_label]["x"]
y = H.nodes[cluster_label]["y"]
xy = [(x, y)]
// for each edge incident to this new merged node, update
// its geometry to extend to/from the new node"s point coords
mask = (new_edges["u"] == cluster_label) | (new_edges["v"] == cluster_label)
for u, v, k in new_edges.loc[mask, ["u", "v", "key"]].values:
old_coords = list(H.edges[u, v, k]["geometry"].coords)
new_coords = xy + old_coords if cluster_label == u else old_coords + xy
new_geom = LineString(new_coords)
After Change
// for each edge incident to this new merged node, update its
// geometry to extend to/from the new node"s point coords
in_edges = set(H.in_edges(cluster_label, keys=True))
out_edges = set(H.out_edges(cluster_label, keys=True))
for u, v, k in in_edges | out_edges:
old_coords = list(H.edges[u, v, k]["geometry"].coords)
new_coords = xy + old_coords if cluster_label == u else old_coords + xy
new_geom = LineString(new_coords)