Upload 4 files
Browse files- .gitignore +11 -0
- README.md +50 -14
- app.py +59 -0
.gitignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
ENV/
|
| 9 |
+
.ipynb_checkpoints
|
| 10 |
+
*.DS_Store
|
| 11 |
+
*.zip
|
README.md
CHANGED
|
@@ -1,20 +1,56 @@
|
|
| 1 |
---
|
| 2 |
-
title: WNTR GIS
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
| 7 |
-
|
| 8 |
-
tags:
|
| 9 |
-
- streamlit
|
| 10 |
pinned: false
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
forums](https://discuss.streamlit.io).
|
|
|
|
| 1 |
---
|
| 2 |
+
title: WNTR GIS Streamlit
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
app_file: app.py
|
|
|
|
|
|
|
| 8 |
pinned: false
|
| 9 |
+
tags:
|
| 10 |
+
- water
|
| 11 |
+
- wntr
|
| 12 |
+
- epanet
|
| 13 |
+
- shapefile
|
| 14 |
+
- gis
|
| 15 |
+
- streamlit
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# WNTR GIS Streamlit App π
|
| 19 |
+
|
| 20 |
+
Simulate water networks with EPANET `.inp` files or GIS shapefiles using WNTR and visualize results interactively.
|
| 21 |
+
|
| 22 |
+
# π°οΈ WNTR GIS Streamlit App
|
| 23 |
+
|
| 24 |
+
This app allows you to:
|
| 25 |
+
- Upload EPANET `.inp` files and visualize their network structure
|
| 26 |
+
- Convert `.inp` to GIS layers (`GeoDataFrames`)
|
| 27 |
+
- Upload zipped shapefiles (`.shp`) for nodes and links, and simulate them with WNTR
|
| 28 |
+
- Run hydraulic simulations and view pressure results
|
| 29 |
+
|
| 30 |
+
## π How to Use
|
| 31 |
+
|
| 32 |
+
1. Upload an `.inp` file or zipped shapefiles
|
| 33 |
+
2. View the network geometry on a map
|
| 34 |
+
3. If shapefiles are used, WNTR reconstructs the model from GeoDataFrames
|
| 35 |
+
4. Press "Run Simulation" to get results
|
| 36 |
|
| 37 |
+
## π¦ Dependencies
|
| 38 |
+
|
| 39 |
+
See `requirements.txt`. This app uses:
|
| 40 |
+
- `streamlit`
|
| 41 |
+
- `wntr`
|
| 42 |
+
- `geopandas`
|
| 43 |
+
- `matplotlib`
|
| 44 |
+
|
| 45 |
+
## π οΈ Deployment
|
| 46 |
+
|
| 47 |
+
This app is ready for Hugging Face Spaces using the **Streamlit** template.
|
| 48 |
+
|
| 49 |
+
Simply upload:
|
| 50 |
+
- `app.py`
|
| 51 |
+
- `requirements.txt`
|
| 52 |
+
- `README.md`
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
|
| 56 |
+
*Created by Raza Ali β Civil Engineer with focus on water systems modeling.*
|
|
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import wntr
|
| 4 |
+
import geopandas as gpd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import zipfile
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
st.set_page_config(layout="wide")
|
| 12 |
+
st.title("π WNTR GIS Integration App")
|
| 13 |
+
|
| 14 |
+
uploaded_file = st.file_uploader("Upload EPANET .inp file or zipped shapefiles (.zip)", type=["inp", "zip"])
|
| 15 |
+
|
| 16 |
+
if uploaded_file:
|
| 17 |
+
temp_dir = tempfile.mkdtemp()
|
| 18 |
+
filetype = uploaded_file.name.split('.')[-1].lower()
|
| 19 |
+
|
| 20 |
+
if filetype == 'inp':
|
| 21 |
+
# Load WNTR model
|
| 22 |
+
with open(os.path.join(temp_dir, uploaded_file.name), "wb") as f:
|
| 23 |
+
f.write(uploaded_file.read())
|
| 24 |
+
wn = wntr.network.WaterNetworkModel(os.path.join(temp_dir, uploaded_file.name))
|
| 25 |
+
node_gdf, link_gdf = wn.to_gis()
|
| 26 |
+
|
| 27 |
+
st.subheader("π§© Network Geometry from INP")
|
| 28 |
+
st.map(node_gdf.set_geometry("geometry").to_crs(epsg=4326))
|
| 29 |
+
|
| 30 |
+
st.download_button("Download Nodes (GeoJSON)", node_gdf.to_json(), file_name="nodes.geojson")
|
| 31 |
+
st.download_button("Download Links (GeoJSON)", link_gdf.to_json(), file_name="links.geojson")
|
| 32 |
+
|
| 33 |
+
elif filetype == 'zip':
|
| 34 |
+
# Extract shapefiles and load into GeoDataFrames
|
| 35 |
+
zip_path = os.path.join(temp_dir, uploaded_file.name)
|
| 36 |
+
with open(zip_path, "wb") as f:
|
| 37 |
+
f.write(uploaded_file.read())
|
| 38 |
+
|
| 39 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 40 |
+
zip_ref.extractall(temp_dir)
|
| 41 |
+
|
| 42 |
+
shapefiles = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if f.endswith('.shp')]
|
| 43 |
+
if len(shapefiles) != 2:
|
| 44 |
+
st.error("Please provide exactly two shapefiles: one for nodes and one for links.")
|
| 45 |
+
else:
|
| 46 |
+
node_gdf = gpd.read_file(shapefiles[0])
|
| 47 |
+
link_gdf = gpd.read_file(shapefiles[1])
|
| 48 |
+
wn = wntr.network.WaterNetworkModel.from_gis(node_gdf, link_gdf)
|
| 49 |
+
|
| 50 |
+
st.subheader("π₯ Network Reconstructed from Shapefiles")
|
| 51 |
+
st.map(node_gdf.set_geometry("geometry").to_crs(epsg=4326))
|
| 52 |
+
|
| 53 |
+
st.success("Successfully converted shapefiles to WNTR model.")
|
| 54 |
+
if st.button("Run Simulation"):
|
| 55 |
+
sim = wntr.sim.EpanetSimulator(wn)
|
| 56 |
+
results = sim.run_sim()
|
| 57 |
+
pressure = results.node["pressure"]
|
| 58 |
+
st.write("π§ͺ Sample Pressure Results")
|
| 59 |
+
st.dataframe(pressure.head())
|