import csv from time import sleep from pyhamtools.locator import latlong_to_locator from core.enums import ActivityName, ActivityRefType from data.activity_ref import ActivityRef from providers.activityrefdata.local_file_activity_ref_data_provider import ( LocalFileActivityRefDataProvider, ) class DME(LocalFileActivityRefDataProvider): """Activity ref data provider for Diploma Municipios de Espana""" ACTIVITY = ActivityName.DME PATH = "datafiles/MUNICIPIOS.csv" def __init__(self, provider_config): super().__init__(self.ACTIVITY, provider_config, self.PATH) def _file_to_data(self, path): new_data = [] with open(path, encoding="latin-1") as _f: for row in csv.DictReader(_f, delimiter=";"): # Store reference IDs with the "DME-" prefix rather than just the number. This will prevent Spothole # from agressively thinking every number in a spot comment is DME after it's seen "DME" once. The only # numbers that count are straight after "DME " or "DME-". The dash versus space is normalised in # activity_lookup_helper.py. ref_id = "DME-" + row["COD_INE"][:5] latitude = ( float(row["LATITUD_ETRS89_REGCAN95"].replace(",", ".")) if row.get("LATITUD_ETRS89_REGCAN95") else None ) longitude = ( float(row["LONGITUD_ETRS89_REGCAN95"].replace(",", ".")) if row.get("LONGITUD_ETRS89_REGCAN95") else None ) ref = ActivityRef( sig=self.ACTIVITY, id=ref_id, ref_type=ActivityRefType.TOWN, name=f"{row['NOMBRE_ACTUAL']}, {row['PROVINCIA']}", latitude=latitude, longitude=longitude, ) if latitude and longitude: ref.grid = latlong_to_locator(latitude, longitude, 6) new_data.append(ref) # Bail out if a stop has been requested, i.e. the program is shutting down - no need to parse the rest # of the data in this case if self._stop_event.is_set(): break # Very short pause. This will extend the time to handle activity refs by a few seconds but will ensure some time # is available for other threads e.g. the web server. sleep(0.001) return new_data