Skip to content

Add get_admin1_codes() for first-level administrative divisions - #48

Open
cvl01 wants to merge 1 commit into
yaph:masterfrom
cvl01:add-admin1-codes
Open

Add get_admin1_codes() for first-level administrative divisions#48
cvl01 wants to merge 1 commit into
yaph:masterfrom
cvl01:add-admin1-codes

Conversation

@cvl01

@cvl01 cvl01 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

Adds support for the GeoNames first-level administrative division dataset (admin1CodesASCII.txt), exposed via a new GeonamesCache.get_admin1_codes() method.

Design

The returned dictionary is keyed by the composite code <countrycode>.<admin1code>, e. g. US.CA, matching the format used in the GeoNames source file. This is the headline benefit of the composite key: city records store countrycode and admin1code as separate fields, so their admin1 references are directly resolvable:

import geonamescache

gc = geonamescache.GeonamesCache()
city = gc.get_cities()['5368361']  # Los Angeles
admin1 = gc.get_admin1_codes()[f"{city['countrycode']}.{city['admin1code']}"]
print(admin1['name'])  # California

Each record contains name, asciiname and geonameid.

Changes

  • bin/download_data.py: download admin1CodesASCII.txt
  • bin/admin1.py: new generator producing admin1.json
  • Makefile: run the new generator in the json target
  • geonamescache/types.py: new Admin1 TypedDict and Admin1CodeStr key alias
  • geonamescache/__init__.py: new get_admin1_codes() method
  • Tests, README and CHANGELOG updated

Verification

  • pytest: all new and existing tests pass (the new tests cover key lookup, dataset size and resolving a city's admin1 reference)
  • ruff check and mypy geonamescache tests pass

Adds the GeoNames admin1CodesASCII.txt dataset as a new admin1.json data
file, keyed by the composite code <countrycode>.<admin1code> (e. g.
US.CA). Since city records store countrycode and admin1code separately,
the composite key allows resolving those references to the admin1 name
and geonameid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant