Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,37 @@ def load_geojson(path: str) -> dict:
|
|
| 74 |
return json.load(f)
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# =========================
|
| 78 |
# Main report builder
|
| 79 |
# =========================
|
|
@@ -83,15 +114,7 @@ def build_report():
|
|
| 83 |
# We'll still build a figure (it may appear blank/limited).
|
| 84 |
pass
|
| 85 |
|
| 86 |
-
countries_geojson = load_geojson(GEOJSON_PATH)
|
| 87 |
-
# DEBUG: find which property contains 'FRA'
|
| 88 |
-
sample_props = countries_geojson["features"][0]["properties"]
|
| 89 |
-
print("DEBUG geojson property keys:", list(sample_props.keys())[:50])
|
| 90 |
-
|
| 91 |
-
# Try a few common keys and see which one yields a match for FRA
|
| 92 |
-
for key in ["ISO3166-1-Alpha-3", "ISO_A3", "ADM0_A3", "name", "NAME", "ADMIN"]:
|
| 93 |
-
hits = [f for f in countries_geojson["features"] if f["properties"].get(key) == "FRA"]
|
| 94 |
-
print(f"DEBUG key={key}: matches={len(hits)}")
|
| 95 |
|
| 96 |
# Counters for clean reconciliation
|
| 97 |
scanned = 0
|
|
|
|
| 74 |
return json.load(f)
|
| 75 |
|
| 76 |
|
| 77 |
+
def patch_geojson_iso_codes(countries_geojson: dict) -> dict:
|
| 78 |
+
"""
|
| 79 |
+
Some GeoJSON files have ISO codes as '-99'. Patch them using the 'name' field.
|
| 80 |
+
Updates:
|
| 81 |
+
properties['ISO3166-1-Alpha-2']
|
| 82 |
+
properties['ISO3166-1-Alpha-3']
|
| 83 |
+
"""
|
| 84 |
+
fixed = 0
|
| 85 |
+
for feat in countries_geojson.get("features", []):
|
| 86 |
+
props = feat.get("properties", {}) or {}
|
| 87 |
+
iso3 = str(props.get("ISO3166-1-Alpha-3", "") or "").strip()
|
| 88 |
+
iso2 = str(props.get("ISO3166-1-Alpha-2", "") or "").strip()
|
| 89 |
+
name = str(props.get("name", "") or "").strip()
|
| 90 |
+
|
| 91 |
+
needs_fix = (iso3 == "-99" or iso2 == "-99" or not iso3 or not iso2)
|
| 92 |
+
if not needs_fix or not name:
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
rec = pycountry.countries.search_fuzzy(name)[0]
|
| 97 |
+
props["ISO3166-1-Alpha-3"] = rec.alpha_3
|
| 98 |
+
props["ISO3166-1-Alpha-2"] = rec.alpha_2
|
| 99 |
+
fixed += 1
|
| 100 |
+
except Exception:
|
| 101 |
+
# leave as-is if we can't resolve
|
| 102 |
+
pass
|
| 103 |
+
|
| 104 |
+
print(f"DEBUG patched GeoJSON features: {fixed}")
|
| 105 |
+
return countries_geojson
|
| 106 |
+
|
| 107 |
+
|
| 108 |
# =========================
|
| 109 |
# Main report builder
|
| 110 |
# =========================
|
|
|
|
| 114 |
# We'll still build a figure (it may appear blank/limited).
|
| 115 |
pass
|
| 116 |
|
| 117 |
+
countries_geojson = patch_geojson_iso_codes(load_geojson(GEOJSON_PATH))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Counters for clean reconciliation
|
| 120 |
scanned = 0
|