Skip to content

Commit

Permalink
Merge branch '17-subcatchment-delineation' into 135-roads-at-boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobson committed May 3, 2024
2 parents c51df51 + a08b9cd commit 823d2b1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
6 changes: 3 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ julian==0.14
# via pyswmm
kiwisolver==1.4.5
# via matplotlib
lazy-loader==0.3
lazy-loader==0.4
# via scikit-image
llvmlite==0.41.1
# via numba
Expand All @@ -133,7 +133,7 @@ matplotlib==3.8.2
# swmmanywhere (pyproject.toml)
multiprocess==0.70.15
# via salib
mypy==1.8.0
mypy==1.10.0
# via
# pytest-mypy
# swmmanywhere (pyproject.toml)
Expand Down Expand Up @@ -308,7 +308,7 @@ smmap==5.0.1
# via gitdb
snuggs==1.4.7
# via rasterio
swmm-toolkit==0.15.3
swmm-toolkit==0.15.5
# via pyswmm
tifffile==2024.1.30
# via scikit-image
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ julian==0.14
# via pyswmm
kiwisolver==1.4.5
# via matplotlib
lazy-loader==0.3
lazy-loader==0.4
# via scikit-image
llvmlite==0.41.1
# via numba
Expand Down Expand Up @@ -250,7 +250,7 @@ smmap==5.0.1
# via gitdb
snuggs==1.4.7
# via rasterio
swmm-toolkit==0.15.3
swmm-toolkit==0.15.5
# via pyswmm
tifffile==2024.1.30
# via scikit-image
Expand Down
42 changes: 14 additions & 28 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def iterate_graphfcns(G: nx.Graph,
G = graphfcns[function](G, addresses = addresses, **params)
logger.info(f"graphfcn: {function} completed.")
if verbose:
save_graph(G, addresses.model / f"{function}_graph.json")
save_graph(graphfcns.fix_geometries(G),
addresses.model / f"{function}_graph.json")
return G

@register_graphfcn
Expand Down Expand Up @@ -221,8 +222,8 @@ def __call__(self,
return G

@register_graphfcn
class remove_isolated_nodes(BaseGraphFunction):
"""remove_isolated_nodes class."""
class remove_parallel_edges(BaseGraphFunction):
"""remove_parallel_edges class."""

def __call__(self, G: nx.Graph, **kwargs) -> nx.Graph:
"""Remove parallel edges from a street network.
Expand Down Expand Up @@ -373,7 +374,7 @@ def __call__(self, G: nx.Graph, **kwargs) -> nx.Graph:
"""
# Convert to directed
G_new = G.copy()
G_new = nx.MultiDiGraph(G_new)
G_new = nx.MultiDiGraph(G.copy())

# MultiDiGraph adds edges in both directions, but rivers (and geometries)
# are only in one direction. So we remove the reverse edges and add them
Expand Down Expand Up @@ -446,29 +447,20 @@ def __call__(self,
"""
max_length = subcatchment_derivation.max_street_length

new_edges = {}
new_nodes = set()

# Split edges
for u, v, d in G.edges(data=True):
# Get new geometry
new_linestring = shapely.segmentize(d['geometry'], max_length)

# Create a node at each linestring segment
new_nodes.update(
[(x,y) for x,y in
np.reshape(
shapely.get_coordinates(new_linestring),
(-1, 2))
]
)

new_linestrings = shapely.segmentize([d['geometry']
for u,v,d in G.edges(data=True)],
max_length)
new_nodes = shapely.get_coordinates(new_linestrings)


new_edges = {}
for new_linestring, (u,v,d) in zip(new_linestrings, G.edges(data=True)):
# Create an arc for each segment
for start, end in zip(new_linestring.coords[:-1],
new_linestring.coords[1:]):
geom = shapely.LineString([start, end])
new_edges[(start, end, 0)] = {**d,
'geometry' : geom,
'length' : geom.length
}

Expand All @@ -479,7 +471,7 @@ def __call__(self,
nx.set_edge_attributes(new_graph, new_edges)
nx.set_node_attributes(
new_graph,
{node: {'x': node[0], 'y': node[1]} for node in new_nodes}
{tuple(node): {'x': node[0], 'y': node[1]} for node in new_nodes}
)
return nx.relabel_nodes(new_graph,
{node: ix for ix, node in enumerate(new_graph.nodes)}
Expand Down Expand Up @@ -539,12 +531,6 @@ def __call__(self,
self_loops = list(nx.selfloop_edges(G))
G.remove_edges_from(self_loops)

# Recalculate geometries
for u, v, data in G.edges(data=True):
data['geometry'] = shapely.LineString([(G.nodes[u]['x'],
G.nodes[u]['y']),
(G.nodes[v]['x'],
G.nodes[v]['y'])])
return G

@register_graphfcn
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data/demo_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ graphfcn_list:
- fix_geometries # Ensure consistent geometry directions
- remove_non_pipe_allowable_links # Filter out things like bridges/motorways
- calculate_streetcover # Create shapefile of impervious street cover
- remove_parallel_edges # Remove parallel edges retaining the shorter one
- to_undirected # Convert graph to undirected to facilitate cleanup
- split_long_edges # Set a maximum edge length
- merge_nodes # Merge nodes that are too close together
Expand All @@ -33,6 +34,7 @@ graphfcn_list:
- identify_outlets # Identify potential street->river outlets
- derive_topology # Shortest path to give network topology
- pipe_by_pipe # Design pipe diameters and depths
- fix_geometries # Ensure geometries present before printing
- assign_id # Final pass to ensure consistent 'id' and remove duplicates
metric_list:
- nc_deltacon0
Expand Down

0 comments on commit 823d2b1

Please sign in to comment.