Spaces:
Sleeping
Sleeping
Upload app/airports.py with huggingface_hub
Browse files- app/airports.py +56 -0
app/airports.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Airport metadata: IATA → name, city, state, lat, lon.
|
| 3 |
+
Uses the `airportsdata` package; falls back to a hardcoded dict for any misses.
|
| 4 |
+
"""
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
import airportsdata
|
| 7 |
+
|
| 8 |
+
_RAW = airportsdata.load("IATA")
|
| 9 |
+
|
| 10 |
+
# Supplement / override for a handful of airports airportsdata might miss
|
| 11 |
+
_OVERRIDES: dict[str, dict] = {}
|
| 12 |
+
|
| 13 |
+
MONTH_NAMES = {
|
| 14 |
+
1: "January", 2: "February", 3: "March", 4: "April",
|
| 15 |
+
5: "May", 6: "June", 7: "July", 8: "August",
|
| 16 |
+
9: "September", 10: "October", 11: "November", 12: "December",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get(iata: str) -> dict:
|
| 21 |
+
"""Return airport info dict with keys: name, city, subd (state), lat, lon."""
|
| 22 |
+
code = iata.upper().strip()
|
| 23 |
+
if code in _OVERRIDES:
|
| 24 |
+
return _OVERRIDES[code]
|
| 25 |
+
return _RAW.get(code, {"name": code, "city": code, "subd": "", "lat": None, "lon": None})
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def label(iata: str) -> str:
|
| 29 |
+
"""Short display label: 'DFW — Dallas/Fort Worth'."""
|
| 30 |
+
info = get(iata)
|
| 31 |
+
city = info.get("city") or info.get("name") or iata
|
| 32 |
+
return f"{iata} — {city}"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def coords(iata: str) -> tuple[float | None, float | None]:
|
| 36 |
+
"""Return (lat, lon) or (None, None)."""
|
| 37 |
+
info = get(iata)
|
| 38 |
+
return info.get("lat"), info.get("lon")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def build_airport_df(iata_codes: list[str]) -> "pd.DataFrame":
|
| 42 |
+
"""DataFrame of airport metadata for a list of IATA codes."""
|
| 43 |
+
import pandas as pd
|
| 44 |
+
rows = []
|
| 45 |
+
for code in iata_codes:
|
| 46 |
+
info = get(code)
|
| 47 |
+
lat, lon = info.get("lat"), info.get("lon")
|
| 48 |
+
rows.append({
|
| 49 |
+
"iata": code,
|
| 50 |
+
"name": info.get("name", code),
|
| 51 |
+
"city": info.get("city", code),
|
| 52 |
+
"state": info.get("subd", ""),
|
| 53 |
+
"lat": lat,
|
| 54 |
+
"lon": lon,
|
| 55 |
+
})
|
| 56 |
+
return pd.DataFrame(rows)
|