add support for custom per-country postcode extents

This commit is contained in:
Sarah Hoffmann
2025-12-23 14:17:06 +01:00
parent 9ec607b556
commit a97b5d97cb

View File

@@ -2,7 +2,7 @@
#
# This file is part of Nominatim. (https://nominatim.org)
#
# Copyright (C) 2024 by the Nominatim developer community.
# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Functions for formatting postcodes according to their country-specific
@@ -67,12 +67,15 @@ class PostcodeFormatter:
self.country_without_postcode: Set[Optional[str]] = {None}
self.country_matcher = {}
self.default_matcher = CountryPostcodeMatcher('', {'pattern': '.*'})
self.postcode_extent: dict[Optional[str], int] = {}
for ccode, prop in country_info.iterate('postcode'):
if prop is False:
self.country_without_postcode.add(ccode)
elif isinstance(prop, dict):
self.country_matcher[ccode] = CountryPostcodeMatcher(ccode, prop)
if 'extent' in prop:
self.postcode_extent[ccode] = int(prop['extent'])
else:
raise UsageError(f"Invalid entry 'postcode' for country '{ccode}'")
@@ -113,3 +116,9 @@ class PostcodeFormatter:
`match()`
"""
return self.country_matcher.get(country_code, self.default_matcher).normalize(match)
def get_postcode_extent(self, country_code: Optional[str]) -> int:
""" Return the extent (in m) to use for the given country. If no
specific extent is set, then the default of 5km will be returned.
"""
return self.postcode_extent.get(country_code, 5000)