X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/9659afbade47d1ec6d5359b2b21e1e874516ed80..98c1b923fc090f830b7b3a575e3d08ba399bd870:/src/nominatim_db/tools/postcodes.py?ds=sidebyside diff --git a/src/nominatim_db/tools/postcodes.py b/src/nominatim_db/tools/postcodes.py index 357b2bae..4763aa03 100644 --- a/src/nominatim_db/tools/postcodes.py +++ b/src/nominatim_db/tools/postcodes.py @@ -25,6 +25,7 @@ from ..tokenizer.base import AbstractAnalyzer, AbstractTokenizer LOG = logging.getLogger() + def _to_float(numstr: str, max_value: float) -> float: """ Convert the number in string into a float. The number is expected to be in the range of [-max_value, max_value]. Otherwise rises a @@ -36,6 +37,7 @@ def _to_float(numstr: str, max_value: float) -> float: return num + class _PostcodeCollector: """ Collector for postcodes of a single country. """ @@ -46,7 +48,6 @@ class _PostcodeCollector: self.collected: Dict[str, PointsCentroid] = defaultdict(PointsCentroid) self.normalization_cache: Optional[Tuple[str, Optional[str]]] = None - def add(self, postcode: str, x: float, y: float) -> None: """ Add the given postcode to the collection cache. If the postcode already existed, it is overwritten with the new centroid. @@ -63,7 +64,6 @@ class _PostcodeCollector: if normalized: self.collected[normalized] += (x, y) - def commit(self, conn: Connection, analyzer: AbstractAnalyzer, project_dir: Path) -> None: """ Update postcodes for the country from the postcodes selected so far as well as any externally supplied postcodes. @@ -97,9 +97,9 @@ class _PostcodeCollector: """).format(pysql.Literal(self.country)), to_update) - - def _compute_changes(self, conn: Connection) \ - -> Tuple[List[Tuple[str, float, float]], List[str], List[Tuple[float, float, str]]]: + def _compute_changes( + self, conn: Connection + ) -> Tuple[List[Tuple[str, float, float]], List[str], List[Tuple[float, float, str]]]: """ Compute which postcodes from the collected postcodes have to be added or modified and which from the location_postcode table have to be deleted. @@ -125,7 +125,6 @@ class _PostcodeCollector: return to_add, to_delete, to_update - def _update_from_external(self, analyzer: AbstractAnalyzer, project_dir: Path) -> None: """ Look for an external postcode file for the active country in the project directory and add missing postcodes when found. @@ -155,7 +154,6 @@ class _PostcodeCollector: finally: csvfile.close() - def _open_external(self, project_dir: Path) -> Optional[TextIO]: fname = project_dir / f'{self.country}_postcodes.csv' @@ -225,6 +223,7 @@ def update_postcodes(dsn: str, project_dir: Path, tokenizer: AbstractTokenizer) analyzer.update_postcodes_from_db() + def can_compute(dsn: str) -> bool: """ Check that the place table exists so that