Spaces:
Sleeping
Sleeping
import streamlit as st | |
import geopandas as gpd | |
from shapely.geometry import Point, Polygon | |
import folium | |
from streamlit_folium import st_folium | |
# Fonction pour calculer les informations et le polygone autour du point cliqué | |
def getinfo(lat, lon): | |
# Exemple de calcul pour l'attribut en fonction de la position | |
info = f"Attribut calculé pour la position ({lat}, {lon})" | |
# Création d'un polygone autour du point cliqué | |
polygon = Polygon([ | |
(lon - 0.01, lat - 0.01), | |
(lon + 0.01, lat - 0.01), | |
(lon + 0.01, lat + 0.01), | |
(lon - 0.01, lat + 0.01), | |
(lon - 0.01, lat - 0.01) | |
]) | |
# Création du GeoDataFrame | |
gdf = gpd.GeoDataFrame({ | |
'info': [info], | |
'geometry': [polygon] | |
}) | |
return gdf | |
# Initialisation de l'application Streamlit | |
st.set_page_config(layout="wide") | |
# Sidebar pour les informations | |
st.sidebar.title("Informations sur le point") | |
point_info = st.sidebar.empty() | |
# Création de la carte avec Folium | |
map_center = [48.8566, 2.3522] | |
m = folium.Map(location=map_center, zoom_start=13) | |
# Ajout d'un callback sur les clics de la carte | |
m.add_child(folium.LatLngPopup()) | |
# Affichage de la carte | |
st_map = st_folium(m, width=700, height=500) | |
# Fonction pour ajouter un marqueur et un polygone sur la carte | |
def add_marker_and_polygon(lat, lon): | |
gdf = getinfo(lat, lon) | |
info = gdf['info'].iloc[0] | |
polygon_coords = list(gdf['geometry'].iloc[0].exterior.coords) | |
# Ajouter le marqueur | |
folium.Marker([lat, lon], tooltip=info).add_to(m) | |
# Ajouter le polygone | |
folium.Polygon(polygon_coords, color="blue", fill=True, fill_opacity=0.5).add_to(m) | |
# Afficher les informations dans la sidebar | |
point_info.markdown(f""" | |
**Coordonnées** | |
Latitude: {lat:.5f} | |
Longitude: {lon:.5f} | |
**Information** | |
{info} | |
""") | |
# Si un clic est détecté, mettre à jour la carte et les informations | |
if st_map and 'last_clicked' in st_map and st_map['last_clicked']: | |
lat = st_map['last_clicked']['lat'] | |
lon = st_map['last_clicked']['lng'] | |
add_marker_and_polygon(lat, lon) | |
# Afficher la carte mise à jour | |
st_map = st_folium(m, width=700, height=500) | |