-
Notifications
You must be signed in to change notification settings - Fork 0
feat: authoritative Albania block resolver (#118) #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e719705
feat: add Albania postal-code block resolver (#118)
bk86a 03fef08
test: golden regression locking block resolver to shipped AL codes (#…
bk86a 7e69728
feat: resolve Albania via the block map in lookup() (#118)
bk86a 1385974
refactor: drop GeoNames AL estimates in favor of the block resolver (…
bk86a 9952d0d
docs: changelog for Albania block resolver (#118)
bk86a 388076d
docs: align README and lookup() docstring with the Albania block reso…
bk86a ec2290b
docs: refresh estimates statistics after Albania rows removed (#118)
bk86a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Authoritative Albania (AL) NUTS3 resolver from the postal-code BLOCK scheme. | ||
|
|
||
| Albania has no Eurostat TERCET file. Its postal codes are block-allocated by | ||
| district: the first two digits identify one of ~33 postal districts, and each | ||
| district sits in exactly one of the 12 qarks (= NUTS3). A range map keyed on the | ||
| district-center codes resolves ANY well-formed 4-digit code to its NUTS3 by the | ||
| block it falls into — covering the gaps GeoNames leaves (issue #118) by | ||
| construction, at NUTS3 granularity. | ||
|
|
||
| Source: official Posta Shqiptare allocation, cross-checked vs. Wikipedia "Postal | ||
| codes in Albania" and the UPU addressing PDF. The district->qark->NUTS3 mapping | ||
| reuses the GISCO-verified qark codes; the two non-obvious assignments (Kruje | ||
| 15xx -> AL012, Kavaje 25xx -> AL022) are confirmed by GeoNames' own 15xx/25xx | ||
| tagging. Validated 100% against the 489 previously-shipped GeoNames codes (see | ||
| tests/test_albania_golden.py). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from bisect import bisect_right | ||
|
|
||
| SUPPORTED: frozenset[str] = frozenset({"AL"}) | ||
|
|
||
| # (district-center code, NUTS3, district name). Ascending by code. Each code is | ||
| # the LOWER bound of that district's block; a block runs to the next code. | ||
| # 1700 "Transit" / 1800 "EMS" are non-geographic service codes folded into | ||
| # Tirana (AL022), matching how GeoNames tags the 17xx/18xx prefixes. | ||
| BLOCKS: list[tuple[int, str, str]] = [ | ||
| (1000, "AL022", "Tirana"), | ||
| (1500, "AL012", "Kruje"), | ||
| (1700, "AL022", "Transit (service)"), | ||
| (1800, "AL022", "EMS Office (service)"), | ||
| (2000, "AL012", "Durres"), | ||
| (2500, "AL022", "Kavaje"), | ||
| (3000, "AL021", "Elbasan"), | ||
| (3300, "AL021", "Gramsh"), | ||
| (3400, "AL021", "Librazhd"), | ||
| (3500, "AL021", "Peqin"), | ||
| (4000, "AL015", "Shkoder"), | ||
| (4300, "AL015", "Malesi e Madhe"), | ||
| (4400, "AL015", "Puke"), | ||
| (4500, "AL014", "Lezhe"), | ||
| (4600, "AL014", "Mirdite"), | ||
| (4700, "AL014", "Kurbin"), | ||
| (5000, "AL031", "Berat"), | ||
| (5300, "AL031", "Kucove"), | ||
| (5400, "AL031", "Skrapar"), | ||
| (6000, "AL033", "Gjirokaster"), | ||
| (6300, "AL033", "Tepelene"), | ||
| (6400, "AL033", "Permet"), | ||
| (7000, "AL034", "Korce"), | ||
| (7300, "AL034", "Pogradec"), | ||
| (7400, "AL034", "Kolonje"), | ||
| (8000, "AL011", "Mat"), | ||
| (8300, "AL011", "Diber"), | ||
| (8400, "AL011", "Bulqize"), | ||
| (8500, "AL013", "Kukes"), | ||
| (8600, "AL013", "Has"), | ||
| (8700, "AL013", "Tropoje"), | ||
| (9000, "AL032", "Lushnje"), | ||
| (9300, "AL032", "Fier"), | ||
| (9400, "AL035", "Vlore"), | ||
| (9700, "AL035", "Sarande"), | ||
| ] | ||
|
|
||
| _STARTS = [b[0] for b in BLOCKS] | ||
| _NUTS3 = [b[1] for b in BLOCKS] | ||
|
|
||
|
|
||
| def resolve_al_block(postal_code: str) -> str | None: | ||
| """NUTS3 code for a well-formed 4-digit AL postal code, else None. | ||
|
|
||
| Any code >= 1000 maps to its enclosing district block (incl. 9800-9999 -> | ||
| Sarande/AL035 as best-effort). Codes < 1000, wrong length, or non-numeric | ||
| return None. | ||
| """ | ||
| if not (len(postal_code) == 4 and postal_code.isdigit()): | ||
| return None | ||
| n = int(postal_code) | ||
| if n < _STARTS[0]: | ||
| return None | ||
| return _NUTS3[bisect_right(_STARTS, n) - 1] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lookup uses the previous center code for every numeric value until the next center, so an AL input like
1900or9999is returned as a high-confidence Tirana/Sarandë estimate even though the module's own allocation describes district identity by the first two digits andBLOCKSonly lists prefixes such as 10, 15, 17, 18, 20, etc. The new tier therefore turns unallocated 4-digit AL codes in the gaps between listed prefixes into successful NUTS matches; use the first-two-digit block keys (or explicit valid ranges) rather than a continuous bisect interval if these should keep returning no match.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct that the bisect range-fills: any code ≥ 1000 resolves to its enclosing district block, so unallocated-prefix inputs like 1900 or 9999 return a best-effort region rather than no-match. This is a deliberate design choice, not an oversight — the goal of #118 was to stop valid AL codes 404-ing, and the resolver leans toward best-effort coverage (documented in the docstring's "9800–9999 → Sarandë as best-effort" note).
Worth stating precisely, since it bounds the concern: all 489 real AL codes — and every gap code #118 was about — sit in allocated 2-digit prefixes, so they resolve identically under either approach (the golden regression test covers this). The only inputs affected by range-fill vs. prefix-strict are ~5,500 codes in unallocated prefixes, which are almost certainly non-existent codes. Range-fill returns a plausible neighbor region for those; prefix-strict would 404 them.
Your alternative (key on the 35 allocated 2-digit prefixes, 404 the gaps) is a reasonable and arguably more honest tradeoff — it stops asserting confidence 0.9 for codes with no allocated district — and it's free for real coverage. I've flagged it to the maintainer as a follow-up decision, since switching reverses the approved best-effort behavior. If we adopt it, it'll be a small follow-up PR against the
_STARTS/bisect logic. Thanks for the catch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #134: the resolver now keys on the allocated 2-digit district prefix, so codes in unallocated prefixes (1900, 9999, …) return not-found instead of a fabricated region. All 489 real codes and every #118 gap code still resolve identically (golden test unchanged).