Skip to main content

Fast, offline reverse geocoding for India

Project description

lakhua (Python)

Sub-millisecond reverse geocoding for India. Runs entirely in-memory — zero API calls, zero network, zero latency overhead.

PyPI Python License: MIT

Features

  • 📍 converts lat, lon to city, state, optional district and pincode
  • 🔢 supports direct H3 index lookup via geocode_h3()
  • ↩️ parent-cell fallback (resolution 5 → 4) when exact cell has no data
  • ⚡ data loaded once per process — all subsequent lookups are in-memory dict reads
  • 🐛 optional debug mode traces load time and per-lookup timing
  • 🔷 fully typed — dataclasses with py.typed marker included

Installation

pip install lakhua

Quick Start

from lakhua import geocode

result = geocode(28.6139, 77.2090)
if result:
    print(result.city, result.state)

API

Top-level functions (recommended)

geocode(lat: float, lon: float, options: Optional[GeocodeOptions] = None) -> Optional[GeocodeResult]
geocode_h3(h3_index: str, options: Optional[GeocodeOptions] = None) -> Optional[GeocodeResult]

These use the internal singleton geocoder — no class instantiation needed.

GeocodeOptions

from dataclasses import dataclass

@dataclass
class GeocodeOptions:
    resolution: int = 5       # H3 resolution for geocode(lat, lon)
    fallback: bool = True     # Walk up to parent resolution on miss
    debug: bool = False       # Print load and lookup timings

GeocodeResult

from dataclasses import dataclass
from typing import Optional

@dataclass(frozen=True)
class GeocodeResult:
    city: str
    state: str
    matched_h3: str            # H3 cell that matched (may be parent)
    matched_resolution: int    # Resolution of the matched cell
    district: Optional[str] = None
    pincode: Optional[str] = None

Returns None for invalid input or when no data exists for the given location.

Advanced class APIs

from lakhua import ReverseGeocoder, DataLoader

ReverseGeocoder.get_instance()
DataLoader.get_instance()

Use these only when you need explicit control — e.g. testing or custom singleton lifecycle.

Examples

Coordinate lookup

from lakhua import geocode

result = geocode(12.9716, 77.5946)  # Bengaluru
if result:
    print(result.city, result.state, result.pincode)

Direct H3 lookup

from lakhua import geocode_h3

result = geocode_h3("8560145bfffffff")
if result:
    print(result.city)

Debug mode

from lakhua import geocode, GeocodeOptions

result = geocode(19.076, 72.8777, GeocodeOptions(debug=True))
# prints load + lookup timings to stdout

Disable fallback

from lakhua import geocode, GeocodeOptions

result = geocode(28.6139, 77.2090, GeocodeOptions(fallback=False))
# only checks resolution 5, no parent lookup

Data Source and Indexing

  • Indexing system: Uber H3
  • Geographic source data: OpenStreetMap data by OpenStreetMap contributors
  • Distribution model: precomputed JSON stores bundled with the package

Performance

  • Data is loaded into memory once on first call.
  • Each lookup is a single dict read — typically < 1ms.
  • With fallback enabled, up to 2 dict reads (resolution 5, then 4).

Development

# install with dev dependencies
pip install -e ".[dev]"

# run tests
pytest

# lint and format
ruff check .
ruff format .

# type check
mypy lakhua

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lakhua-1.0.0.tar.gz (241.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lakhua-1.0.0-py3-none-any.whl (250.0 kB view details)

Uploaded Python 3

File details

Details for the file lakhua-1.0.0.tar.gz.

File metadata

  • Download URL: lakhua-1.0.0.tar.gz
  • Upload date:
  • Size: 241.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lakhua-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a348fa354671806315120421bb1e3d6fdf066f3c25b7e28cb5d913f2d09d69ea
MD5 574e1051b6feab3fd4482af40d08452e
BLAKE2b-256 b4510fdd2204c40ee22e9c8c0b9379644aa60537ff3d2a6678440f80c993259c

See more details on using hashes here.

Provenance

The following attestation bundles were made for lakhua-1.0.0.tar.gz:

Publisher: pypi-publish.yml on aialok/lakhua

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lakhua-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: lakhua-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 250.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lakhua-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9b8aa10a20c5a89bbd521a9c555e8388f911a34d0026831cab16af9f40fb0ec
MD5 cb3d52210f28882d4382b564f7b7218e
BLAKE2b-256 7b4289ca51e1ab6c6da4452f6df6f34033a43e35ec3b9ac0c6ed641472be87da

See more details on using hashes here.

Provenance

The following attestation bundles were made for lakhua-1.0.0-py3-none-any.whl:

Publisher: pypi-publish.yml on aialok/lakhua

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page