File size: 20,655 Bytes
5c385ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b2c8c3
5c385ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""Géolocalise les arrêts du réseau avec Nominatim (OpenStreetMap).



Pour chaque arrêt unique de data/itineraires_dakar.csv, on interroge Nominatim,

on garde latitude, longitude, quartier et commune, puis on écrit

data/arrets_geo.csv. Aucun CSV existant ni la recherche de ligne n'est modifié.



Robustesse de la recherche :

  - plusieurs requêtes avant d'abandonner un arrêt : nom nettoyé des préfixes

    (« Giratoire de Liberté VI » -> « Liberté 6 »), chiffres romains convertis,

    puis commune de voisinage (« Pikine », « Guédiawaye », « Rufisque ») quand

    les arrêts voisins de la ligne y sont ;

  - les arrêts toujours introuvables reçoivent une position estimée (milieu de

    leurs deux voisins connus sur la même ligne, si ces voisins sont à moins de

    3 km), confiance « estimee » ; sinon l'arrêt reste « introuvable » ;

  - un arrêt n'est « douteuse » que s'il fait un détour anormal entre ses deux

    voisins : d(précédent, arrêt) + d(arrêt, suivant) > 2·d(précédent, suivant) + 2 km ;

  - un géocodage inverse (reverse, zoom 14) donne un quartier/commune homogène

    pour chaque position finale.



Politique d'usage Nominatim : au plus une requête par seconde, User-Agent

explicite, et cache local (data/sources/cache_nominatim.json) : une requête déjà

faite n'est jamais refaite.



Usage :

    python data/sources/geolocaliser_arrets.py              # collecte + rapport

    python data/sources/geolocaliser_arrets.py --limite 10  # essai sur 10 arrêts

"""

from __future__ import annotations

import argparse
import csv
import json
import math
import re
import sys
import time
import unicodedata
from pathlib import Path

import requests

RACINE = Path(__file__).resolve().parents[2]
DATA = RACINE / "data"
ITINERAIRES = DATA / "itineraires_dakar.csv"
SORTIE = DATA / "arrets_geo.csv"
CACHE = Path(__file__).resolve().parent / "cache_nominatim.json"

GEOCODE = "https://nominatim.openstreetmap.org/search"
REVERSE = "https://nominatim.openstreetmap.org/reverse"
USER_AGENT = "GuindiMa-AI/1.0 (+https://github.com/chniang/wolof-bus-assistant)"
DELAI_SECONDES = 1.0
TIMEOUT = 30
ZOOM_REVERSE = 14
RAYON_DETOUR = 2000.0  # marge du contrôle « détour anormal », en mètres
DISTANCE_VOISINS_MAX = 3000.0  # écart max entre les deux voisins connus, pour estimer

CHAMPS_QUARTIER = ("neighbourhood", "quarter", "suburb")
CHAMPS_COMMUNE = ("city_district", "city", "municipality")

# Communes de la banlieue : si un voisin de la ligne y est, on retente avec cette
# commune au lieu de « Dakar ».
COMMUNES_BANLIEUE = {"pikine": "Pikine", "guediawaye": "Guédiawaye", "rufisque": "Rufisque"}

# Préfixes d'arrêt sans valeur pour Nominatim.
PREFIXES = re.compile(
    r"^\s*(?:terminus|arr[êe]t|giratoire(?:\s+de)?|rond[-\s]?point(?:\s+de)?|"
    r"croisement(?:\s+de)?|station|gare(?:\s+(?:de|routi[èe]re))?)\s+",
    re.I,
)

ROMAINS = {
    "i": 1, "ii": 2, "iii": 3, "iv": 4, "v": 5, "vi": 6,
    "vii": 7, "viii": 8, "ix": 9, "x": 10, "xi": 11, "xii": 12,
}


# --------------------------------------------------------------------------- #
# Nettoyage des noms
# --------------------------------------------------------------------------- #


def sans_accents(texte: str) -> str:
    normalise = unicodedata.normalize("NFD", str(texte).lower().replace("œ", "oe"))
    return "".join(c for c in normalise if unicodedata.category(c) != "Mn")


def nettoyer_prefixes(nom: str) -> str:
    """Retire les préfixes d'arrêt (« Terminus Gare de X » -> « X »)."""
    precedent = None
    while precedent != nom:
        precedent = nom
        nom = PREFIXES.sub("", nom).strip()
    return nom


def remplacer_romains(nom: str) -> str:
    """« Hamo VI » -> « Hamo 6 ». Ne touche qu'aux nombres romains entiers."""
    return re.sub(
        r"\b[IVXLCDMivxlcdm]{1,4}\b",
        lambda m: str(ROMAINS[m.group(0).lower()]) if m.group(0).lower() in ROMAINS else m.group(0),
        nom,
    )


def formes(arret: str) -> list[str]:
    """Requêtes possibles pour un arrêt, de la plus fidèle à la plus nettoyée."""
    resultat: list[str] = []

    def ajouter(nom: str) -> None:
        nom = nom.strip()
        if nom and nom not in resultat:
            resultat.append(nom)

    ajouter(arret)
    ajouter(nettoyer_prefixes(arret))
    ajouter(remplacer_romains(arret))
    ajouter(remplacer_romains(nettoyer_prefixes(arret)))
    return resultat


# --------------------------------------------------------------------------- #
# Nominatim
# --------------------------------------------------------------------------- #


def _champs(objet: dict) -> dict:
    """Latitude, longitude, quartier, commune d'un objet Nominatim."""
    adresse = objet.get("address") or {}
    quartier = next((adresse[c] for c in CHAMPS_QUARTIER if adresse.get(c)), "")
    commune = next((adresse[c] for c in CHAMPS_COMMUNE if adresse.get(c)), "")
    return {
        "lat": float(objet["lat"]),
        "lon": float(objet["lon"]),
        "quartier": quartier,
        "commune": commune,
        "pays": str(adresse.get("country_code") or "").lower(),
        "display_name": objet.get("display_name", ""),
    }


def _resultat_geocode(brut) -> dict:
    return {"trouve": True, **_champs(brut[0])} if brut else {"trouve": False}


def _resultat_reverse(brut) -> dict:
    if not brut or brut.get("error"):
        return {"trouve": False}
    return {"trouve": True, **_champs(brut)}


class Nominatim:
    """Client poli : cache d'abord, puis au plus une requête réseau par seconde."""

    def __init__(self) -> None:
        self.cache = charger_cache()
        self.session = requests.Session()
        self.derniere = 0.0
        self.requetes = 0

    def _sauver(self) -> None:
        CACHE.write_text(json.dumps(self.cache, ensure_ascii=False, indent=1, sort_keys=True), encoding="utf-8")

    def _attendre(self) -> None:
        attente = DELAI_SECONDES - (time.monotonic() - self.derniere)
        if attente > 0:
            time.sleep(attente)

    def _appeler(self, url: str, params: dict, cle: str, convertir):
        """Cache d'abord, puis une requête (1/s). None si échec réseau (non mis en cache)."""
        if cle in self.cache:
            return self.cache[cle]
        self._attendre()
        try:
            reponse = self.session.get(url, params=params, headers={"User-Agent": USER_AGENT}, timeout=TIMEOUT)
            reponse.raise_for_status()
            donnees = reponse.json()
        except Exception as exc:
            self.derniere = time.monotonic()
            print(f"    [échec réseau] {cle} : {type(exc).__name__} : {exc}", flush=True)
            return None
        self.derniere = time.monotonic()
        self.requetes += 1
        self.cache[cle] = convertir(donnees)
        self._sauver()
        return self.cache[cle]

    def geocode(self, requete: str) -> dict | None:
        params = {"q": requete, "format": "jsonv2", "addressdetails": "1", "limit": "1"}
        return self._appeler(GEOCODE, params, f"fwd|{requete}", _resultat_geocode)

    def reverse(self, lat: float, lon: float) -> dict | None:
        params = {"lat": f"{lat}", "lon": f"{lon}", "format": "jsonv2", "zoom": str(ZOOM_REVERSE), "addressdetails": "1"}
        return self._appeler(REVERSE, params, f"rev|{lat:.5f},{lon:.5f}", _resultat_reverse)


def distance_metres(a: tuple[float, float], b: tuple[float, float]) -> float:
    rayon = 6_371_000.0
    p1, p2 = math.radians(a[0]), math.radians(b[0])
    dp = math.radians(b[0] - a[0])
    dl = math.radians(b[1] - a[1])
    h = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
    return 2 * rayon * math.asin(math.sqrt(h))


# --------------------------------------------------------------------------- #
# Données
# --------------------------------------------------------------------------- #


def lire_itineraires() -> list[dict]:
    with open(ITINERAIRES, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def arrets_uniques(rangees: list[dict]) -> list[str]:
    vus: dict[str, None] = {}
    for rangee in rangees:
        vus.setdefault(rangee["arret"], None)
    return list(vus)


def lignes_ordonnees(rangees: list[dict]) -> dict[tuple[str, str], list[str]]:
    par_ligne: dict[tuple[str, str], list[str]] = {}
    for rangee in rangees:
        par_ligne.setdefault((rangee["compagnie"], rangee["ligne"]), []).append(rangee["arret"])
    return par_ligne


def charger_cache() -> dict:
    """Charge le cache ; convertit l'ancien format (clé = arrêt) au nouveau."""
    if not CACHE.exists():
        return {}
    donnees = json.loads(CACHE.read_text(encoding="utf-8"))
    migre = {}
    for cle, valeur in donnees.items():
        if cle.startswith(("fwd|", "rev|")):
            migre[cle] = valeur
        else:  # la requête de base était « <arrêt>, Dakar, Sénégal »
            migre[f"fwd|{cle}, Dakar, Sénégal"] = valeur
    return migre


# --------------------------------------------------------------------------- #
# Collecte
# --------------------------------------------------------------------------- #


def geocoder_arret(arret: str, client: Nominatim, variantes: list[str]) -> dict | None:
    """Première requête qui trouve l'arrêt ; None si échec réseau ou aucune trouvaille."""
    for requete in variantes:
        info = client.geocode(requete)
        if info is None:  # échec réseau : on n'abandonne pas définitivement, on retente plus tard
            return None
        if info.get("trouve"):
            return {**info, "_requete": requete}
    return None


def communes_voisines(arret: str, par_ligne: dict, trouves: dict[str, dict]) -> list[str]:
    """Communes de banlieue où se trouvent les arrêts voisins de la ligne."""
    cibles: list[str] = []
    for arrets in par_ligne.values():
        if arret not in arrets:
            continue
        index = arrets.index(arret)
        for voisin in (index - 1, index + 1):
            if not 0 <= voisin < len(arrets):
                continue
            info = trouves.get(arrets[voisin])
            if not info:
                continue
            texte = sans_accents(f"{info.get('display_name', '')} {info.get('commune', '')}")
            for cle, nom in COMMUNES_BANLIEUE.items():
                if re.search(rf"\b{cle}\b", texte) and nom not in cibles:
                    cibles.append(nom)
    return cibles


def interpoler(

    par_ligne: dict[tuple[str, str], list[str]], positions: dict[str, tuple[float, float]]

) -> dict[str, list[tuple[float, float]]]:
    """Estimations : milieu des deux voisins connus, s'ils sont à moins de 3 km.



    On ne devine une position qu'entre deux arrêts géocodés proches : au-delà de

    DISTANCE_VOISINS_MAX, le milieu n'a plus de sens (il tomberait au hasard

    entre deux points éloignés) et l'arrêt reste « introuvable ».

    """
    estimations: dict[str, list[tuple[float, float]]] = {}
    for arrets in par_ligne.values():
        connus = [i for i, a in enumerate(arrets) if a in positions]
        for i, arret in enumerate(arrets):
            if arret in positions:
                continue
            avant = max((j for j in connus if j < i), default=None)
            apres = min((j for j in connus if j > i), default=None)
            if avant is None or apres is None:
                continue
            pa, pb = positions[arrets[avant]], positions[arrets[apres]]
            if distance_metres(pa, pb) > DISTANCE_VOISINS_MAX:
                continue
            estimations.setdefault(arret, []).append(((pa[0] + pb[0]) / 2, (pa[1] + pb[1]) / 2))
    return estimations


def detours_anormaux(

    par_ligne: dict[tuple[str, str], list[str]], positions: dict[str, tuple[float, float]]

) -> dict[str, str]:
    """Arrêts dont d(précédent, arrêt) + d(arrêt, suivant) > 2·d(précédent, suivant) + 2 km."""
    douteux: dict[str, str] = {}
    for (compagnie, ligne), arrets in par_ligne.items():
        for i in range(1, len(arrets) - 1):
            avant, arret, apres = arrets[i - 1], arrets[i], arrets[i + 1]
            if not all(a in positions for a in (avant, arret, apres)):
                continue
            d1 = distance_metres(positions[avant], positions[arret])
            d2 = distance_metres(positions[arret], positions[apres])
            d0 = distance_metres(positions[avant], positions[apres])
            if d1 + d2 > 2 * d0 + RAYON_DETOUR:
                douteux.setdefault(
                    arret,
                    f"{compagnie} {ligne} : {d1 / 1000:.1f} + {d2 / 1000:.1f} km "
                    f"au lieu de {d0 / 1000:.1f} km",
                )
    return douteux


# --------------------------------------------------------------------------- #
# Sortie et rapport
# --------------------------------------------------------------------------- #


def ecrire_csv(lignes: list[dict]) -> None:
    colonnes = ["arret", "lat", "lon", "quartier", "commune", "confiance"]
    with open(SORTIE, "w", newline="", encoding="utf-8") as f:
        ecrivain = csv.DictWriter(f, fieldnames=colonnes)
        ecrivain.writeheader()
        ecrivain.writerows({c: ligne[c] for c in colonnes} for ligne in lignes)


def arrets_de_ligne(rangees: list[dict], compagnie: str, ligne: str) -> list[str]:
    """Arrêts d'une ligne, dans l'ordre, dédoublonnés."""
    retenues = sorted(
        (r for r in rangees if r["compagnie"] == compagnie and r["ligne"] == ligne),
        key=lambda r: float(r["ordre"]),
    )
    vus: dict[str, None] = {}
    for rangee in retenues:
        vus.setdefault(rangee["arret"], None)
    return list(vus)


def afficher_ligne(titre: str, arrets: list[str], lignes: list[dict]) -> None:
    par_arret = {ligne["arret"]: ligne for ligne in lignes}
    print(f"\n--- {titre} ({len(arrets)} arrêts) ---")
    for arret in arrets:
        ligne = par_arret.get(arret, {})
        lieu = ligne.get("quartier") or ligne.get("commune") or ""
        coords = f"{ligne.get('lat', '')},{ligne.get('lon', '')}" if ligne.get("lat") else "—"
        print(f"  {arret:<38} {lieu:<20} {coords:<23} [{ligne.get('confiance', '')}]")


def main() -> None:
    if hasattr(sys.stdout, "reconfigure"):
        sys.stdout.reconfigure(encoding="utf-8", errors="replace")

    parser = argparse.ArgumentParser(description="Géolocalise les arrêts du réseau avec Nominatim.")
    parser.add_argument("--limite", type=int, default=None, help="ne traiter que les N premiers arrêts")
    args = parser.parse_args()

    rangees = lire_itineraires()
    arrets = arrets_uniques(rangees)
    if args.limite:
        arrets = arrets[: args.limite]
    par_ligne = lignes_ordonnees(rangees)

    client = Nominatim()
    positions: dict[str, tuple[float, float]] = {}
    trouves: dict[str, dict] = {}

    # 1) Requêtes de base et nettoyées, sur Dakar.
    print(f"=== 1) Géocodage de {len(arrets)} arrêts ===")
    for i, arret in enumerate(arrets, start=1):
        variantes = [f"{forme}, Dakar, Sénégal" for forme in formes(arret)]
        info = geocoder_arret(arret, client, variantes)
        if info:
            positions[arret] = (info["lat"], info["lon"])
            trouves[arret] = info
        if i % 50 == 0 or i == len(arrets):
            print(f"  {i:3d}/{len(arrets)}  trouvés {len(positions)}  requêtes {client.requetes}", flush=True)

    # 2) Restants : on retente avec la commune des voisins (si banlieue).
    restants = [a for a in arrets if a not in positions]
    deja_trouves = len(positions)
    print(f"\n=== 2) {len(restants)} arrêts à retenter par commune de voisinage ===")
    for i, arret in enumerate(restants, start=1):
        communes = communes_voisines(arret, par_ligne, trouves)
        variantes = [f"{forme}, {commune}, Sénégal" for commune in communes for forme in formes(arret)]
        info = geocoder_arret(arret, client, variantes)
        if info:
            positions[arret] = (info["lat"], info["lon"])
            trouves[arret] = info
        if i % 50 == 0 or i == len(restants):
            print(f"  {i:3d}/{len(restants)}  trouvés en plus {len(positions) - deja_trouves}  requêtes {client.requetes}", flush=True)

    # 3) Interpolation des arrêts toujours introuvables.
    estimes: dict[str, tuple[float, float]] = {}
    for arret, liste in interpoler(par_ligne, positions).items():
        lat = sum(p[0] for p in liste) / len(liste)
        lon = sum(p[1] for p in liste) / len(liste)
        positions[arret] = estimes[arret] = (lat, lon)

    # 4) Détours anormaux.
    douteux = detours_anormaux(par_ligne, positions)

    # 5) Géocodage inverse : quartier/commune homogène pour chaque position.
    print(f"\n=== 3) Géocodage inverse de {len(positions)} positions ===")
    infos: dict[str, dict] = {}
    for i, (arret, (lat, lon)) in enumerate(positions.items(), start=1):
        infos[arret] = client.reverse(lat, lon) or {"trouve": False}
        if i % 100 == 0 or i == len(positions):
            print(f"  {i:3d}/{len(positions)}  requêtes {client.requetes}", flush=True)

    # 6) Fichier et bilan.
    lignes = []
    for arret in arrets:
        info = infos.get(arret) or {"trouve": False}
        if arret not in positions:
            confiance = "introuvable"
        elif arret in douteux:
            confiance = "douteuse"
        elif arret in estimes:
            confiance = "estimee"
        else:
            confiance = "bonne"
        lignes.append(
            {
                "arret": arret,
                "lat": f"{positions[arret][0]:.6f}" if arret in positions else "",
                "lon": f"{positions[arret][1]:.6f}" if arret in positions else "",
                "quartier": info.get("quartier", "") if info.get("trouve") else "",
                "commune": info.get("commune", "") if info.get("trouve") else "",
                "confiance": confiance,
            }
        )
    ecrire_csv(lignes)

    comptes = {"bonne": 0, "estimee": 0, "douteuse": 0, "introuvable": 0}
    for ligne in lignes:
        comptes[ligne["confiance"]] += 1

    print("\n=== Bilan ===")
    print(f"{len(lignes)} arrêts -> {SORTIE.relative_to(RACINE)}")
    for nom in ("bonne", "estimee", "douteuse", "introuvable"):
        print(f"  {nom:11s}: {comptes[nom]}")

    if douteux:
        print(f"\n--- Détours anormaux ({len(douteux)}) ---")
        for arret in douteux:
            print(f"  {arret} : {douteux[arret]}")
    if lignes and sum(1 for l in lignes if l["confiance"] == "introuvable"):
        print(f"\n--- Introuvables ({sum(1 for l in lignes if l['confiance'] == 'introuvable')}) ---")
        for arret in (l["arret"] for l in lignes if l["confiance"] == "introuvable"):
            print(f"  {arret}")

    # Deux lignes de référence : Médina doit apparaître sur « Avenue Blaise Diagne »
    # et Liberté 6 sur « Giratoire [de] Liberté VI ».
    for compagnie, ligne in (("Tata AFTU", "Ligne 1"), ("Dakar Dem Dikk", "Ligne 4")):
        afficher_ligne(f"{compagnie} {ligne}", arrets_de_ligne(rangees, compagnie, ligne), lignes)

    # Arrêts proches du Marché Sandaga (le marché est géocodé d'abord).
    marche = client.geocode("Marché Sandaga, Dakar, Sénégal")
    if marche and marche.get("trouve"):
        centre = (marche["lat"], marche["lon"])
        par_arret = {ligne["arret"]: ligne for ligne in lignes}
        proches = sorted((distance_metres(centre, p), a) for a, p in positions.items() if distance_metres(centre, p) <= 500)
        print(f"\n--- Arrêts à moins de 500 m du Marché Sandaga ({marche.get('display_name', '')}) ---")
        for distance, arret in proches:
            ligne = par_arret.get(arret, {})
            lieu = ligne.get("quartier") or ligne.get("commune") or ""
            print(f"  {arret:<38} {distance:4.0f} m  {lieu}")
        if not proches:
            print("  (aucun)")
    else:
        print("\n[Marché Sandaga] introuvable via Nominatim")

    print(f"\n{client.requetes} requêtes Nominatim cette exécution — {len(client.cache)} entrées en cache")


if __name__ == "__main__":
    main()