A Rust port of Google's S2 Geometry library, with optional Python bindings (via PyO3) and a WebAssembly target.
The provided software should be considered beta, and should not be used in production.
S2 is a library for spherical geometry — geometry on the surface of a sphere. It provides exact, efficient representations of points, regions, and shapes on the sphere, and supports operations like containment tests, set operations, nearest-neighbour queries, and discrete cell decomposition over a hierarchical quad-tree (the S2 cell hierarchy).
| Path | Crate | Description |
|---|---|---|
core/ |
s2rst |
The Rust core library. |
python/ |
s2rst-python |
Python bindings, exposed as the s2rst module. |
wasm/ |
s2rst-wasm |
WebAssembly bindings. |
The Rust core builds with stable Rust:
cargo build --release -p s2rst
cargo test --release -p s2rst
The Python package is built with maturin:
cd python
uv sync --extra test --group dev
uv run maturin develop --release
uv run pytest
import s2rst
# Create a point on the sphere from latitude/longitude (in degrees).
ll = s2rst.LatLng.from_degrees(37.7749, -122.4194)
p = ll.to_point()
# Find the leaf cell containing the point.
cell = s2rst.CellId.from_point(p)
print(cell.to_token()) # hex token, e.g. "808f7c"
print(cell.level()) # 30 (leaf)
# Walk up to a coarser level.
parent = cell.parent_at_level(10)
print(parent.to_debug_string()) # face/childpath form
# Build a circular cap (disc) of 10 km angular radius around the point.
radius = s2rst.Angle.from_radians(10.0 / 6371.0) # 10 km / earth radius
cap = s2rst.Cap.from_center_angle(p, radius)
print(cap.area()) # steradiansThe Python package ships type stubs (py.typed + _core.pyi) and is
recognised as a typed package by mypy and pyright.
Licensed under the Apache License, Version 2.0, the same license as the upstream S2 geometry library this project is derived from.
s2rst is an independent port and is not affiliated with, endorsed by, or sponsored by Google.