import streamlit as st import folium from streamlit_folium import folium_static import requests from PIL import Image import io from datetime import datetime, timedelta import imageio # Constants WMS_URL = "https://wms.geo.admin.ch/" LAYER_NAME = "ch.swisstopo.zeitreihen" def get_wms_image(bbox, width, height, time): params = { 'SERVICE': 'WMS', 'VERSION': '1.3.0', 'REQUEST': 'GetMap', 'LAYERS': LAYER_NAME, 'STYLES': '', 'CRS': 'EPSG:4326', 'BBOX': ','.join(map(str, bbox)), 'WIDTH': width, 'HEIGHT': height, 'FORMAT': 'image/png', 'TIME': time.strftime('%Y-%m-%d') } response = requests.get(WMS_URL, params=params) return Image.open(io.BytesIO(response.content)) def create_animation(bbox, width, height, start_date, end_date, step): images = [] current_date = start_date while current_date <= end_date: img = get_wms_image(bbox, width, height, current_date) images.append(img) current_date += step return images # Streamlit app st.title("Swisstopo Historical Animation Generator") # Sidebar for input parameters st.sidebar.header("Input Parameters") start_year = st.sidebar.number_input("Start Year", min_value=1850, max_value=2022, value=1900) end_year = st.sidebar.number_input("End Year", min_value=1850, max_value=2022, value=2022) step_years = st.sidebar.number_input("Step (years)", min_value=1, max_value=50, value=10) # Main content st.subheader("Select Area of Interest") m = folium.Map(location=[46.8, 8.2], zoom_start=8) folium_static(m) if st.button("Generate Animation"): # Get bounding box from the map bounds = m.get_bounds() bbox = [bounds['_southWest']['lng'], bounds['_southWest']['lat'], bounds['_northEast']['lng'], bounds['_northEast']['lat']] width, height = 800, 600 # Image dimensions start_date = datetime(start_year, 1, 1) end_date = datetime(end_year, 12, 31) step = timedelta(days=step_years * 365) with st.spinner("Generating animation..."): images = create_animation(bbox, width, height, start_date, end_date, step) # Save as GIF output_file = "historical_animation.gif" imageio.mimsave(output_file, images, duration=1) # 1 second per frame st.success(f"Animation generated and saved as {output_file}") st.image(output_file) 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'.")