Skip to content

Commit

Permalink
Merge branch 'main' into 7-downloader-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Mar 26, 2024
2 parents 4ce5f67 + 6021815 commit 7044fb7
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 57 deletions.
7 changes: 2 additions & 5 deletions swmmanywhere/geospatial_utilities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# -*- coding: utf-8 -*-
"""Created 2024-01-20.
"""Geospatial utilities module for SWMManywhere.
A module containing functions to perform a variety of geospatial operations,
such as reprojecting coordinates and handling raster data.
@author: Barnaby Dobson
"""
import itertools
import json
Expand Down Expand Up @@ -420,7 +417,7 @@ def delineate_catchment(grid: pysheds.sgrid.sGrid,
# Snap the node to the nearest grid cell
x, y = data['x'], data['y']
grid_ = deepcopy(grid)
x_snap, y_snap = grid_.snap_to_mask(flow_acc > 5, (x, y))
x_snap, y_snap = grid_.snap_to_mask(flow_acc >= 0, (x, y))

# Delineate the catchment
catch = grid_.catchment(x=x_snap,
Expand Down
45 changes: 41 additions & 4 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Created on 2024-01-26.
"""Graph utilities module for SWMManywhere.
@author: Barney
A module to contain graphfcns, the graphfcn registry object, and other graph
utilities (such as save/load functions).
"""
import json
import os
Expand Down Expand Up @@ -208,11 +208,15 @@ def __call__(self,
G (nx.Graph): The same graph with an ID assigned to each edge
"""
edge_ids: set[str] = set()
for u, v, data in G.edges(data=True):
edges_to_remove = []
for u, v, key, data in G.edges(data=True, keys = True):
data['id'] = f'{u}-{v}'
if data['id'] in edge_ids:
logger.warning(f"Duplicate edge ID: {data['id']}")
edges_to_remove.append((u, v, key))
edge_ids.add(data['id'])
for u, v, key in edges_to_remove:
G.remove_edge(u, v, key)
return G

@register_graphfcn
Expand Down Expand Up @@ -409,6 +413,39 @@ def create_new_edge_data(line, data, id_):

return graph

@register_graphfcn
class fix_geometries(BaseGraphFunction,
required_edge_attributes = ['geometry'],
required_node_attributes = ['x', 'y']):
"""fix_geometries class."""
def __call__(self, G: nx.Graph, **kwargs) -> nx.Graph:
"""Fix the geometries of the edges.
This function fixes the geometries of the edges. The geometries are
recalculated from the node coordinates.
Args:
G (nx.Graph): A graph
**kwargs: Additional keyword arguments are ignored.
Returns:
G (nx.Graph): A graph
"""
G = G.copy()
for u, v, data in G.edges(data=True):
start_point_node = (G.nodes[u]['x'], G.nodes[u]['y'])
start_point_edge = data['geometry'].coords[0]
end_point_node = (G.nodes[v]['x'], G.nodes[v]['y'])
end_point_edge = data['geometry'].coords[-1]
if (start_point_edge == end_point_node) & \
(end_point_edge == start_point_node):
data['geometry'] = data['geometry'].reverse()
elif (start_point_edge != start_point_node) | \
(end_point_edge != end_point_node):
data['geometry'] = shapely.LineString([start_point_node,
end_point_node])
return G

@register_graphfcn
class calculate_contributing_area(BaseGraphFunction,
required_edge_attributes = ['id', 'geometry', 'width'],
Expand Down
15 changes: 11 additions & 4 deletions swmmanywhere/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# -*- coding: utf-8 -*-
"""Created on 2024-03-04.
@author: Barney
"""Logging module for SWMManywhere.
Example:
>>> import os
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "true"
>>> # logging is now enabled in any swmmanywhere module
>>> from swmmanywhere.logging import logger # You can now log yourself
>>> logger.info("This is an info message.") # Write to stdout # doctest: +SKIP
This is an info message.
>>> logger.add("file.log") # Add a log file # doctest: +SKIP
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "false" # Disable logging
"""
import os
import sys
Expand Down
Loading

0 comments on commit 7044fb7

Please sign in to comment.