Spaces:
Running
Running
Update src/modules/map_dashboard.py
Browse files- src/modules/map_dashboard.py +51 -19
src/modules/map_dashboard.py
CHANGED
|
@@ -4,6 +4,7 @@ import pydeck as pdk # <-- Cette ligne doit être présente
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from geopy.exc import GeocoderTimedOut
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
# Ajoutez ceci juste après vos imports, avant les constantes
|
|
@@ -36,40 +37,71 @@ COLOR_GREEN_ZONE = "#54BD4B" # Vert Cluster
|
|
| 36 |
COLOR_YELLOW_LINE = "#FFD700" # Lignes de distance
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
# --- 1. FONCTIONS DE GÉOCODAGE (AVEC CACHE) ---
|
| 40 |
-
@st.cache_data(show_spinner=False)
|
| 41 |
def geocode_addresses(df_clients):
|
| 42 |
-
"""
|
| 43 |
|
| 44 |
if 'Adresse' not in df_clients.columns or 'ID_Client' not in df_clients.columns:
|
| 45 |
return pd.DataFrame()
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
geocoded_data = []
|
| 49 |
-
|
| 50 |
for index, row in df_clients.iterrows():
|
| 51 |
-
address = row['Adresse']
|
| 52 |
client_id = row['ID_Client']
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
if
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
return pd.DataFrame(geocoded_data)
|
| 72 |
|
|
|
|
| 73 |
|
| 74 |
# --- 2. FONCTION PRINCIPALE D'AFFICHAGE ---
|
| 75 |
def show_map_dashboard(client, sheet_name):
|
|
|
|
| 4 |
from geopy.geocoders import Nominatim
|
| 5 |
from geopy.exc import GeocoderTimedOut
|
| 6 |
import time
|
| 7 |
+
import requests
|
| 8 |
|
| 9 |
|
| 10 |
# Ajoutez ceci juste après vos imports, avant les constantes
|
|
|
|
| 37 |
COLOR_YELLOW_LINE = "#FFD700" # Lignes de distance
|
| 38 |
|
| 39 |
|
| 40 |
+
|
| 41 |
# --- 1. FONCTIONS DE GÉOCODAGE (AVEC CACHE) ---
|
| 42 |
+
@st.cache_data(show_spinner=False, ttl=3600)
|
| 43 |
def geocode_addresses(df_clients):
|
| 44 |
+
"""Géocodage via Mapbox API - Meilleure couverture mondiale"""
|
| 45 |
|
| 46 |
if 'Adresse' not in df_clients.columns or 'ID_Client' not in df_clients.columns:
|
| 47 |
return pd.DataFrame()
|
| 48 |
|
| 49 |
+
# METTEZ VOTRE TOKEN MAPBOX ICI
|
| 50 |
+
MAPBOX_TOKEN = "VOTRE_TOKEN_MAPBOX" # Le même que vous avez utilisé pour la carte
|
| 51 |
+
|
| 52 |
geocoded_data = []
|
| 53 |
+
|
| 54 |
for index, row in df_clients.iterrows():
|
| 55 |
+
address = str(row['Adresse']).strip()
|
| 56 |
client_id = row['ID_Client']
|
| 57 |
|
| 58 |
try:
|
| 59 |
+
# Encoder l'adresse pour l'URL
|
| 60 |
+
import urllib.parse
|
| 61 |
+
encoded_address = urllib.parse.quote(address)
|
| 62 |
+
|
| 63 |
+
# Appel API Mapbox Geocoding
|
| 64 |
+
url = f"https://api.mapbox.com/geocoding/v5/mapbox.places/{encoded_address}.json"
|
| 65 |
+
params = {
|
| 66 |
+
'access_token': MAPBOX_TOKEN,
|
| 67 |
+
'limit': 1,
|
| 68 |
+
'language': 'fr'
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
import requests
|
| 72 |
+
response = requests.get(url, params=params, timeout=10)
|
| 73 |
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
data = response.json()
|
| 76 |
+
|
| 77 |
+
if data['features']:
|
| 78 |
+
coords = data['features'][0]['geometry']['coordinates']
|
| 79 |
+
lon, lat = coords[0], coords[1]
|
| 80 |
+
place_name = data['features'][0]['place_name']
|
| 81 |
+
|
| 82 |
+
geocoded_data.append({
|
| 83 |
+
"ID": client_id,
|
| 84 |
+
"Nom": row.get('Nom_Complet', 'Client Inconnu'),
|
| 85 |
+
"Adresse": address,
|
| 86 |
+
"lat": lat,
|
| 87 |
+
"lon": lon,
|
| 88 |
+
"Revenus": row.get('Revenus_Mensuels', 0)
|
| 89 |
+
})
|
| 90 |
+
|
| 91 |
+
st.success(f"✅ {client_id}: {place_name}")
|
| 92 |
+
else:
|
| 93 |
+
st.error(f"❌ {client_id}: Adresse introuvable - '{address}'")
|
| 94 |
+
else:
|
| 95 |
+
st.error(f"❌ {client_id}: Erreur API {response.status_code}")
|
| 96 |
+
|
| 97 |
+
time.sleep(0.3) # Rate limit respectueux
|
| 98 |
|
| 99 |
+
except Exception as e:
|
| 100 |
+
st.error(f"❌ {client_id}: Erreur - {str(e)}")
|
| 101 |
+
|
| 102 |
return pd.DataFrame(geocoded_data)
|
| 103 |
|
| 104 |
+
|
| 105 |
|
| 106 |
# --- 2. FONCTION PRINCIPALE D'AFFICHAGE ---
|
| 107 |
def show_map_dashboard(client, sheet_name):
|