Core API Reference¶
yoromaps¶
Top-level module with convenience imports.
yoromaps
¶
Yoro Maps — Offline maps, routing, and POI for Africa.
Companion to the yoro geocoding package.
Usage::
import yoromaps
# Build a .yoromaps file for Mali
yoromaps.build("ML", "mali.yoromaps")
# Route between Yoro codes
conn = yoromaps.open_db("mali.yoromaps")
result = yoromaps.route(conn, start_lat=12.6, start_lon=-8.0, end_lat=14.5, end_lon=-4.0)
print(f"{result.distance_km} km, {result.duration_min} min")
# Route from Yoro codes
legs = yoromaps.route_from_codes(conn, ["ML-ABC", "ML-XYZ"])
RouteResult
dataclass
¶
Result of a routing query.
Source code in src/yoromaps/routing.py
build(country_code, output, pbf_path=None, include_tiles=False, zoom_min=6, zoom_max=12, progress=None)
¶
Build a .yoromaps file for a country.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_code
|
str
|
ISO country code (e.g. "ML", "BF", "CI"). |
required |
output
|
str | Path
|
Output .yoromaps file path. |
required |
pbf_path
|
str | Path | None
|
Path to an existing PBF file. If None, downloads from Geofabrik. |
None
|
include_tiles
|
bool
|
Also download map tiles (slow, ~200+ MB). |
False
|
zoom_min
|
int
|
Min tile zoom level (if include_tiles). |
6
|
zoom_max
|
int
|
Max tile zoom level (if include_tiles). |
12
|
progress
|
ProgressCallback | None
|
Optional callable(message, current, total). |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created .yoromaps file. |
Source code in src/yoromaps/download.py
update(db_path, pbf_cache_dir=None, progress=None)
¶
Update an existing .yoromaps file with the latest OSM data.
Downloads the PBF only if Geofabrik has a newer version. Rebuilds the road graph while preserving local POI data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Path to the existing .yoromaps file. |
required |
pbf_cache_dir
|
str | Path | None
|
Directory to cache PBF files. Defaults to same dir as db. |
None
|
progress
|
ProgressCallback | None
|
Optional progress callback. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with |
Source code in src/yoromaps/download.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
open_db(path, create=False)
¶
Open a .yoromaps database. Creates schema if create is True.
Existing databases are upgraded transparently: the schema is fully
idempotent (CREATE ... IF NOT EXISTS), so files built by older
versions gain new tables/indexes on first open.
Source code in src/yoromaps/db.py
db_config(path)
¶
route(conn, start_lat, start_lon, end_lat, end_lon)
¶
Find the shortest route between two GPS coordinates.
Loads the graph into memory on each call. For multiple routes,
use Graph.from_db() directly.
Source code in src/yoromaps/routing.py
route_from_codes(conn, codes, graph=None)
¶
Route through multiple Yoro codes in order.
Returns a list of RouteResult, one per leg. Pass a preloaded graph
(e.g. from :func:get_graph) to avoid reloading it from the database.
Source code in src/yoromaps/routing.py
get_graph(db_path)
¶
Load the road graph for a .yoromaps file, cached per process.
The graph is loaded once and reused across calls (and across HTTP
requests in Django). The cache entry is invalidated when the file's
mtime changes, e.g. after yoromaps update.
Source code in src/yoromaps/routing.py
add_poi(conn, lat, lon, name, category='other', source='local', osm_id=None)
¶
Insert a POI; its Yoro code is computed automatically.
Source code in src/yoromaps/poi.py
pois_near(conn, lat, lon, radius_m=1000, category=None, limit=100)
¶
POIs within radius_m metres, sorted by distance.
Source code in src/yoromaps/poi.py
pois_in_cell(conn, code, category=None)
¶
All POIs inside the cell of a Yoro code (any precision).
Raises ValueError for invalid codes.
Source code in src/yoromaps/poi.py
search_pois(conn, query=None, category=None, limit=50)
¶
Search POIs by name substring and/or category.
Source code in src/yoromaps/poi.py
delete_poi(conn, poi_id)
¶
Delete a POI by id. Returns True if a row was removed.
yoromaps.routing¶
A* routing engine on the .yoromaps road graph.
yoromaps.routing
¶
A* routing engine on the .yoromaps road graph.
Loads the graph adjacency list into memory for fast routing. Typical memory usage: ~100-200 MB for a country like Mali or Togo.
RouteResult
dataclass
¶
Result of a routing query.
Source code in src/yoromaps/routing.py
Graph
¶
In-memory road graph loaded from a .yoromaps database.
Load once, route many times::
graph = Graph.from_db(conn)
r1 = graph.route(6.13, 1.22, 9.55, 1.18)
r2 = graph.route(6.13, 1.22, 8.98, 1.13)
Source code in src/yoromaps/routing.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
from_db(conn)
classmethod
¶
Load the entire graph into memory from a .yoromaps database.
Source code in src/yoromaps/routing.py
nearest_with_distance(lat, lon)
¶
Nearest node + haversine distance in metres.
Uses a uniform grid index (built lazily on first call) and searches outward ring by ring. The distance metric matches the historical behavior: squared degree distance for the argmin, haversine for the returned distance.
Source code in src/yoromaps/routing.py
route(start_lat, start_lon, end_lat, end_lon)
¶
Find the shortest route between two GPS coordinates using A*.
Source code in src/yoromaps/routing.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
route(conn, start_lat, start_lon, end_lat, end_lon)
¶
Find the shortest route between two GPS coordinates.
Loads the graph into memory on each call. For multiple routes,
use Graph.from_db() directly.
Source code in src/yoromaps/routing.py
route_from_codes(conn, codes, graph=None)
¶
Route through multiple Yoro codes in order.
Returns a list of RouteResult, one per leg. Pass a preloaded graph
(e.g. from :func:get_graph) to avoid reloading it from the database.
Source code in src/yoromaps/routing.py
get_graph(db_path)
¶
Load the road graph for a .yoromaps file, cached per process.
The graph is loaded once and reused across calls (and across HTTP
requests in Django). The cache entry is invalidated when the file's
mtime changes, e.g. after yoromaps update.
Source code in src/yoromaps/routing.py
yoromaps.poi¶
Points of interest, addressed by Yoro code. Every POI is encoded in the
database's country domain at insertion time, falling back to the worldwide
XX domain for points outside the country's bounding box.
yoromaps.poi
¶
POI management — local points of interest addressed by Yoro codes.
Every POI gets a Yoro code at insertion time (encoded in the database's
country domain, falling back to the global XX domain when the point
lies outside the country bbox). The idx_pois_yoro index makes
"what is at this address" lookups instant; radius and cell queries use
a lat/lon bbox prefilter.
Usage::
conn = yoromaps.open_db("mali.yoromaps")
poi = add_poi(conn, 12.639, -8.002, "Grand marche", category="market")
print(poi["yoro_code"]) # "ML-..."
pois_near(conn, 12.64, -8.0, radius_m=500) # sorted by distance
pois_in_cell(conn, "ML-4H7A3B")
add_poi(conn, lat, lon, name, category='other', source='local', osm_id=None)
¶
Insert a POI; its Yoro code is computed automatically.
Source code in src/yoromaps/poi.py
pois_near(conn, lat, lon, radius_m=1000, category=None, limit=100)
¶
POIs within radius_m metres, sorted by distance.
Source code in src/yoromaps/poi.py
pois_in_cell(conn, code, category=None)
¶
All POIs inside the cell of a Yoro code (any precision).
Raises ValueError for invalid codes.
Source code in src/yoromaps/poi.py
search_pois(conn, query=None, category=None, limit=50)
¶
Search POIs by name substring and/or category.
Source code in src/yoromaps/poi.py
delete_poi(conn, poi_id)
¶
Delete a POI by id. Returns True if a row was removed.
yoromaps.db¶
Database management for .yoromaps files.
yoromaps.db
¶
Database management for .yoromaps files.
A .yoromaps file is a single SQLite database containing: - tiles: MBTiles-compatible tile storage - nodes: road graph intersections - edges: road graph segments - pois: points of interest - metadata: version, country, timestamps
SCHEMA_VERSION = 2
module-attribute
¶
open_db(path, create=False)
¶
Open a .yoromaps database. Creates schema if create is True.
Existing databases are upgraded transparently: the schema is fully
idempotent (CREATE ... IF NOT EXISTS), so files built by older
versions gain new tables/indexes on first open.
Source code in src/yoromaps/db.py
db_config(path)
¶
set_metadata(conn, key, value)
¶
yoromaps.download¶
Download OSM data and build .yoromaps files.
yoromaps.download
¶
Download OSM data and build/update a .yoromaps file for a country.
build(country_code, output, pbf_path=None, include_tiles=False, zoom_min=6, zoom_max=12, progress=None)
¶
Build a .yoromaps file for a country.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_code
|
str
|
ISO country code (e.g. "ML", "BF", "CI"). |
required |
output
|
str | Path
|
Output .yoromaps file path. |
required |
pbf_path
|
str | Path | None
|
Path to an existing PBF file. If None, downloads from Geofabrik. |
None
|
include_tiles
|
bool
|
Also download map tiles (slow, ~200+ MB). |
False
|
zoom_min
|
int
|
Min tile zoom level (if include_tiles). |
6
|
zoom_max
|
int
|
Max tile zoom level (if include_tiles). |
12
|
progress
|
ProgressCallback | None
|
Optional callable(message, current, total). |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created .yoromaps file. |
Source code in src/yoromaps/download.py
download_pbf(country_code, output_dir, progress=None, if_newer_than=None)
¶
Download the latest OSM PBF for a country from Geofabrik.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
country_code
|
str
|
ISO country code. |
required |
output_dir
|
str | Path
|
Directory to save the PBF file. |
required |
progress
|
ProgressCallback | None
|
Optional progress callback. |
None
|
if_newer_than
|
str | None
|
HTTP date string. Skips download if server file is not newer. |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the downloaded file, or None if skipped (already up to date). |
Source code in src/yoromaps/download.py
yoromaps.tiles¶
MBTiles management — download and serve map tiles.
yoromaps.tiles
¶
MBTiles management — download and serve map tiles from SQLite.
get_tile(conn, z, x, y)
¶
Retrieve a tile from the database (XYZ scheme, converted to TMS internally).
Source code in src/yoromaps/tiles.py
tile_count(conn)
¶
download_tiles(conn, bbox, zoom_min=6, zoom_max=14, tile_url=DEFAULT_TILE_URL, progress=None)
¶
Download map tiles for a bounding box into the database.
.. warning:: The default source is the public openstreetmap.org tile server, whose usage policy discourages bulk downloading — keep zoom levels low (the default stops at 12) and prefer your own tile server or a commercial provider via tile_url for large areas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
Connection
|
Open SQLite connection. |
required |
bbox
|
tuple[float, float, float, float]
|
(lon_min, lat_min, lon_max, lat_max). |
required |
zoom_min
|
int
|
Minimum zoom level. |
6
|
zoom_max
|
int
|
Maximum zoom level. |
14
|
tile_url
|
str
|
Tile URL template with {z}, {x}, {y} placeholders. |
DEFAULT_TILE_URL
|
progress
|
ProgressCallback | None
|
Optional callable(message, current, total). |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of tiles downloaded. |
Source code in src/yoromaps/tiles.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
yoromaps.countries¶
Country registry with Geofabrik download URLs.
yoromaps.countries
¶
Country registry — Geofabrik download paths.
Names and bounding boxes come from yoro.DOMAINS (the single source of
truth for domain geometry shared by both packages); this module only adds
the Geofabrik extract path for each country.
COUNTRIES = {code: (_country(code, path)) for code, path in (_GEOFABRIK_PATHS.items())}
module-attribute
¶
GEOFABRIK_BASE = 'https://download.geofabrik.de'
module-attribute
¶
Country
dataclass
¶
geofabrik_url(country_code)
¶
Return the Geofabrik PBF download URL for a country.
Source code in src/yoromaps/countries.py
yoromaps.extract¶
OSM PBF extraction into the road graph.
yoromaps.extract
¶
Extract road graph from OSM PBF into a .yoromaps SQLite database.
Uses pyosmium to parse the PBF file in a single pass, extracting highway ways and their nodes into a graph suitable for routing.
HIGHWAY_SPEEDS = {'motorway': 110, 'motorway_link': 60, 'trunk': 90, 'trunk_link': 50, 'primary': 70, 'primary_link': 40, 'secondary': 60, 'secondary_link': 35, 'tertiary': 50, 'tertiary_link': 30, 'residential': 30, 'unclassified': 40, 'living_street': 20, 'service': 20, 'track': 15, 'path': 5}
module-attribute
¶
extract_graph(pbf_path, conn, progress=None)
¶
Extract road graph from a PBF file into the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pbf_path
|
str | Path
|
Path to the .osm.pbf file. |
required |
conn
|
Connection
|
Open SQLite connection to a .yoromaps database. |
required |
progress
|
ProgressCallback | None
|
Optional callable(message, current, total) for progress reporting. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: |
Source code in src/yoromaps/extract.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
haversine(lat1, lon1, lat2, lon2)
¶
Distance in meters between two GPS points.