Skip to main content

Crate kevy_geo

Crate kevy_geo 

Source
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 proj or geo-types.
  • A no_std crate — uses f64::sqrt/sin/cos/atan2 from std.

Constants§

EARTH_RADIUS_METERS
Mean great-circle Earth radius in metres, matching Redis’s constant exactly (6_372_797.560_856). Used by haversine_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 from GEOHASH. 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. Returns None if 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). Returns 0.0 if 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 of radius_m metres around (lon, lat). Each range maps a step-k geohash cell to its contiguous score interval in the step-26 layout.