Spaces:
Sleeping
Sleeping
Update pages/v2.py
Browse files- pages/v2.py +22 -13
pages/v2.py
CHANGED
|
@@ -18,7 +18,24 @@ from concurrent.futures import ThreadPoolExecutor
|
|
| 18 |
from functools import lru_cache
|
| 19 |
import numpy as np
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@st.cache_data
|
| 23 |
def uploaded_file_to_gdf(data):
|
| 24 |
import tempfile
|
|
@@ -39,7 +56,6 @@ def uploaded_file_to_gdf(data):
|
|
| 39 |
|
| 40 |
return gdf
|
| 41 |
|
| 42 |
-
# Fonction pour construire l'URL de requête WMS
|
| 43 |
@lru_cache(maxsize=128)
|
| 44 |
def get_wms_url(bbox, width, height, time):
|
| 45 |
url = "https://wms.geo.admin.ch/"
|
|
@@ -59,7 +75,6 @@ def get_wms_url(bbox, width, height, time):
|
|
| 59 |
}
|
| 60 |
return url + "?" + "&".join(f"{k}={v}" for k, v in params.items())
|
| 61 |
|
| 62 |
-
# Fonction pour ajouter une date à l'image
|
| 63 |
def add_date_to_image(image, date):
|
| 64 |
draw = ImageDraw.Draw(image)
|
| 65 |
font = ImageFont.load_default()
|
|
@@ -76,7 +91,6 @@ def add_date_to_image(image, date):
|
|
| 76 |
draw.text((x, y), text, font=font, fill="white")
|
| 77 |
return image
|
| 78 |
|
| 79 |
-
# Fonction asynchrone pour télécharger une image
|
| 80 |
async def fetch_image(session, url, date, semaphore):
|
| 81 |
async with semaphore:
|
| 82 |
try:
|
|
@@ -89,14 +103,12 @@ async def fetch_image(session, url, date, semaphore):
|
|
| 89 |
print(f"Erreur lors de la récupération de l'image pour la date {date}: {str(e)}")
|
| 90 |
return None
|
| 91 |
|
| 92 |
-
# Téléchargement des images en fonction des années disponibles
|
| 93 |
async def download_images(bbox, width, height, available_years):
|
| 94 |
-
semaphore = asyncio.Semaphore(20)
|
| 95 |
async with aiohttp.ClientSession() as session:
|
| 96 |
tasks = [fetch_image(session, get_wms_url(bbox, width, height, date), date, semaphore) for date in available_years]
|
| 97 |
return await asyncio.gather(*tasks)
|
| 98 |
|
| 99 |
-
# Traitement des images et génération des fichiers ZIP par lots
|
| 100 |
def process_images_stream(images, format_option, speed, temp_dir, batch_size=20):
|
| 101 |
results = {}
|
| 102 |
|
|
@@ -134,7 +146,6 @@ def process_images_stream(images, format_option, speed, temp_dir, batch_size=20)
|
|
| 134 |
|
| 135 |
return results
|
| 136 |
|
| 137 |
-
# Générer un lien de téléchargement pour les fichiers ZIP
|
| 138 |
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
| 139 |
with open(bin_file, 'rb') as f:
|
| 140 |
data = f.read()
|
|
@@ -142,7 +153,6 @@ def get_binary_file_downloader_html(bin_file, file_label='File'):
|
|
| 142 |
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Télécharger {file_label}</a>'
|
| 143 |
return href
|
| 144 |
|
| 145 |
-
# Application Streamlit principale
|
| 146 |
def app():
|
| 147 |
st.title("Générateur de Timelapse Historique Suisse")
|
| 148 |
|
|
@@ -171,10 +181,9 @@ def app():
|
|
| 171 |
)
|
| 172 |
|
| 173 |
with st.form("submit_form"):
|
| 174 |
-
start_year = st.selectbox("Sélectionnez l'année de début:", [
|
| 175 |
-
end_year = st.selectbox("Sélectionnez l'année de fin:", [
|
| 176 |
|
| 177 |
-
# Options de taille d'image
|
| 178 |
size_choice = st.selectbox("Choisissez la taille de l'image:", ["HD (720p)", "Full HD (1080p)"])
|
| 179 |
width, height = (1280, 720) if size_choice == "HD (720p)" else (1920, 1080)
|
| 180 |
|
|
@@ -187,7 +196,7 @@ def app():
|
|
| 187 |
if data:
|
| 188 |
gdf = uploaded_file_to_gdf(data)
|
| 189 |
bbox = tuple(gdf.to_crs(epsg=2056).total_bounds)
|
| 190 |
-
available_years = [
|
| 191 |
|
| 192 |
images = asyncio.run(download_images(bbox, width, height, available_years))
|
| 193 |
|
|
|
|
| 18 |
from functools import lru_cache
|
| 19 |
import numpy as np
|
| 20 |
|
| 21 |
+
# Liste complète des dates disponibles
|
| 22 |
+
AVAILABLE_DATES = [
|
| 23 |
+
18641231, 18701231, 18801231, 18901231, 18941231, 18951231, 18961231, 18971231, 18981231, 18991231,
|
| 24 |
+
19001231, 19011231, 19021231, 19031231, 19041231, 19051231, 19061231, 19071231, 19081231, 19091231,
|
| 25 |
+
19101231, 19111231, 19121231, 19131231, 19141231, 19151231, 19161231, 19171231, 19181231, 19191231,
|
| 26 |
+
19201231, 19211231, 19221231, 19231231, 19241231, 19251231, 19261231, 19271231, 19281231, 19291231,
|
| 27 |
+
19301231, 19311231, 19321231, 19331231, 19341231, 19351231, 19361231, 19371231, 19381231, 19391231,
|
| 28 |
+
19401231, 19411231, 19421231, 19431231, 19441231, 19451231, 19461231, 19471231, 19481231, 19491231,
|
| 29 |
+
19501231, 19511231, 19521231, 19531231, 19541231, 19551231, 19561231, 19571231, 19581231, 19591231,
|
| 30 |
+
19601231, 19611231, 19621231, 19631231, 19641231, 19651231, 19661231, 19671231, 19681231, 19691231,
|
| 31 |
+
19701231, 19711231, 19721231, 19731231, 19741231, 19751231, 19761231, 19771231, 19781231, 19791231,
|
| 32 |
+
19801231, 19811231, 19821231, 19831231, 19841231, 19851231, 19861231, 19871231, 19881231, 19891231,
|
| 33 |
+
19901231, 19911231, 19921231, 19931231, 19941231, 19951231, 19961231, 19971231, 19981231, 19991231,
|
| 34 |
+
20001231, 20011231, 20021231, 20031231, 20041231, 20051231, 20061231, 20071231, 20081231, 20091231,
|
| 35 |
+
20101231, 20111231, 20121231, 20131231, 20141231, 20151231, 20161231, 20171231, 20181231, 20191231,
|
| 36 |
+
20201231, 20211231
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
@st.cache_data
|
| 40 |
def uploaded_file_to_gdf(data):
|
| 41 |
import tempfile
|
|
|
|
| 56 |
|
| 57 |
return gdf
|
| 58 |
|
|
|
|
| 59 |
@lru_cache(maxsize=128)
|
| 60 |
def get_wms_url(bbox, width, height, time):
|
| 61 |
url = "https://wms.geo.admin.ch/"
|
|
|
|
| 75 |
}
|
| 76 |
return url + "?" + "&".join(f"{k}={v}" for k, v in params.items())
|
| 77 |
|
|
|
|
| 78 |
def add_date_to_image(image, date):
|
| 79 |
draw = ImageDraw.Draw(image)
|
| 80 |
font = ImageFont.load_default()
|
|
|
|
| 91 |
draw.text((x, y), text, font=font, fill="white")
|
| 92 |
return image
|
| 93 |
|
|
|
|
| 94 |
async def fetch_image(session, url, date, semaphore):
|
| 95 |
async with semaphore:
|
| 96 |
try:
|
|
|
|
| 103 |
print(f"Erreur lors de la récupération de l'image pour la date {date}: {str(e)}")
|
| 104 |
return None
|
| 105 |
|
|
|
|
| 106 |
async def download_images(bbox, width, height, available_years):
|
| 107 |
+
semaphore = asyncio.Semaphore(20)
|
| 108 |
async with aiohttp.ClientSession() as session:
|
| 109 |
tasks = [fetch_image(session, get_wms_url(bbox, width, height, date), date, semaphore) for date in available_years]
|
| 110 |
return await asyncio.gather(*tasks)
|
| 111 |
|
|
|
|
| 112 |
def process_images_stream(images, format_option, speed, temp_dir, batch_size=20):
|
| 113 |
results = {}
|
| 114 |
|
|
|
|
| 146 |
|
| 147 |
return results
|
| 148 |
|
|
|
|
| 149 |
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
| 150 |
with open(bin_file, 'rb') as f:
|
| 151 |
data = f.read()
|
|
|
|
| 153 |
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Télécharger {file_label}</a>'
|
| 154 |
return href
|
| 155 |
|
|
|
|
| 156 |
def app():
|
| 157 |
st.title("Générateur de Timelapse Historique Suisse")
|
| 158 |
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
with st.form("submit_form"):
|
| 184 |
+
start_year = st.selectbox("Sélectionnez l'année de début:", [date // 10000 for date in AVAILABLE_DATES])
|
| 185 |
+
end_year = st.selectbox("Sélectionnez l'année de fin:", [date // 10000 for date in AVAILABLE_DATES], index=len(AVAILABLE_DATES) - 1)
|
| 186 |
|
|
|
|
| 187 |
size_choice = st.selectbox("Choisissez la taille de l'image:", ["HD (720p)", "Full HD (1080p)"])
|
| 188 |
width, height = (1280, 720) if size_choice == "HD (720p)" else (1920, 1080)
|
| 189 |
|
|
|
|
| 196 |
if data:
|
| 197 |
gdf = uploaded_file_to_gdf(data)
|
| 198 |
bbox = tuple(gdf.to_crs(epsg=2056).total_bounds)
|
| 199 |
+
available_years = [date for date in AVAILABLE_DATES if start_year <= date // 10000 <= end_year]
|
| 200 |
|
| 201 |
images = asyncio.run(download_images(bbox, width, height, available_years))
|
| 202 |
|