Expand description
kevy-geo — pure-Rust, zero-dependency primitives for Redis-style
GEO commands. Provides geohash encoding (score-form, used as the ZSet
score that backs every GEO key in kevy) and the standard 11-character
base32 string geohash, plus the WGS84 great-circle distance kevy needs
for GEODIST / GEOSEARCH BYRADIUS. Implementation deliberately
matches Redis bit-for-bit so kevy GEO keys are wire-interchangeable
with valkey clients.
What it is NOT:
- A full geo library — no projection conversions, no datums other than
WGS84, no path/intersection geometry, no R-tree spatial index. The
Redis-style GEO API is intentionally narrow; this crate matches that
narrowness rather than trying to be
projorgeo-types. - A
no_stdcrate — usesf64::sqrt/sin/cos/atan2fromstd.
Constants§
- EARTH_
RADIUS_ METERS - Mean great-circle Earth radius in metres, matching Redis’s constant
exactly (
6_372_797.560_856). Used byhaversine_meters. - GEO_
LAT_ MAX - Inclusive latitude bound (degrees).
- GEO_
LAT_ MIN - Inclusive latitude bound (degrees). Matches Redis’s Web Mercator limit — the encoding cannot represent the poles because Web Mercator maps them to ±∞.
- GEO_
LON_ MAX - Inclusive longitude bound (degrees).
- GEO_
LON_ MIN - Inclusive longitude bound (degrees).
- GEO_
STEP - Bits per axis in the 52-bit interleaved score. Matches Redis.
Functions§
- decode_
score - Inverse of
encode_score: decode a ZSet score back to the(longitude, latitude)centre of its geohash cell. Out-of-range scores saturate to the WGS84 bounds rather than producing garbage. - encode_
base32_ geohash - Encode
(lon, lat)as the 11-character base32 geohash string Redis returns fromGEOHASH. Uses the standard lat range [-90, 90] (NOT the WGS84 ±85.05 range used for the score). The high 55 bits of a step-26 standard-range encoding are emitted in 5-bit groups; the low 3 bits of the 11th char are always zero (52 ÷ 5 = 10 r 2). - encode_
score - Encode
(longitude, latitude)as the 52-bit interleaved geohash stored as a ZSet score. ReturnsNoneif either coordinate is out of WGS84 range. The score is a non-negative integer ≤ 2⁵² so its f64 representation is exact (within the 53-bit f64 mantissa). - haversine_
meters - Great-circle distance in metres between two
(longitude, latitude)points on the WGS84 sphere (mean radius — matches Redis). Returns0.0if the inputs are equal. - neighbor_
score_ ranges - Compute up to 9 ZSet-score ranges (closed-inclusive
(min, max)as f64-encoded 52-bit integers) that cover at least the disc ofradius_mmetres around(lon, lat). Each range maps a step-kgeohash cell to its contiguous score interval in the step-26 layout.