Region management API
Date: 2026-10-04
Goal
Let callers (1) download or import a Geofabrik region explicitly, by name/id or by point/shape, and (2) inspect which regions contain a point or shape and which of them poiidx would actually use.
Today the region selection is internal: PoiIdx.init_regions_by_shape runs
RegionFinder.find_regions, then downloads and ingests whatever it picked, and the only
way to trigger this is a query function. Nothing public exposes Region or the
selection.
Background
RegionFinder.find_regions(shape)greedily picks the smallest-area Geofabrik region that removes part of the remaining shape, repeating until the shape is empty. A large region that contains the shape (e.g. DACH for a point in Germany) is a candidate but is never picked when a smaller region already covers it.Pbf.get_pbf_filenamedownloads into the cache dir (atomic, checksum-verified) or returns the cached file. The cache dir is only persistent whenconnect(pbf_cache=True)(default); otherwise aTemporaryDirectoryis used and deleted after ingestion.- Ingestion is guarded by per-region locks (
_region_lock) and an all-or-nothingdatabase.atomic()block. - "Imported" is currently
has_region_data: anyPoirow with that region key.
Public API (poiidx/__init__.py)
All functions call assert_initialized() and return list[dict[str, Any]], matching
the existing functions. shape is a shapely geometry; buffer has the same meaning as
in get_nearest_pois.
| Function | Behaviour |
|---|---|
list_regions(shape=None, buffer=None) |
No shape: all Geofabrik regions. With shape: every region intersecting the (buffered) shape. |
download_region(name_or_id) |
Ensure the PBF is in the cache. No import. |
import_region(name_or_id) |
Download if needed, then import. |
download_regions_at(shape, buffer=None) |
download_region for each region the finder selects. Returns them. |
import_regions_at(shape, buffer=None) |
import_region for each region the finder selects. Returns them. |
Import implies download. Importing an already imported region is a no-op.
Result dict
| Key | Meaning |
|---|---|
id |
Geofabrik region id |
name |
Region name |
url |
PBF URL |
used |
The finder selects this region for the given shape. Always False when list_regions has no shape. |
downloaded |
The PBF is present in the persistent cache. Always False when pbf_cache=False. |
imported |
The region has data in the database. |
The *_at functions only return selected regions, so used is True there.
Name or id lookup
- Exact id match wins.
- Otherwise a case-insensitive name match.
- If a name matches several regions, raise
ValueErrorlisting the candidate ids. - If nothing matches, raise
ValueError.
Errors
download_region/download_regions_atraiseRuntimeErrorwhenpbf_cache=False, since the file would be deleted immediately.import_*work in both modes.- Network failures propagate from
Pbfunchanged.
Internals
PoiIdx.find_regions_by_shapestays the single selection implementation. AddRegionFinder.find_candidates(shape)returning all intersecting regions;list_regionsmarks the subset returned byfind_regionsasused, so the flag cannot drift from what ingestion does.- Move the buffer logic (
LocalProjectionconvex hull + buffer) out ofinit_regions_by_shapeinto a shared helper so all shape-based entry points apply it identically. - Extract the cache-dir selection from
initialize_pois_for_regioninto one helper used by both download and import. - Add
PoiIdx.download_region_pbf(region_id)andPoiIdx.import_region(region_id); the latter takes_region_lock(region_id)and re-checkshas_region_datainside it, exactly likeinit_regions_by_shape.init_regions_by_shapeis rewritten on top of it. downloadedis(cachedir / f"{id}.pbf").exists().has_region_databecomes "region has Poi rows or AdministrativeBoundary rows". Today a region with no POIs matching the filter config is re-ingested on every call, and would be reported as not imported. No schema change (that would wipe user data, see CLAUDE.md). A region with neither POIs nor boundaries still counts as not imported.Region(NamedTuple) stays internal; the public surface is the dicts above.
Testing
- No Docker:
RegionFindercandidates vs. selected, using a small hand-made GeoJSON index with nested regions (a DACH-like parent containing two children); name/id lookup including ambiguity and unknown names. - Existing testcontainers module:
import_region/import_regions_atwith the PBF fetch stubbed (idempotence,importedflag, the zero-POI-with-boundaries case), anddownload_*raising underpbf_cache=False. - Concurrency: two threads calling
import_regionfor the same id ingest once.
Docs
docs/reference.md: the five functions and the result dict.docs/how-to-guides.md: "Pre-fetch a region" (by name, by point).docs/explanation.md: candidate vs. used regions, with the DACH example, and the change to what counts as imported.
Out of scope
- Deleting or evicting regions or cached PBFs.
- Reporting why a region was not used.
- Refreshing a region's data or the Geofabrik index.
- CLI commands (can follow once the API exists).