Skip to content

Commit

Permalink
Merge branch '128-pipes-not-under-roads' into sensitivity_analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobson committed Apr 23, 2024
2 parents dbda1cc + 7212423 commit b7dfa43
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ numpy==1.26.4
# swmmanywhere (pyproject.toml)
# tifffile
# xarray
osmnx==1.8.1
osmnx==1.9.1
# via swmmanywhere (pyproject.toml)
packaging==23.2
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ numpy==1.26.4
# swmmanywhere (pyproject.toml)
# tifffile
# xarray
osmnx==1.8.1
osmnx==1.9.1
# via swmmanywhere (pyproject.toml)
packaging==23.2
# via
Expand Down
37 changes: 37 additions & 0 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,43 @@ def __call__(self,
for u, v, key in edges_to_remove:
G.remove_edge(u, v, key)
return G

@register_graphfcn
class remove_non_pipe_allowable_links(BaseGraphFunction):
"""remove_non_pipe_allowable_links class."""
def __call__(self,
G: nx.Graph,
topology_derivation: parameters.TopologyDerivation,
**kwargs) -> nx.Graph:
"""Remove non-pipe allowable links.
This function removes links that are not allowable for pipes. The non-
allowable links are specified in the 'omit_edges' attribute of the
topology_derivation parameter. If the omit property is present in the
'highway' attribute of the edge (e.g., 'motorway' is a category under
'highway'), the edge is removed. If the omit property is not a 'highway'
attribute, the edge is removed if the omit property is not null in the
edge data (e.g., any type of 'bridge').
Args:
G (nx.Graph): A graph
topology_derivation (parameters.TopologyDerivation): A TopologyDerivation
parameter object
**kwargs: Additional keyword arguments are ignored.
Returns:
G (nx.Graph): A graph
"""
edges_to_remove = set()
for u, v, keys, data in G.edges(data=True,keys = True):
for omit in topology_derivation.omit_edges:
if data.get('highway', None) == omit:
edges_to_remove.add((u, v, keys))
elif data.get(omit, None):
edges_to_remove.add((u, v, keys))
for u, v, keys in edges_to_remove:
G.remove_edge(u, v, keys)
return G

@register_graphfcn
class format_osmnx_lanes(BaseGraphFunction,
Expand Down
8 changes: 8 additions & 0 deletions swmmanywhere/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class TopologyDerivation(BaseModel):
unit = "-",
description = "Weights for topo derivation")

omit_edges: list = Field(default = ['motorway',
'motorway_link',
'bridge',
'tunnel'],
min_items = 1,
unit = "-",
description = "OSM paths pipes are not allowed under")

chahinian_slope_scaling: float = Field(default = 1,
le = 1,
ge = 0,
Expand Down
3 changes: 2 additions & 1 deletion swmmanywhere/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def download_street(bbox: tuple[float, float, float, float]) -> nx.MultiDiGraph:
``truncate_by_edge set`` to True.
"""
west, south, east, north = bbox
bbox = (north, south, east, west) # not sure why osmnx uses this order
graph = ox.graph_from_bbox(
north, south, east, west, network_type="drive", truncate_by_edge=True
bbox = bbox, network_type="drive", truncate_by_edge=True
)
return cast("nx.MultiDiGraph", graph)

Expand Down
1 change: 1 addition & 0 deletions tests/test_data/demo_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ starting_graph: null
graphfcn_list:
- assign_id
- format_osmnx_lanes
- remove_non_pipe_allowable_links
- double_directed
- fix_geometries
- split_long_edges
Expand Down
25 changes: 24 additions & 1 deletion tests/test_graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,30 @@ def test_pipe_by_pipe():
for u, d in G.nodes(data=True):
assert 'chamber_floor_elevation' in d.keys()
assert math.isfinite(d['chamber_floor_elevation'])


def get_edge_types(G):
"""Get the edge types in the graph."""
edge_types = set()
for u,v,d in G.edges(data=True):
if isinstance(d['highway'], list):
edge_types.union(d['highway'])
else:
edge_types.add(d['highway'])
return edge_types

def test_remove_non_pipe_allowable_links():
"""Test the remove_non_pipe_allowable_links function."""
G = load_graph(Path(__file__).parent / 'test_data' / 'street_graph.json')
# Ensure some invalid paths
topology_params = parameters.TopologyDerivation(omit_edges = ['primary', 'bridge'])
assert len(set([d.get('bridge',None) for u,v,d in G.edges(data=True)])) > 1
assert 'primary' in get_edge_types(G)

G_ = gu.remove_non_pipe_allowable_links(G, topology_params)
assert 'primary' not in get_edge_types(G_)
assert len(set([d.get('bridge',None) for u,v,d in G_.edges(data=True)])) == 1


def test_iterate_graphfcns():
"""Test the iterate_graphfcns function."""
G = load_graph(Path(__file__).parent / 'test_data' / 'graph_topo_derived.json')
Expand Down

0 comments on commit b7dfa43

Please sign in to comment.