Skip to content

Commit

Permalink
Update graph_utilities.py
Browse files Browse the repository at this point in the history
vectorise some operations
  • Loading branch information
Dobson committed Apr 30, 2024
1 parent ad665db commit 5658819
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,23 +460,15 @@ 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:]):
Expand All @@ -493,7 +485,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

0 comments on commit 5658819

Please sign in to comment.