nomadro/apps/api/services/social_svc/geo_h3.py
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 02:25:05 +08:00

26 lines
948 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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)