-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_app2.py
62 lines (51 loc) · 2.67 KB
/
archive_app2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import xml.etree.ElementTree as ET
from datetime import datetime
import streamlit as st
import io
import zipfile
# Set the language to French
st.set_page_config(page_title="Découpe horaire de fichiers Mothy", layout="wide", initial_sidebar_state="expanded")
# Add a title to the app
st.title("Découpe horaire de fichiers Mothy")
# Add a logo to the app
st.image("logo.png", width=200)
# Add a file uploader widget to allow the user to upload the GPX file
uploaded_file = st.file_uploader("Insérez le fichier gpx crée par Mothy pour le découper heure par heure", type="gpx")
if uploaded_file is not None:
# Load the GPX file
tree = ET.parse(uploaded_file)
root = tree.getroot()
# Create a dictionary to store the waypoints by date and time
waypoints_by_date = {}
# Iterate over the waypoints
for wpt in root.findall('.//wpt'):
time_str = wpt.find('time').text
dt = datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%SZ')
date_str = dt.strftime('%Y-%m-%d')
time_str = dt.strftime('%H')
if date_str not in waypoints_by_date:
waypoints_by_date[date_str] = {}
if time_str not in waypoints_by_date[date_str]:
waypoints_by_date[date_str][time_str] = []
waypoints_by_date[date_str][time_str].append(wpt)
# Create a ZIP archive to store the GPX files
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, mode='w', compression=zipfile.ZIP_DEFLATED) as zipf:
# Create a separate GPX file for each date and time
for date_str, time_dict in waypoints_by_date.items():
for time_str, waypoints in time_dict.items():
filename = f'{date_str}_{time_str}h.gpx'
with open(filename, 'w') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write('<gpx version="1.1">\n')
f.write(f' <metadata><name>{date_str} {time_str}h</name></metadata>\n')
for wpt in waypoints:
f.write(ET.tostring(wpt, encoding='unicode').strip() + '\n')
f.write('</gpx>\n')
# Add the file to the ZIP archive
with open(filename, 'rb') as f:
zipf.writestr(filename, f.read())
# Display a download button for the ZIP archive
st.download_button(label="Téléchargement de tous les fichiers GPX en .zip", data=zip_buffer.getvalue(), file_name="decoupage_mothy.zip")
# Display a message to indicate that the files have been created
st.success("Découpage effectué avec succès, vous pouvez télécharger l'ensemble des fichiers. N'oubliez pas de dézipper pour faire glisser dans un SIG.")