Switch to an authoritative source of Spanish municipality data #114 and credit it in README #115

This commit is contained in:
Ian Renton
2026-06-23 19:26:22 +01:00
parent e4c3a52299
commit eb1d575623
13 changed files with 8168 additions and 8155 deletions

View File

@@ -7,8 +7,10 @@ from core.cache_utils import SEMI_STATIC_URL_DATA_CACHE
from core.constants import SIGS, HTTP_HEADERS
from core.geo_utils import wab_wai_square_to_lat_lon
with open("datafiles/dme-geodata.csv", encoding="utf-8") as _f:
_DME_INDEX = {row["dme"]: row for row in csv.DictReader(_f)}
# Load Spanish municipality data for the DME programme. There's no convenient lookup API for this, so we embed the data
# file in Spothole and load it on startup.
with open("datafiles/MUNICIPIOS.csv", encoding="latin-1") as _f:
_DME_INDEX = {row["COD_INE"][:5]: row for row in csv.DictReader(_f, delimiter=";")}
def get_ref_regex_for_sig(sig):
@@ -192,11 +194,12 @@ def populate_sig_ref_info(sig_ref):
except:
logging.debug("Invalid lat/lon received for reference")
elif sig.upper() == "DME":
row = _DME_INDEX.get(ref_id)
# Zero-pad to 5 digits to match our source data
row = _DME_INDEX.get(ref_id.zfill(5))
if row:
sig_ref.name = row["municipio"] + ", " + row["provincia"]
sig_ref.latitude = float(row["lat"]) if row.get("lat") else None
sig_ref.longitude = float(row["lon"]) if row.get("lon") else None
sig_ref.name = row["NOMBRE_ACTUAL"] + ", " + row["PROVINCIA"]
sig_ref.latitude = float(row["LATITUD_ETRS89_REGCAN95"].replace(",", ".")) if row.get("LATITUD_ETRS89_REGCAN95") else None
sig_ref.longitude = float(row["LONGITUD_ETRS89_REGCAN95"].replace(",", ".")) if row.get("LONGITUD_ETRS89_REGCAN95") else None
if sig_ref.latitude and sig_ref.longitude:
try:
sig_ref.grid = latlong_to_locator(sig_ref.latitude, sig_ref.longitude, 6)