Search by

survos / geonames-bundle

tacman1123

Symfony bundle for fetching packaged GeoNames authority databases.

Package info

github.com/survos/geonames-bundle

Type:symfony-bundle

pkg:composer/survos/geonames-bundle

Fund package maintenance!

kbond

Statistics

Installs: 206

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

2.45.80 2026-11-20 17:30 UTC

README

survos/geonames-bundle is the GeoNames authority-list bundle for packaged SQLite place databases.

The runtime model is intentionally small:

  • fetch a published authority database
  • store it locally
  • query it through a service such as GeoService

Runtime Databases

Published databases:

  • geo.sqlite contains country, admin1, admin2, and alias lookups for every country GeoNames covers
  • <countryCode>.sqlite (e.g. us.sqlite, hu.sqlite) contains that country's cities (city table), name-lookup aliases (alias table), and locale-specific alternate names (alt_name table — see below)

GeoNames provides the canonical ids, so the shared runtime base is geo.sqlite, with country-specific city databases layered on top.

Scope: what this bundle is not

GeoNames covers countries, administrative regions, and populated places (cities/towns) — it is not comprehensive for individual schools, stores, churches, or other points of interest, and this bundle doesn't try to make it one. For that layer, two companion bundles:

  • survos/place-map-bundle — a curated point/polygon library, one Nominatim query at a time.
  • survos/poi-bundle — bulk/automated matching of a literal name (e.g. from a facet tag) against OSM features within a city this bundle already resolved.

See survos/mono#28 for how the split was decided.

Locale-specific alternate names (alt_name)

Each <countryCode>.sqlite also carries an alt_name table (geoname_id, iso_language, alternate_name, is_preferred, is_short, is_colloquial, is_historic) sourced from GeoNames' per-country alternatenames/<CC>.zip export — the real language-tagged data (German "Wien" vs. French "Vienne" vs. English "Vienna" for the same geonameId), not the untagged alternatenames convenience column already on city rows. Query it through:

$names = $this->geoService->alternateNames(3054643, 'HU'); // Budapest
// ['de' => ['Budapest', 'Ofen-Pest'], 'ru' => ['Будапешт'], 'sk' => ['Budapešť'], ...]

Cities only for now — country/admin1/admin2 alt names in geo.sqlite aren't wired up yet (see PLAN.md). Background: survos/mono#26.

DuckDB backend

DuckDB is the primary format. When ext-pdo_duckdb is loaded, GeoService reads <name>.duckdb files instead of <name>.sqlite. Lookup order per database (geo, us, ...):

  1. <data_dir>/<name>.duckdb: a local file, read-only. This is also the cache directory.
  2. <duckdb_uri>/<name>.duckdb: the source of truth. With an https:// URI (the Hugging Face dataset), the file is downloaded into data_dir on first use and read locally from then on. Reading a remote file in place costs several round trips per lookup (Hugging Face took 0.3 to 0.75 s per range request), and every web request would pay that again. Set duckdb_cache: false to read it in place anyway. An s3://bucket/prefix URI is always read in place (needs survos/duckdb-bundle).
  3. <data_dir>/<name>.sqlite (the legacy format, for apps without the extension, and the fallback if the download fails)

A cached file is never refreshed automatically: run bin/console survos:geo --force (or delete the file) to fetch a new version. survos:geo fills the same directory, so deploys can warm the cache before the first request. Concurrent workers download under a file lock (<name>.duckdb.lock).

survos_geonames:
    # a public Hugging Face dataset: no credentials, read with HTTP range requests (needs DuckDB's httpfs)
    duckdb_uri: 'https://huggingface.co/datasets/museado/geonames-duckdb/resolve/main'
    # or S3 (also needs survos/duckdb-bundle):
    # duckdb_uri: 's3://my-bucket/geonames-data'
    # duckdb_s3: { endpoint: 'https://s3.eu-central-1.amazonaws.com', region: eu-central-1, access_key: '%env(S3_KEY)%', secret_key: '%env(S3_SECRET)%' }

Install httpfs for the engine and the runtime user when you provision the host (INSTALL httpfs); the bundle only runs LOAD httpfs.

How the files are built

admin.php build-duckdb reads the worldwide GeoNames dump files (allCountries.zip, alternateNamesV2.zip, and the small countryInfo.txt, admin1CodesASCII.txt, admin2Codes.txt: about 630 MB in five downloads) with DuckDB's own CSV reader. It loads the populated places (feature class P) and the language-tagged names once, then slices one <cc>.duckdb per country from them, so a full rebuild takes about a minute once the files are on disk. The tables are typed: INTEGER ids, DOUBLE coordinates, BOOLEAN flags, a DATE modification date, VARCHAR[] for the comma-separated columns (languages, neighbors) and ENUMs for feature_code and alias.target_table. There are no keys or indexes, because the files are read-only. Rows are sorted by the lookup column so DuckDB can skip most of the file. Table and column names match the SQLite files, except that city has no alternate_names column: every entry is already in alias (lowercased) or alt_name (language-tagged), and the list cost 21% of the file.

cd admin
php admin.php download                    # the five worldwide files
php admin.php build-duckdb                # geo.duckdb + every country; --country=hu limits the slices written

publish.sh runs both and uploads the result to its own dataset, museado/geonames-duckdb (HF_DUCKDB_REPO), so that apps can read the files over HTTP. The SQLite files are legacy and built only with LEGACY_SQLITE=1, from per-country archives (download --per-country).

Differences from the SQLite files: aliases are lowercased with Unicode rules (SQLite: ASCII only), and GeoService lowercases search terms to match the backend it queries.

Measurements

On us (193,759 cities): 75.5 MB as SQLite, 17.1 MB as DuckDB, and lookups take 13 ms instead of 141 ms (lower(name) = ... cannot use the SQLite indexes, so SQLite scans the table). Building hu from the raw files takes 0.1 s. Reads over HTTP from a local server took 210 ms for the first lookup (two attaches) and 6 ms after that; reads from S3 or Hugging Face add one network round trip per range request and are not measured yet.

We require DuckDB 2.0 or later and assume later versions read 2.0 files; if one does not, rebuild the files with build-duckdb. Composer cannot check this: ext-pdo_duckdb reports the extension's own version (1.0.0), not the engine's. Check the engine with php --ri pdo_duckdb ("DuckDB Library Version") or SELECT version(); survos:geo warns when it is older than 2.0.

Build Workflow

Build-time tooling is separate from the public bundle command, so the published bundle itself stays light — admin/ has its own dev dependencies (composer install inside bu/geonames-bundle) and never ships to consumer apps.

Admin console (admin/admin.php, a standalone Symfony Console app):

cd admin
php admin.php download                    # the worldwide dumps for build-duckdb (about 630 MB)
php admin.php build-duckdb                # geo.duckdb + one <cc>.duckdb per country
php admin.php download --per-country --country=hu  # legacy: one archive pair per country, for the SQLite build
php admin.php build --force --country=hu  # legacy: rebuild geo.sqlite + hu.sqlite from the per-country archives
php admin.php metadata                    # regenerate README.md/datasets.jsonl only, no rebuild

With --per-country, download fetches, per requested country, both <CC>.zip (cities) and alternatenames/<CC>.zip (locale-specific alternate names — see above) alongside the shared countryInfo.txt, admin1CodesASCII.txt, admin2Codes.txt, timeZones.txt, hierarchy.zip. build --force parses those files directly into SQLite with prepared statements — no ORM — and refreshes the dataset-card README.md/datasets.jsonl in the output directory.

One-shot: download + build + publish

admin/publish.sh runs the full pipeline — download, build, and hf upload to the museado/geonames-duckdb Hugging Face dataset (also museado/geonames-data with LEGACY_SQLITE=1) — for a country or every country:

cd admin
./publish.sh                  # every country GeoNames covers
./publish.sh us,hu,ch         # just these countries
SKIP_UPLOAD=1 ./publish.sh hu # download + build only, skip the Hugging Face upload

Requires the hf CLI (pip install -U huggingface_hub[cli]) logged in with write access to the dataset repo (hf auth login) unless SKIP_UPLOAD=1.

Publishing To Hugging Face

Each Hugging Face dataset directory contains a README.md dataset card, datasets.jsonl, geo.sqlite, and one <countryCode>.sqlite per built country. survos:geo (the runtime bundle command, below) fetches those published files rather than rebuilding them locally.

Installing The Bundle

composer req survos/geonames-bundle

Fetching The Authority

The public Symfony command:

bin/console survos:geo                  # fetch geo.duckdb
bin/console survos:geo --country=us,hu  # + one or more per-country city databases
bin/console survos:geo --all            # every published per-country database
bin/console survos:geo --format=sqlite  # the legacy SQLite files (default when ext-pdo_duckdb is missing)

fetches the published databases straight from the museado/geonames-duckdb (DuckDB) or museado/geonames-data (SQLite) Hugging Face dataset into GeoService::sqliteDir() — no auth needed, it's a public repo.

Fetch strategy (cache/retry/resume)

survos:geo uses two different survos/fetch-bundle services for two different jobs — don't reach for the wrong one if extending this command:

  • Listing published files (--all, the HF Hub tree API call) goes through PersistentFetcherInterface — small JSON, cached forever (until forget()/force_fetch) in a SQLite pool at var/data/fetch_cache.db. Retried up to 5 times with full-jitter exponential backoff (200ms base, 10s cap) on transport errors/429/5xx.
  • Downloading the .sqlite files themselves (up to ~3.4 GB total) goes through ChunkDownloader, not PersistentFetcher — PersistentFetcher buffers the whole response into the cache pool, which is fine for JSON and wrong for gigabytes. ChunkDownloader streams straight to disk (constant memory), resumes via HTTP Range if a transfer drops partway (<file>.sqlite.part), and retries transient failures independently of the listing cache. --force passes overwrite: true through to it.

Before 2026-08-08 download() was a hand-rolled stream-to-disk loop with no resume — a dropped 3 GB transfer restarted from byte 0.

Using The Bundle

The runtime API will stay lookup-oriented.

Example:

$geoId = $this->geoService->find('Oslo');

We will refine the query methods and return values later. For now, the design center is local lookup against fetched authority data.

See PLAN.md for the CSV layout and next migration steps.

Configuration

data_dir is the directory that holds the databases (<name>.duckdb, or the legacy <name>.sqlite) and the downloads cached by GeoService. It defaults to APP_SHARED_DIRECTORY/geonames-data, else var/geonames-data. sqlite_dir and GeoService::sqliteDir() still work and are deprecated since 2.45.76.