Skip to content

Commit

Permalink
only allow topological node consolidation to be run once on a graph
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Jan 25, 2025
1 parent ce0615b commit 7ced0e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions osmnx/_osm_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def _save_graph_xml(
)
warn(msg, category=UserWarning, stacklevel=2)

# raise error if graph has been simplified
if G.graph.get("simplified", False):
msg = "Graph must be unsimplified to save as OSM XML."
# raise error if graph has been simplified or nodes consolidated
if G.graph.get("simplified", False) or G.graph.get("consolidated", False):
msg = "Graph must be unsimplified and unconsolidated to save as OSM XML."
raise GraphSimplificationError(msg)

# set default filepath if None was provided
Expand Down
5 changes: 4 additions & 1 deletion osmnx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ def load_graphml(
raise ValueError(msg)

# specify default graph/node/edge attribute values' data types
default_graph_dtypes = {"simplified": _convert_bool_string}
default_graph_dtypes = {
"consolidated": _convert_bool_string,
"simplified": _convert_bool_string,
}
default_node_dtypes = {
"elevation": float,
"elevation_res": float,
Expand Down
7 changes: 7 additions & 0 deletions osmnx/simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ def _consolidate_intersections_rebuild_graph( # noqa: C901,PLR0912,PLR0915
A rebuilt graph with consolidated intersections and (optionally)
reconnected edge geometries.
"""
if G.graph.get("consolidated"): # pragma: no cover
msg = "This graph has already been consolidated, cannot consolidate it again."
raise GraphSimplificationError(msg)

# default node attributes to aggregate upon consolidation
if node_attr_aggs is None:
node_attr_aggs = {"elevation": "mean"}
Expand Down Expand Up @@ -715,6 +719,9 @@ def _consolidate_intersections_rebuild_graph( # noqa: C901,PLR0912,PLR0915
node_attrs[col] = unique_vals
Gc.add_node(cluster_label, **node_attrs)

# mark the graph as having been consolidated
G.graph["consolidated"] = True

if len(G.edges) == 0 or not reconnect_edges:
# if reconnect_edges is False or there are no edges in original graph
# (after dead-end removed), then skip edges and return new graph as-is
Expand Down

0 comments on commit 7ced0e2

Please sign in to comment.