|
| 1 | +# lakhua (Python) |
| 2 | + |
| 3 | +Offline reverse geocoding for India, optimized for fast in-memory lookups using H3 indexes. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Offline-first**: No API keys, no network dependency |
| 8 | +- **In-memory lookup**: Singleton loader for efficient data management |
| 9 | +- **Parent-resolution fallback**: Automatic fallback from resolution 5 → 4 |
| 10 | +- **Type-safe**: Full type hints and dataclasses |
| 11 | +- **Modern Python**: Supports Python 3.8+ |
| 12 | +- **Fast**: In-memory map-based lookups with minimal overhead |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +pip install lakhua |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +```python |
| 23 | +from lakhua import geocode |
| 24 | + |
| 25 | +location = geocode(28.6139, 77.209) |
| 26 | + |
| 27 | +if location: |
| 28 | + print(location.city, location.state) |
| 29 | +``` |
| 30 | + |
| 31 | +## API Reference |
| 32 | + |
| 33 | +### Recommended top-level APIs |
| 34 | + |
| 35 | +- `geocode(lat, lon, options=None) -> GeocodeResult | None` |
| 36 | +- `geocode_h3(h3_index, options=None) -> GeocodeResult | None` |
| 37 | + |
| 38 | +These helpers use the default singleton geocoder internally. |
| 39 | + |
| 40 | +### Advanced class APIs |
| 41 | + |
| 42 | +- `ReverseGeocoder.get_instance()` |
| 43 | +- `DataLoader.get_instance()` |
| 44 | + |
| 45 | +Use these only when you need explicit control in advanced runtime or testing scenarios. |
| 46 | + |
| 47 | +### `GeocodeOptions` |
| 48 | + |
| 49 | +```python |
| 50 | +@dataclass |
| 51 | +class GeocodeOptions: |
| 52 | + resolution: int = 5 # H3 resolution for geocode(lat, lon) |
| 53 | + fallback: bool = True # Enable parent resolution fallback |
| 54 | + debug: bool = False # Print timing logs |
| 55 | +``` |
| 56 | + |
| 57 | +### `GeocodeResult` |
| 58 | + |
| 59 | +```python |
| 60 | +@dataclass |
| 61 | +class GeocodeResult: |
| 62 | + city: str |
| 63 | + state: str |
| 64 | + district: str | None |
| 65 | + pincode: str | None |
| 66 | + matched_h3: str |
| 67 | + matched_resolution: int |
| 68 | +``` |
| 69 | + |
| 70 | +Returns `None` for invalid inputs or when no match is found. |
| 71 | + |
| 72 | +## Examples |
| 73 | + |
| 74 | +### Coordinate lookup |
| 75 | + |
| 76 | +```python |
| 77 | +from lakhua import geocode |
| 78 | + |
| 79 | +result = geocode(12.9716, 77.5946) |
| 80 | +print(result) |
| 81 | +``` |
| 82 | + |
| 83 | +### Direct H3 lookup |
| 84 | + |
| 85 | +```python |
| 86 | +from lakhua import geocode_h3 |
| 87 | + |
| 88 | +result = geocode_h3("8560145bfffffff") |
| 89 | +print(result) |
| 90 | +``` |
| 91 | + |
| 92 | +### Debug mode |
| 93 | + |
| 94 | +```python |
| 95 | +from lakhua import geocode, GeocodeOptions |
| 96 | + |
| 97 | +result = geocode(19.076, 72.8777, GeocodeOptions(debug=True)) |
| 98 | +print(result) |
| 99 | +``` |
| 100 | + |
| 101 | +### Custom resolution |
| 102 | + |
| 103 | +```python |
| 104 | +from lakhua import geocode, GeocodeOptions |
| 105 | + |
| 106 | +result = geocode( |
| 107 | + 28.6139, 77.2090, |
| 108 | + GeocodeOptions(resolution=4, fallback=False) |
| 109 | +) |
| 110 | +``` |
| 111 | + |
| 112 | +## Performance Notes |
| 113 | + |
| 114 | +- Data files are loaded once per process into memory |
| 115 | +- Lookups are map-based in-memory operations |
| 116 | +- Fallback adds up to two additional parent checks (5 → 4) when enabled |
| 117 | +- Average lookup time: < 1ms (in-memory) |
| 118 | + |
| 119 | +## Project Layout |
| 120 | + |
| 121 | +```text |
| 122 | +lakhua/ |
| 123 | + core/ |
| 124 | + __init__.py |
| 125 | + constants.py |
| 126 | + data_loader.py |
| 127 | + geocoder.py |
| 128 | + types.py |
| 129 | + __init__.py |
| 130 | + data/ |
| 131 | + reverse_geo_4.json |
| 132 | + reverse_geo_5.json |
| 133 | +``` |
| 134 | + |
| 135 | +## Development |
| 136 | + |
| 137 | +### Setup |
| 138 | + |
| 139 | +```bash |
| 140 | +pip install -e ".[dev]" |
| 141 | +``` |
| 142 | + |
| 143 | +### Run tests |
| 144 | + |
| 145 | +```bash |
| 146 | +pytest |
| 147 | +``` |
| 148 | + |
| 149 | +### Linting and formatting |
| 150 | + |
| 151 | +```bash |
| 152 | +ruff check . |
| 153 | +ruff format . |
| 154 | +``` |
| 155 | + |
| 156 | +### Type checking |
| 157 | + |
| 158 | +```bash |
| 159 | +mypy lakhua |
| 160 | +``` |
| 161 | + |
| 162 | +## License |
| 163 | + |
| 164 | +MIT |
| 165 | + |
0 commit comments