Custom data demo¶
Note - this script can also be opened in interactive Python if you wanted to
play around. On the GitHub it is in docs/notebooks.
To run this on your local machine, you will need to install the optional dependencies
for doc
:
pip install swmmanywhere[doc]
Introduction¶
This script demonstrates how to use swmmanywhere
when you have custom data.
Since this is a notebook, we will define config
as a dictionary rather than a yaml
file, but the same principles apply.
Initial setup¶
We will use the same example as the extended demo, but with a custom elevation dataset. Let's start by rerunning it.
In [1]:
Copied!
# Imports
from __future__ import annotations
import tempfile
from pathlib import Path
import folium
import geopandas as gpd
from swmmanywhere.logging import set_verbose
from swmmanywhere.swmmanywhere import swmmanywhere
from swmmanywhere.utilities import plot_map
# Create temporary directory
temp_dir = tempfile.TemporaryDirectory(dir=".")
base_dir = Path(temp_dir.name)
# Define minimum viable config
bbox = (1.52740, 42.50524, 1.54273, 42.51259)
config = {
"base_dir": base_dir,
"project": "my_first_swmm",
"bbox": bbox,
"run_settings": {"duration": 3600},
"parameter_overrides": {
"topology_derivation": {
"allowable_networks": ["drive"],
"omit_edges": ["bridge"],
},
"outfall_derivation": {
"outfall_length": 5,
"river_buffer_distance": 30,
},
},
}
set_verbose(True) # Set verbosity
# Run SWMManywhere
outputs = swmmanywhere(config)
model_dir = outputs[0].parent
# Imports
from __future__ import annotations
import tempfile
from pathlib import Path
import folium
import geopandas as gpd
from swmmanywhere.logging import set_verbose
from swmmanywhere.swmmanywhere import swmmanywhere
from swmmanywhere.utilities import plot_map
# Create temporary directory
temp_dir = tempfile.TemporaryDirectory(dir=".")
base_dir = Path(temp_dir.name)
# Define minimum viable config
bbox = (1.52740, 42.50524, 1.54273, 42.51259)
config = {
"base_dir": base_dir,
"project": "my_first_swmm",
"bbox": bbox,
"run_settings": {"duration": 3600},
"parameter_overrides": {
"topology_derivation": {
"allowable_networks": ["drive"],
"omit_edges": ["bridge"],
},
"outfall_derivation": {
"outfall_length": 5,
"river_buffer_distance": 30,
},
},
}
set_verbose(True) # Set verbosity
# Run SWMManywhere
outputs = swmmanywhere(config)
model_dir = outputs[0].parent
/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
2025/05/22 10:43:06 | Creating project structure.
2025/05/22 10:43:06 | Project structure created at tmp9afa7292
2025/05/22 10:43:06 | Project name: my_first_swmm
2025/05/22 10:43:06 | Bounding box: (1.5274, 42.50524, 1.54273, 42.51259),
number: 1
2025/05/22 10:43:06 | Model number: 1
2025/05/22 10:43:06 | Loading and setting parameters.
2025/05/22 10:43:06 | Setting topology_derivation allowable_networks to ['drive']
2025/05/22 10:43:06 | Setting topology_derivation omit_edges to ['bridge']
2025/05/22 10:43:06 | Setting outfall_derivation outfall_length to 5
2025/05/22 10:43:06 | Setting outfall_derivation river_buffer_distance to 30
2025/05/22 10:43:06 | Allowable networks have been changed, removing old street graph.
2025/05/22 10:43:06 | Running downloads.
2025/05/22 10:43:06 | downloading elevation to tmp9afa7292/my_first_swmm/bbox_1/download/elevation.tif
2025/05/22 10:43:12 | downloading buildings to tmp9afa7292/my_first_swmm/bbox_1/download/building.geoparquet
2025/05/22 10:43:25 | downloading network to tmp9afa7292/my_first_swmm/bbox_1/download/street.parquet
2025/05/22 10:43:28 | downloading river network to tmp9afa7292/my_first_swmm/bbox_1/download/river.parquet
2025/05/22 10:43:30 | Iterating graphs.
2025/05/22 10:43:30 | Iterating graph functions.
2025/05/22 10:43:30 | graphfcn: assign_id completed.
2025/05/22 10:43:30 | graphfcn: fix_geometries completed.
2025/05/22 10:43:30 | graphfcn: remove_non_pipe_allowable_links completed.
/opt/hostedtoolcache/Python/3.11.12/x64/lib/python3.11/site-packages/networkx/readwrite/json_graph/node_link.py:287: FutureWarning: The default value will be changed to `edges="edges" in NetworkX 3.6. To make this warning go away, explicitly set the edges kwarg, e.g.: nx.node_link_graph(data, edges="links") to preserve current behavior, or nx.node_link_graph(data, edges="edges") for forward compatibility. warnings.warn(
2025/05/22 10:43:30 | graphfcn: calculate_streetcover completed.
2025/05/22 10:43:30 | graphfcn: remove_parallel_edges completed.
2025/05/22 10:43:30 | graphfcn: to_undirected completed.
2025/05/22 10:43:30 | graphfcn: split_long_edges completed.
2025/05/22 10:43:31 | graphfcn: merge_street_nodes completed.
2025/05/22 10:43:31 | graphfcn: assign_id completed.
Downloading WhiteboxTools from https://www.whiteboxgeo.com/WBT_Linux/WhiteboxTools_linux_amd64.zip
Extracted WhiteboxTools to tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56/WBT
Using existing WhiteboxTools executable: tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56/WBT/whitebox_tools
Starting WhiteboxTools session with source directory: /home/runner/work/SWMManywhere/SWMManywhere/tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56
Running WhiteboxTools version: 2.4.0
Command: tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56/WBT/whitebox_tools --run=BreachDepressions --wd=tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56 --compress_rasters=false --max_procs=1 -i=dem.tif --fillpits -o=dem_corr.tif
WhiteboxTools output:
******************************** * Welcome to BreachDepressions * * Powered by WhiteboxTools * * www.whiteboxgeo.com * ******************************** Reading data... Progress: 0% Progress: 1% Progress: 2% Progress: 3% Progress: 4% Progress: 5% Progress: 6% Progress: 7% Progress: 8% Progress: 9% Progress: 10% Progress: 11% Progress: 12% Progress: 13% Progress: 14% Progress: 15% Progress: 16% Progress: 17% Progress: 18% Progress: 19% Progress: 20% Progress: 21% Progress: 22% Progress: 23% Progress: 24% Progress: 25% Progress: 26% Progress: 27% Progress: 28% Progress: 29% Progress: 30% Progress: 31% Progress: 32% Progress: 33% Progress: 34% Progress: 35% Progress: 36% Progress: 37% Progress: 38% Progress: 39% Progress: 40% Progress: 41% Progress: 42% Progress: 43% Progress: 44% Progress: 45% Progress: 46% Progress: 47% Progress: 48% Progress: 49% Progress: 50% Progress: 51% Progress: 52% Progress: 53% Progress: 54% Progress: 55% Progress: 56% Progress: 57% Progress: 58% Progress: 59% Progress: 60% Progress: 61% Progress: 62% Progress: 63% Progress: 64% Progress: 65% Progress: 66% Progress: 67% Progress: 68% Progress: 69% Progress: 70% Progress: 71% Progress: 72% Progress: 73% Progress: 74% Progress: 75% Progress: 76% Progress: 77% Progress: 78% Progress: 79% Progress: 80% Progress: 81% Progress: 82% Progress: 83% Progress: 84% Progress: 85% Progress: 86% Progress: 87% Progress: 88% Progress: 89% Progress: 90% Progress: 91% Progress: 92% Progress: 93% Progress: 94% Progress: 95% Progress: 96% Progress: 97% Progress: 98% Progress: 99% Progress: 100% Progress: 101% Progress: 102% Progress: 103% Progress: 104% Progress: 105% Progress: 106% Progress: 107% Progress: 108% Progress: 109% Saving data... Output file written Elapsed Time (excluding I/O): 0.0s
Running WhiteboxTools version: 2.4.0
Command: tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56/WBT/whitebox_tools --run=D8Pointer --wd=tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56 --compress_rasters=false --max_procs=1 -i=dem_corr.tif -o=fdir.tif
WhiteboxTools output:
**************************** * Welcome to D8Pointer * * Powered by WhiteboxTools * * www.whiteboxgeo.com * **************************** Reading data... Progress: 0% Progress: 2% Progress: 5% Progress: 8% Progress: 11% Progress: 14% Progress: 17% Progress: 20% Progress: 23% Progress: 26% Progress: 29% Progress: 32% Progress: 35% Progress: 38% Progress: 41% Progress: 44% Progress: 47% Progress: 50% Progress: 52% Progress: 55% Progress: 58% Progress: 61% Progress: 64% Progress: 67% Progress: 70% Progress: 73% Progress: 76% Progress: 79% Progress: 82% Progress: 85% Progress: 88% Progress: 91% Progress: 94% Progress: 97% Progress: 100% Saving data... Output file written Elapsed Time (excluding I/O): 0.0s
Completed WhiteboxTools session with source directory: /home/runner/work/SWMManywhere/SWMManywhere/tmp9afa7292/my_first_swmm/bbox_1/download/tmpa23onw56
2025/05/22 10:43:35 | graphfcn: clip_to_catchments completed.
Extracted WhiteboxTools to tmpgrn1dxfv/tmpcqau1h1m/WBT
Using existing WhiteboxTools executable: tmpgrn1dxfv/tmpcqau1h1m/WBT/whitebox_tools
Starting WhiteboxTools session with source directory: /home/runner/work/SWMManywhere/SWMManywhere/tmpgrn1dxfv/tmpcqau1h1m
Running WhiteboxTools version: 2.4.0
Command: tmpgrn1dxfv/tmpcqau1h1m/WBT/whitebox_tools --run=BreachDepressions --wd=tmpgrn1dxfv/tmpcqau1h1m --compress_rasters=false --max_procs=1 -i=dem.tif --fillpits -o=dem_corr.tif
WhiteboxTools output:
******************************** * Welcome to BreachDepressions * * Powered by WhiteboxTools * * www.whiteboxgeo.com * ******************************** Reading data... Progress: 0% Progress: 1% Progress: 2% Progress: 3% Progress: 4% Progress: 5% Progress: 6% Progress: 7% Progress: 8% Progress: 9% Progress: 10% Progress: 11% Progress: 12% Progress: 13% Progress: 14% Progress: 15% Progress: 16% Progress: 17% Progress: 18% Progress: 19% Progress: 20% Progress: 21% Progress: 22% Progress: 23% Progress: 24% Progress: 25% Progress: 26% Progress: 27% Progress: 28% Progress: 29% Progress: 30% Progress: 31% Progress: 32% Progress: 33% Progress: 34% Progress: 35% Progress: 36% Progress: 37% Progress: 38% Progress: 39% Progress: 40% Progress: 41% Progress: 42% Progress: 43% Progress: 44% Progress: 45% Progress: 46% Progress: 47% Progress: 48% Progress: 49% Progress: 50% Progress: 51% Progress: 52% Progress: 53% Progress: 54% Progress: 55% Progress: 56% Progress: 57% Progress: 58% Progress: 59% Progress: 60% Progress: 61% Progress: 62% Progress: 63% Progress: 64% Progress: 65% Progress: 66% Progress: 67% Progress: 68% Progress: 69% Progress: 70% Progress: 71% Progress: 72% Progress: 73% Progress: 74% Progress: 75% Progress: 76% Progress: 77% Progress: 78% Progress: 79% Progress: 80% Progress: 81% Progress: 82% Progress: 83% Progress: 84% Progress: 85% Progress: 86% Progress: 87% Progress: 88% Progress: 89% Progress: 90% Progress: 91% Progress: 92% Progress: 93% Progress: 94% Progress: 95% Progress: 96% Progress: 97% Progress: 98% Progress: 99% Progress: 100% Progress: 101% Progress: 102% Progress: 103% Progress: 104% Progress: 105% Progress: 106% Progress: 107% Progress: 108% Progress: 109% Saving data... Output file written Elapsed Time (excluding I/O): 0.0s
Running WhiteboxTools version: 2.4.0
Command: tmpgrn1dxfv/tmpcqau1h1m/WBT/whitebox_tools --run=D8Pointer --wd=tmpgrn1dxfv/tmpcqau1h1m --compress_rasters=false --max_procs=1 -i=dem_corr.tif -o=fdir.tif
WhiteboxTools output:
**************************** * Welcome to D8Pointer * * Powered by WhiteboxTools * * www.whiteboxgeo.com * **************************** Reading data... Progress: 0% Progress: 2% Progress: 5% Progress: 8% Progress: 11% Progress: 14% Progress: 17% Progress: 20% Progress: 23% Progress: 26% Progress: 29% Progress: 32% Progress: 35% Progress: 38% Progress: 41% Progress: 44% Progress: 47% Progress: 50% Progress: 52% Progress: 55% Progress: 58% Progress: 61% Progress: 64% Progress: 67% Progress: 70% Progress: 73% Progress: 76% Progress: 79% Progress: 82% Progress: 85% Progress: 88% Progress: 91% Progress: 94% Progress: 97% Progress: 100% Saving data... Output file written Elapsed Time (excluding I/O): 0.0s
Completed WhiteboxTools session with source directory: /home/runner/work/SWMManywhere/SWMManywhere/tmpgrn1dxfv/tmpcqau1h1m
2025/05/22 10:43:36 | graphfcn: calculate_contributing_area completed.
2025/05/22 10:43:36 | graphfcn: set_elevation completed.
2025/05/22 10:43:37 | graphfcn: double_directed completed.
2025/05/22 10:43:37 | graphfcn: fix_geometries completed.
2025/05/22 10:43:37 | graphfcn: set_surface_slope completed.
2025/05/22 10:43:37 | graphfcn: set_chahinian_slope completed.
2025/05/22 10:43:37 | graphfcn: set_chahinian_angle completed.
2025/05/22 10:43:38 | graphfcn: calculate_weights completed.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
877, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
842, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
472, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
697, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
773, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
798, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
1278, using this node as outfall.
2025/05/22 10:43:38 | No outfalls found for subgraph containing
1583, using this node as outfall.
2025/05/22 10:43:38 | graphfcn: identify_outfalls completed.
2025/05/22 10:43:38 | Total graph weight 267.5752120322625.
2025/05/22 10:43:38 | graphfcn: derive_topology completed.
0%| | 0/397 [00:00<?, ?it/s]
10%|█ | 41/397 [00:00<00:00, 395.41it/s]
21%|██▏ | 85/397 [00:00<00:00, 412.59it/s]
32%|███▏ | 127/397 [00:00<00:00, 374.52it/s]
42%|████▏ | 165/397 [00:00<00:00, 314.91it/s]
50%|████▉ | 198/397 [00:00<00:00, 309.27it/s]
58%|█████▊ | 230/397 [00:00<00:00, 287.15it/s]
65%|██████▌ | 260/397 [00:00<00:00, 259.83it/s]
72%|███████▏ | 287/397 [00:00<00:00, 257.18it/s]
79%|███████▉ | 315/397 [00:01<00:00, 262.43it/s]
86%|████████▌ | 342/397 [00:01<00:00, 256.19it/s]
93%|█████████▎| 368/397 [00:01<00:00, 243.19it/s]
99%|█████████▉| 393/397 [00:01<00:00, 236.00it/s]
100%|██████████| 397/397 [00:01<00:00, 275.93it/s]
2025/05/22 10:43:39 | graphfcn: pipe_by_pipe completed.
2025/05/22 10:43:40 | graphfcn: fix_geometries completed.
2025/05/22 10:43:40 | graphfcn: assign_id completed.
2025/05/22 10:43:40 | Saving final graph and writing inp file.
Skipping field osmid: unsupported OGR type: 1
Skipping field highway: unsupported OGR type: 5
Skipping field lanes: unsupported OGR type: 5
Skipping field maxspeed: unsupported OGR type: 5
Skipping field name: unsupported OGR type: 5
Skipping field reversed: unsupported OGR type: 1
2025/05/22 10:43:40 | Running the synthetic model.
2025/05/22 10:43:40 | tmp9afa7292/my_first_swmm/bbox_1/model_1/model_1.inp initialised in pyswmm
2025/05/22 10:43:40 | Starting simulation for: tmp9afa7292/my_first_swmm/bbox_1/model_1/model_1.inp
0%| | 0/3600 [00:00<?, ?it/s]
13%|█▎ | 485.0/3600 [00:00<00:00, 4849.32it/s]
27%|██▋ | 970.0/3600 [00:00<00:01, 2247.90it/s]
35%|███▌ | 1266.0/3600 [00:00<00:01, 2135.02it/s]
44%|████▎ | 1572.0/3600 [00:00<00:00, 2372.47it/s]
57%|█████▋ | 2045.0/3600 [00:00<00:00, 2985.52it/s]
81%|████████▏ | 2930.0/3600 [00:00<00:00, 4605.11it/s]
2025/05/22 10:43:41 | Model run complete.
3605.0it [00:00, 3796.40it/s]
2025/05/22 10:43:41 | Writing synthetic results.
2025/05/22 10:43:41 | No real network provided, returning SWMM .inp file.
Plotting output¶
Now we can plot the output. To highlight the differences in the supplied data that we are about to demonstrate, we also plot the subbasins.
In [2]:
Copied!
m = plot_map(model_dir)
subbasins = gpd.read_file(model_dir / "subbasins.geoparquet")
folium.GeoJson(subbasins, fill_opacity=0, color="blue", weight=2).add_to(m)
m
m = plot_map(model_dir)
subbasins = gpd.read_file(model_dir / "subbasins.geoparquet")
folium.GeoJson(subbasins, fill_opacity=0, color="blue", weight=2).add_to(m)
m
Skipping field osmid: unsupported OGR type: 1
Skipping field highway: unsupported OGR type: 5
Skipping field lanes: unsupported OGR type: 5
Skipping field maxspeed: unsupported OGR type: 5
Skipping field name: unsupported OGR type: 5
Skipping field reversed: unsupported OGR type: 1
Out[2]:
Make this Notebook Trusted to load map: File -> Trust Notebook