"""H3 cell helpers — soft-dep on `h3` package; rounded fallback otherwise.""" from __future__ import annotations from .config import H3_CITY_RES, H3_NEAR_RES try: import h3 # type: ignore def latlng_to_cell(lat: float, lng: float, res: int | None = None) -> str: r = H3_CITY_RES if res is None else res return str(h3.latlng_to_cell(float(lat), float(lng), int(r))) def near_cell(lat: float, lng: float) -> str: return latlng_to_cell(lat, lng, H3_NEAR_RES) except Exception: # pragma: no cover def latlng_to_cell(lat: float, lng: float, res: int | None = None) -> str: r = H3_CITY_RES if res is None else res # Coarse grid fallback (~res 4–5 order of magnitude) step = 0.5 if r <= 4 else 0.2 if r <= 5 else 0.05 return f"g{r}:{round(float(lat) / step) * step:.4f}:{round(float(lng) / step) * step:.4f}" def near_cell(lat: float, lng: float) -> str: return latlng_to_cell(lat, lng, H3_NEAR_RES)