API Reference
Complete API reference for poiidx.
Module: poiidx
The main module provides high-level functions for working with POIs and administrative boundaries.
Functions
init()
Initialize the database connection and optionally create/update the schema.
Automatic Data Recreation
Even when recreate=False, poiidx will automatically drop and recreate all data if it detects:
- Schema changes (from library updates)
- Filter configuration changes
- Missing or corrupted schema metadata
All data should be considered temporary.
Parameters:
filter_config(list[dict]): List of POI filter configurations. Each dict should contain:symbol(str): Symbol category for grouping POIsdescription(str): Human-readable descriptionfilters(list[dict]): List of OSM tag combinations to match
recreate(bool, optional): If True, forces immediate drop and recreation of the database schema. Default: False (but automatic recreation may still occur based on schema detection)**kwargs: Database connection parameters:host(str): Database hostdatabase(str): Database nameuser(str): Database userpassword(str): Database passwordport(int): Database port (default: 5432)pbf_cache(bool): Enable PBF file caching (default: True)
Returns: None
Raises:
- RuntimeError: If initialization fails
Example:
import yaml
import poiidx
with open('filters.yaml') as f:
filters = yaml.safe_load(f)
poiidx.init(
filter_config=filters,
host='localhost',
database='poiidx_db',
user='poiidx_user',
password='secret',
port=5432
)
close()
Close the database connection.
Parameters: None
Returns: None
Example:
get_nearest_pois()
Find nearest Points of Interest to a given geometry.
poiidx.get_nearest_pois(
shape: shapely.geometry.base.BaseGeometry,
buffer: float | None = None,
**kwargs: Any
) -> list[dict[str, Any]]
Parameters:
shape(BaseGeometry): Shapely geometry to search from (Point, Polygon, etc.)buffer(float, optional): Buffer distance in meters for region initialization**kwargs: Additional parameters passed toPoiIdx.get_nearest_pois():max_distance(float, optional): Maximum distance in meterslimit(int): Number of results to return (default: 1)regions(list[str], optional): Filter by specific regionsrank_range(tuple[int, int], optional): Filter by rank range (min, max)
Returns: list[dict] - List of POI dictionaries with keys:
- osm_id (str): OpenStreetMap ID
- name (str): POI name
- region (str): Region identifier
- coordinates (str): Geometry as WKT string
- filter_item (str): OSM tag key
- filter_expression (str): OSM tag value
- rank (int): Priority rank
- symbol (str): Symbol category
Example:
from shapely.geometry import Point
berlin = Point(13.4050, 52.5200)
pois = poiidx.get_nearest_pois(
berlin,
max_distance=1000,
limit=5
)
get_administrative_hierarchy()
Get administrative boundaries containing the given geometry.
poiidx.get_administrative_hierarchy(
shape: shapely.geometry.base.BaseGeometry,
buffer: float | None = None
) -> list[dict[str, Any]]
Parameters:
shape(BaseGeometry): Shapely geometry to querybuffer(float, optional): Buffer distance in meters for region initialization
Returns: list[dict] - List of administrative boundary dictionaries with keys:
- osm_id (str): OpenStreetMap ID
- name (str): Boundary name
- admin_level (int): Administrative level (2=country, 4=state, 6=county, etc.)
- geometry (str): Boundary geometry as WKT string
- tags (dict): Additional OSM tags (may include localized names)
Example:
from shapely.geometry import Point
location = Point(13.4050, 52.5200)
hierarchy = poiidx.get_administrative_hierarchy(location)
for admin in hierarchy:
print(f"Level {admin['admin_level']}: {admin['name']}")
get_administrative_hierarchy_string()
Get administrative hierarchy as a formatted string.
poiidx.get_administrative_hierarchy_string(
shape: shapely.geometry.base.BaseGeometry,
lang: str | None = None,
buffer: float | None = None
) -> str
Parameters:
shape(BaseGeometry): Shapely geometry to querylang(str, optional): ISO 639-1 language code for localized names (e.g., 'de', 'fr', 'es')buffer(float, optional): Buffer distance in meters for region initialization
Returns: str - Formatted administrative hierarchy string (comma-separated)
Example:
from shapely.geometry import Point
location = Point(13.4050, 52.5200)
# Default (English)
hierarchy = poiidx.get_administrative_hierarchy_string(location)
# "Berlin, Germany"
# French
hierarchy_fr = poiidx.get_administrative_hierarchy_string(location, lang='fr')
# "Berlin, Allemagne"
recreate_schema()
Drop and recreate the database schema.
Parameters: None
Returns: None
Warning: This will delete all data!
drop_schema()
Drop all database tables.
Parameters: None
Returns: None
Warning: This will delete all data!
Class: PoiIdx
Low-level class providing direct database access. Most users should use the module-level functions instead.
Located in: poiidx.poiIdx
Class Methods
PoiIdx.connect()
Initialize database connection.
PoiIdx.get_nearest_pois()
Low-level POI search with additional options.
PoiIdx.get_nearest_pois(
shape: shapely.geometry.base.BaseGeometry,
max_distance: float | None = None,
limit: int = 1,
regions: list[str] | None = None,
rank_range: tuple[int, int] | None = None
) -> list[Poi]
Returns: list[Poi] - List of Poi model instances (not dictionaries)
PoiIdx.get_administrative_hierarchy()
Low-level administrative boundary query.
PoiIdx.get_administrative_hierarchy(
shape: shapely.geometry.base.BaseGeometry
) -> list[AdministrativeBoundary]
Returns: list[AdministrativeBoundary] - List of model instances
Models
Database models using Peewee ORM.
Poi
Point of Interest model.
Located in: poiidx.poi
Fields:
osm_id(CharField): OpenStreetMap identifiername(CharField): POI nameregion(CharField): Region identifier (e.g., 'europe/germany')coordinates(GeometryField): PostGIS point geometry (SRID 4326)filter_item(CharField): OSM tag keyfilter_expression(CharField): OSM tag valuerank(IntegerField): Priority ranksymbol(CharField): Symbol category
Indexes:
- SPGIST index on
coordinatesfor efficient spatial queries - Index on
region - Index on
rank
AdministrativeBoundary
Administrative boundary model.
Located in: poiidx.administrativeBoundary
Fields:
osm_id(CharField): OpenStreetMap identifiername(CharField): Boundary nameadmin_level(IntegerField): Administrative levelgeometry(GeometryField): PostGIS geometry (SRID 4326)tags(JSONField): Additional OSM tags (including localized names)
Indexes:
- GIST index on
geometry - Index on
admin_level
Country
Country information model.
Located in: poiidx.country
Fields:
id(CharField, primary key): Country codename(CharField): Country namegeometry(GeometryField): Country boundary geometry
System
System configuration model.
Located in: poiidx.system
Fields:
system(BooleanField, primary key): System flagfilter_config(TextField): JSON-encoded filter configurationregion_index(TextField): Serialized region index
Geometry Types
poiidx uses Shapely for geometry handling. All geometries use WGS84 (SRID 4326).
Supported Geometry Types
-
Point: Single location
-
Polygon: Area boundary
-
LineString: Path or route
-
MultiPoint, MultiPolygon, MultiLineString: Collections
Filter Configuration Schema
POI filters are defined in YAML or as Python dictionaries:
- symbol: string # Symbol category for grouping
description: string # Human-readable description
filters: # List of tag combinations
- tag_key: tag_value # All tags in a dict must match (AND)
tag_key2: tag_value2
- tag_key3: tag_value3 # Different dicts are alternatives (OR)
Example:
- symbol: food
description: Food and dining establishments
filters:
- amenity: restaurant
- amenity: cafe
- amenity: bar
- symbol: tourism
description: Tourist attractions and hotels
filters:
- tourism: hotel
- museum
- attraction
symbol: tourism
rank: 2
Distance Units
All distance parameters use meters as the unit:
max_distance: metersbuffer: meters
Distance calculations use PostGIS geography functions for accurate results on the Earth's surface.
Region Identifiers
Regions follow the Geofabrik naming scheme:
europe/germany- Germanynorth-america/us- United Statesasia/japan- Japan- etc.
Data is downloaded from Geofabrik on-demand when a region is first queried.
Error Handling
Common Exceptions
- RuntimeError: "PoiIdx not initialized"
- Call
poiidx.init()before using other functions - Database connection errors
- Check PostgreSQL is running and credentials are correct
- PostGIS not found
- Install PostGIS extension:
CREATE EXTENSION postgis;
Best Practices
Always use try-finally or context managers:
Type Hints
poiidx includes comprehensive type hints. Use with mypy:
Common type imports: