Create histoire.py
Browse files- pages/histoire.py +75 -0
pages/histoire.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import folium
|
3 |
+
from streamlit_folium import folium_static
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
from datetime import datetime, timedelta
|
8 |
+
import imageio
|
9 |
+
|
10 |
+
# Constants
|
11 |
+
WMS_URL = "https://wms.geo.admin.ch/"
|
12 |
+
LAYER_NAME = "ch.swisstopo.zeitreihen"
|
13 |
+
|
14 |
+
def get_wms_image(bbox, width, height, time):
|
15 |
+
params = {
|
16 |
+
'SERVICE': 'WMS',
|
17 |
+
'VERSION': '1.3.0',
|
18 |
+
'REQUEST': 'GetMap',
|
19 |
+
'LAYERS': LAYER_NAME,
|
20 |
+
'STYLES': '',
|
21 |
+
'CRS': 'EPSG:4326',
|
22 |
+
'BBOX': ','.join(map(str, bbox)),
|
23 |
+
'WIDTH': width,
|
24 |
+
'HEIGHT': height,
|
25 |
+
'FORMAT': 'image/png',
|
26 |
+
'TIME': time.strftime('%Y-%m-%d')
|
27 |
+
}
|
28 |
+
response = requests.get(WMS_URL, params=params)
|
29 |
+
return Image.open(io.BytesIO(response.content))
|
30 |
+
|
31 |
+
def create_animation(bbox, width, height, start_date, end_date, step):
|
32 |
+
images = []
|
33 |
+
current_date = start_date
|
34 |
+
while current_date <= end_date:
|
35 |
+
img = get_wms_image(bbox, width, height, current_date)
|
36 |
+
images.append(img)
|
37 |
+
current_date += step
|
38 |
+
return images
|
39 |
+
|
40 |
+
# Streamlit app
|
41 |
+
st.title("Swisstopo Historical Animation Generator")
|
42 |
+
|
43 |
+
# Sidebar for input parameters
|
44 |
+
st.sidebar.header("Input Parameters")
|
45 |
+
start_year = st.sidebar.number_input("Start Year", min_value=1850, max_value=2022, value=1900)
|
46 |
+
end_year = st.sidebar.number_input("End Year", min_value=1850, max_value=2022, value=2022)
|
47 |
+
step_years = st.sidebar.number_input("Step (years)", min_value=1, max_value=50, value=10)
|
48 |
+
|
49 |
+
# Main content
|
50 |
+
st.subheader("Select Area of Interest")
|
51 |
+
m = folium.Map(location=[46.8, 8.2], zoom_start=8)
|
52 |
+
folium_static(m)
|
53 |
+
|
54 |
+
if st.button("Generate Animation"):
|
55 |
+
# Get bounding box from the map
|
56 |
+
bounds = m.get_bounds()
|
57 |
+
bbox = [bounds['_southWest']['lng'], bounds['_southWest']['lat'],
|
58 |
+
bounds['_northEast']['lng'], bounds['_northEast']['lat']]
|
59 |
+
|
60 |
+
width, height = 800, 600 # Image dimensions
|
61 |
+
start_date = datetime(start_year, 1, 1)
|
62 |
+
end_date = datetime(end_year, 12, 31)
|
63 |
+
step = timedelta(days=step_years * 365)
|
64 |
+
|
65 |
+
with st.spinner("Generating animation..."):
|
66 |
+
images = create_animation(bbox, width, height, start_date, end_date, step)
|
67 |
+
|
68 |
+
# Save as GIF
|
69 |
+
output_file = "historical_animation.gif"
|
70 |
+
imageio.mimsave(output_file, images, duration=1) # 1 second per frame
|
71 |
+
|
72 |
+
st.success(f"Animation generated and saved as {output_file}")
|
73 |
+
st.image(output_file)
|
74 |
+
|
75 |
+
st.sidebar.info("This application generates historical animations using Swisstopo's WMS. Select the area of interest on the map, set the time range and step, then click 'Generate Animation'.")
|