rust-s2
Rust port of Google's S2 spherical geometry library
Repository Health
Technical Analysis
rust-s2 is a Rust port of Google’s S2 geometry library, which represents geometry on the surface of a sphere rather than a flat plane — the model needed for accurate geographic computation. It provides the S2 cell hierarchy, cell IDs, cell unions, points, lat/lng, and region types used for spatial indexing and geometric queries over the globe.
Ported principally from the Go implementation and adapting to Rust idioms, the crate also includes the supporting one-, two-, and three-dimensional Cartesian (R1/R2/R3) and circular (S1) geometry modules the spherical layer is built on, with optional Serde support.
What You Get
- The S2 cell hierarchy with CellID, Cell, and CellUnion for spatial indexing
- Spherical geometry types: Point, LatLng, Region, and metrics
- Supporting R1/R2/R3 Cartesian and S1 circular geometry modules
- Region covering to approximate shapes with sets of S2 cells
- Optional Serde serialization for S2 values
Common Use Cases
- Indexing geographic points and regions with hierarchical S2 cell IDs
- Computing coverings of shapes for spatial search and bucketing
- Performing accurate distance and containment queries on the sphere
- Interoperating with systems that already use Google’s S2 cell scheme
Under The Hood
Architecture — The crate is organized by dimensional layer mirroring Google’s S2: src/r1/, src/r2/, src/r3/ provide one-, two-, and three-dimensional Cartesian primitives, src/s1/ covers circular geometry, and src/s2/ builds the spherical layer on top — Cell, CellID, CellUnion, LatLng, Point, Region, metrics, and the stuv cube-face projection. consts.rs holds shared constants and src/bin/ contains supporting binaries.
Tech Stack — Rust (edition 2024) using cgmath for vector/matrix math, libm and float_extras for portable floating-point, bigdecimal for exact predicate arithmetic, lazy_static for precomputed tables, and optional serde and rand. Default features enable serde and float_extras.
Code Quality — The port tracks the Go S2 library module-by-module (the README documents parity status per layer), giving it a well-understood reference implementation and test suite; coverage is tracked via Coveralls. Some advanced modules (edgeutil, predicates, Rect) are noted as in progress.
API Design — The API follows the established S2 naming (CellID, CellUnion, region covering), so anyone familiar with S2 in Go or C++ can transfer knowledge directly. The layered module structure is discoverable, though the underlying spherical-geometry concepts give it a moderate learning curve for newcomers.