Core API Reference¶
Yoro Codec — Geographic addressing via 2D Hilbert curves.
Bijection between GPS coordinates and compact alphanumeric codes. Pure Python, zero external dependencies.
Theory: Paul Guindo, Altius Academy SNC.
encode(lat, lon, precision=12, domain='CI')
¶
Encode GPS coordinates to an Yoro string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude (WGS 84). |
required |
lon
|
float
|
Longitude (WGS 84). |
required |
precision
|
int
|
Hilbert order (higher = finer grid). Default 12. |
12
|
domain
|
str
|
ISO country code or "XX" for global. |
'CI'
|
Returns:
| Type | Description |
|---|---|
str
|
Code string, e.g. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If domain is unknown, or if the coordinates fall outside the domain's bounding box. |
Source code in src/yoro/codec.py
encode_for_country(lat, lon, country_code, precision=12)
¶
Encode a point using its country's domain, falling back worldwide.
The country-specific domains are bounding boxes, so a point can be
legitimately in a country and outside its box: border areas, enclaves,
offshore points, and countries covered by a combined extract. A country may
also have no domain at all. Either way the point stays addressable — this
falls back to the planet-wide "XX" domain instead of raising.
Prefer this over encode(lat, lon, domain=domain_for_country(cc))
whenever the coordinates are not known to sit inside the box: that form
raises, and every caller ends up reimplementing the same fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
float
|
Latitude in degrees. |
required |
lon
|
float
|
Longitude in degrees. |
required |
country_code
|
str | None
|
ISO 3166-1 alpha-2 code, e.g. |
required |
precision
|
int
|
Hilbert order; see :func: |
12
|
Returns:
| Type | Description |
|---|---|
str
|
Code string, in the country's domain when it fits, |
Example::
>>> encode_for_country(6.8, -5.3, "CI") # inside the box
'CI-NW64D'
>>> encode_for_country(64.1, -21.9, "CI") # Reykjavik is not
'XX-...'
Source code in src/yoro/codec.py
decode(code)
¶
Decode an Yoro string to GPS coordinates and cell bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Yoro string, e.g. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the code format, length, or domain is invalid. |
Source code in src/yoro/codec.py
neighbors(code)
¶
Return up to 8 neighboring cell codes (edge/corner adjacency).
Codes on the domain boundary may return fewer than 8 neighbors.
Source code in src/yoro/codec.py
resolution(p, domain='CI')
¶
Approximate spatial resolution in meters for Hilbert order p in domain.
Source code in src/yoro/codec.py
get_bounds(code)
¶
cells_in_bounds(lat_min, lat_max, lon_min, lon_max, precision=12, domain='CI', max_cells=2000)
¶
Return all Hilbert cells that intersect a geographic bounding box.
Each item is {"code": "CI-...", "bounds": {...}}.
Raises:
| Type | Description |
|---|---|
ValueError
|
If domain is unknown, or if the requested area would produce more than max_cells cells (lower the precision or shrink the bounding box). |
Source code in src/yoro/codec.py
precision_levels(domain='CI', max_code_length=10)
¶
Return all canonical precision levels for a domain.
A canonical precision is a Hilbert order p that produces a distinct
code length k. Because codes use base-29, the mapping from p to k
is k = ceil(2p * log2 / log29). Several consecutive values of p
map to the same k — only the highest p for each k is canonical,
i.e. the one that fully exploits the address space of k characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
ISO country code (affects resolution in meters). |
'CI'
|
max_code_length
|
int
|
Stop after this many characters (default 10 → ~4 cm). |
10
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts with keys: |
list[dict]
|
|
Source code in src/yoro/codec.py
snap_precision(p)
¶
Return the canonical precision that p actually resolves to.
Because the code length is quantized to whole base-29 characters, several values of p produce the same grid. This function shows which canonical precision is effectively used.
Example::
>>> snap_precision(18)
19 # p=18 and p=19 both use 8-character codes
>>> snap_precision(15)
17 # p=15 and p=16 both snap up to canonical p=17
Source code in src/yoro/codec.py
domain_for_country(country_code)
¶
Map an ISO country code to an Yoro domain. Falls back to "XX".
domain_of(code)
¶
Return the domain a code belongs to, without decoding it.
Cheaper than :func:decode when all you need is the domain — reading the
prefix costs a string split, while decoding walks the whole Hilbert curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
A Yoro code, e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The domain prefix, e.g. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is invalid or the domain is unknown. |
Example::
>>> domain_of("ci-4h7a3b")
'CI'