From 6d8c072836984a92e8d41c3eb1c9494968021089 Mon Sep 17 00:00:00 2001 From: Gareth Simons Date: Thu, 7 Dec 2023 22:31:35 +0000 Subject: [PATCH] fixes sporadic bug in node consolidation --- pyproject.toml | 2 +- pysrc/cityseer/tools/graphs.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a20e592e..68e53a03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cityseer" -version = '4.6.3b1' +version = '4.6.3b2' description = "Computational tools for network-based pedestrian-scale urban analysis" readme = "README.md" requires-python = ">=3.10, <3.12" diff --git a/pysrc/cityseer/tools/graphs.py b/pysrc/cityseer/tools/graphs.py index 6f0318d0..f27eeaec 100644 --- a/pysrc/cityseer/tools/graphs.py +++ b/pysrc/cityseer/tools/graphs.py @@ -563,8 +563,15 @@ def _squash_adjacent( raise ValueError(f"Attempted to add a duplicate node for node_group {node_group}.") # iterate the nodes to be removed and connect their existing edge geometries to the new centroid for nd_key in node_group: + nd_data: NodeData = nx_multigraph.nodes[nd_key] + nd_xy = (nd_data["x"], nd_data["y"]) # iterate the node's existing neighbours for nb_nd_key in nx.neighbors(nx_multigraph, nd_key): + # no need to rewire the edge if the neighbour is the same as the new node + # this would otherwise result in a zero length edge + # the edge will be dropped once the nd_key is removed + if nb_nd_key == new_nd_name: + continue # if a neighbour is also going to be dropped, then no need to create new between edges # an exception exists when a geom is looped, in which case the neighbour is also the current node if nb_nd_key in node_group and nb_nd_key != nd_key: @@ -581,8 +588,6 @@ def _squash_adjacent( f"for edge {nd_key}-{nb_nd_key}." ) # orient the LineString so that the geom starts from the node's x_y - nd_data: NodeData = nx_multigraph.nodes[nd_key] - nd_xy = (nd_data["x"], nd_data["y"]) line_coords = util.align_linestring_coords(line_geom.coords, nd_xy) # update geom starting point to new parent node's coordinates line_coords = util.snap_linestring_startpoint(line_coords, (new_cent.x, new_cent.y)) @@ -594,6 +599,8 @@ def _squash_adjacent( target_nd_key = nb_nd_key # build the new geom new_edge_geom = geometry.LineString(line_coords) + if new_edge_geom.length == 0: + raise ValueError(f"Attempted to add a zero length edge from {new_nd_name} to {target_nd_key}") # check that a duplicate is not being added dupe = False if nx_multigraph.has_edge(new_nd_name, target_nd_key):