Skip to content

Commit

Permalink
Merge branch 'main' into 24-repo-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Mar 26, 2024
2 parents 532692c + 7044fb7 commit 0d1f9b1
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 88 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module = "tests.*"
disallow_untyped_defs = false

[tool.pytest.ini_options]
# addopts = "-v --mypy -p no:warnings --cov=swmmanywhere --cov-report=html --doctest-modules --ignore=swmmanywhere/__main__.py"
addopts = "-v -p no:warnings --cov=swmmanywhere --cov-report=html --doctest-modules --ignore=swmmanywhere/__main__.py"

[tool.ruff]
select = ["D", "E", "F", "I"] # pydocstyle, pycodestyle, Pyflakes, isort
Expand Down
12 changes: 5 additions & 7 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 @@ -192,8 +189,9 @@ def get_transformer(source_crs: str,
Example:
>>> transformer = get_transformer('EPSG:4326', 'EPSG:32630')
>>> transformer.transform(-0.1276, 51.5074)
(699330.1106898375, 5710164.30300683)
>>> x, y = transformer.transform(-0.1276, 51.5074)
>>> print(f"{x:.6f}, {y:.6f}")
699330.110690, 5710164.303007
"""
return pyproj.Transformer.from_crs(source_crs,
target_crs,
Expand Down Expand Up @@ -419,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 0d1f9b1

Please sign in to comment.