Spaces:
Running
Running
Update src/modules/map_dashboard.py
Browse files- src/modules/map_dashboard.py +28 -4
src/modules/map_dashboard.py
CHANGED
|
@@ -49,6 +49,9 @@ def geocode_addresses(df_clients):
|
|
| 49 |
# METTEZ VOTRE TOKEN MAPBOX ICI
|
| 50 |
MAPBOX_TOKEN = "pk.eyJ1Ijoia2x5ZGUwNyIsImEiOiJjbWpwd3B4cWMwYTVqM2ZxeHcwM3FoMHF5In0.mgiTZbug_4eD3cSNgF2t5Q" # Le même que vous avez utilisé pour la carte
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
geocoded_data = []
|
| 53 |
|
| 54 |
for index, row in df_clients.iterrows():
|
|
@@ -65,7 +68,10 @@ def geocode_addresses(df_clients):
|
|
| 65 |
params = {
|
| 66 |
'access_token': MAPBOX_TOKEN,
|
| 67 |
'limit': 1,
|
| 68 |
-
'language': 'fr'
|
|
|
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
import requests
|
|
@@ -75,10 +81,24 @@ def geocode_addresses(df_clients):
|
|
| 75 |
data = response.json()
|
| 76 |
|
| 77 |
if data['features']:
|
| 78 |
-
|
|
|
|
| 79 |
lon, lat = coords[0], coords[1]
|
| 80 |
-
place_name =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
geocoded_data.append({
|
| 83 |
"ID": client_id,
|
| 84 |
"Nom": row.get('Nom_Complet', 'Client Inconnu'),
|
|
@@ -88,9 +108,13 @@ def geocode_addresses(df_clients):
|
|
| 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 |
|
|
|
|
| 49 |
# METTEZ VOTRE TOKEN MAPBOX ICI
|
| 50 |
MAPBOX_TOKEN = "pk.eyJ1Ijoia2x5ZGUwNyIsImEiOiJjbWpwd3B4cWMwYTVqM2ZxeHcwM3FoMHF5In0.mgiTZbug_4eD3cSNgF2t5Q" # Le même que vous avez utilisé pour la carte
|
| 51 |
|
| 52 |
+
# Bounding Box du Sénégal : [lon_min, lat_min, lon_max, lat_max]
|
| 53 |
+
SENEGAL_BBOX = [-17.535, 12.307, -11.355, 16.691]
|
| 54 |
+
|
| 55 |
geocoded_data = []
|
| 56 |
|
| 57 |
for index, row in df_clients.iterrows():
|
|
|
|
| 68 |
params = {
|
| 69 |
'access_token': MAPBOX_TOKEN,
|
| 70 |
'limit': 1,
|
| 71 |
+
'language': 'fr',
|
| 72 |
+
'country': 'sn', # Limiter au Sénégal uniquement
|
| 73 |
+
'bbox': ','.join(map(str, SENEGAL_BBOX)), # Bounding box Sénégal
|
| 74 |
+
'proximity': '-17.4467,14.6928' # Prioriser autour de Dakar
|
| 75 |
}
|
| 76 |
|
| 77 |
import requests
|
|
|
|
| 81 |
data = response.json()
|
| 82 |
|
| 83 |
if data['features']:
|
| 84 |
+
feature = data['features'][0]
|
| 85 |
+
coords = feature['geometry']['coordinates']
|
| 86 |
lon, lat = coords[0], coords[1]
|
| 87 |
+
place_name = feature['place_name']
|
| 88 |
+
relevance = feature.get('relevance', 0)
|
| 89 |
+
|
| 90 |
+
# Vérifier la pertinence du résultat
|
| 91 |
+
if relevance < 0.5:
|
| 92 |
+
st.warning(f"⚠️ {client_id}: Résultat peu fiable ({relevance:.2f}) - '{place_name}'")
|
| 93 |
+
|
| 94 |
+
# Vérifier que c'est bien au Sénégal
|
| 95 |
+
context = feature.get('context', [])
|
| 96 |
+
is_senegal = any('Senegal' in str(ctx) or 'Sénégal' in str(ctx) for ctx in context)
|
| 97 |
|
| 98 |
+
if not is_senegal and 'Senegal' not in place_name and 'Sénégal' not in place_name:
|
| 99 |
+
st.error(f"❌ {client_id}: Résultat hors Sénégal - '{place_name}' (IGNORÉ)")
|
| 100 |
+
continu
|
| 101 |
+
|
| 102 |
geocoded_data.append({
|
| 103 |
"ID": client_id,
|
| 104 |
"Nom": row.get('Nom_Complet', 'Client Inconnu'),
|
|
|
|
| 108 |
"Revenus": row.get('Revenus_Mensuels', 0)
|
| 109 |
})
|
| 110 |
|
| 111 |
+
st.success(f"✅ {client_id}: {place_name} (confiance: {relevance:.2f})")
|
| 112 |
else:
|
| 113 |
st.error(f"❌ {client_id}: Adresse introuvable - '{address}'")
|
| 114 |
+
|
| 115 |
+
elif response.status_code == 401:
|
| 116 |
+
st.error(f"❌ Token Mapbox invalide ou expiré")
|
| 117 |
+
break
|
| 118 |
else:
|
| 119 |
st.error(f"❌ {client_id}: Erreur API {response.status_code}")
|
| 120 |
|