From 1e16176d24a5c3824850f135cb627ddfd49793f7 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 23 May 2024 16:01:05 +0100 Subject: [PATCH 1/2] Add exception and test --- swmmanywhere/prepare_data.py | 15 +++++++++++---- tests/test_prepare_data.py | 12 +++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/swmmanywhere/prepare_data.py b/swmmanywhere/prepare_data.py index 266c664f..0f61db13 100644 --- a/swmmanywhere/prepare_data.py +++ b/swmmanywhere/prepare_data.py @@ -123,10 +123,17 @@ def download_river(bbox: tuple[float, float, float, float]) -> nx.MultiDiGraph: ``truncate_by_edge set`` to True. """ west, south, east, north = bbox - graph = ox.graph_from_bbox( - bbox = (north, south, east, west), - truncate_by_edge=True, - custom_filter='["waterway"]') + try: + graph = ox.graph_from_bbox( + bbox = (north, south, east, west), + truncate_by_edge=True, + custom_filter='["waterway"]') + except ValueError as e: + if str(e) == "Found no graph nodes within the requested polygon": + logger.warning('No water network found within the bounding box.') + return nx.MultiDiGraph() + else: + raise # Re-raise the exception return cast("nx.MultiDiGraph", graph) diff --git a/tests/test_prepare_data.py b/tests/test_prepare_data.py index dca6fec3..109d8310 100644 --- a/tests/test_prepare_data.py +++ b/tests/test_prepare_data.py @@ -186,12 +186,22 @@ def test_river_downloader(): bbox = (0.0402, 51.55759, 0.09825591114207548, 51.6205) mock_graph = nx.MultiDiGraph() + mock_graph.add_node(1) # Mock ox.graph_from_bbox - with mock.patch.object(ox, 'graph_from_bbox', return_value=mock_graph): + with mock.patch.object(ox, + 'graph_from_bbox', + return_value=mock_graph) as mock_from_bbox: # Call download_street G = downloaders.download_river(bbox) assert G == mock_graph + mock_from_bbox.side_effect = ValueError( + "Found no graph nodes within the requested polygon" + ) + G = downloaders.download_river(bbox) + assert G.size() == 0 + + def test_elevation_downloader(): """Check elevation downloads, writes, contains data, and a known elevation.""" # Please do not reuse api_key From 85d5b940d11ff0e67d9cce2ff9c263a2fadee26a Mon Sep 17 00:00:00 2001 From: barneydobson Date: Fri, 24 May 2024 08:55:07 +0100 Subject: [PATCH 2/2] Update prepare_data.py retain all --- swmmanywhere/prepare_data.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/swmmanywhere/prepare_data.py b/swmmanywhere/prepare_data.py index 0f61db13..0af0d40b 100644 --- a/swmmanywhere/prepare_data.py +++ b/swmmanywhere/prepare_data.py @@ -127,7 +127,8 @@ def download_river(bbox: tuple[float, float, float, float]) -> nx.MultiDiGraph: graph = ox.graph_from_bbox( bbox = (north, south, east, west), truncate_by_edge=True, - custom_filter='["waterway"]') + custom_filter='["waterway"]', + retain_all=True) except ValueError as e: if str(e) == "Found no graph nodes within the requested polygon": logger.warning('No water network found within the bounding box.')