Skip to content

pybind: Add S2Cell bindings - #631

Merged
jmr merged 12 commits into
google:masterfrom
deustis:deustis/s2cell_bindings
Jun 9, 2026
Merged

jmr merged 12 commits into
google:masterfrom
deustis:deustis/s2cell_bindings

Conversation

@deustis

@deustis deustis commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds pybind11 bindings for S2Cell, covering constructors, factory methods, properties, geometric operations, containment/intersection tests, distance queries, and traversal.

Naming: GetDistance is overloaded in C++ for S2Point, edge (a, b), and S2Cell arguments. Per the binding interface notes, the short name (get_distance) is used for the S2Point overload, and longer names (get_distance_to_edge, get_distance_to_cell) for the others. get_max_distance follows the same pattern.

Omitted::

  • _raw variants:** vertex_raw, edge_raw, and center_raw return unnormalized vectors intended for feeding into exact C++ predicates. Python callers won't do that, so these are dropped in favor of the normalized versions.
  • is_distance_less / is_distance_less_or_equal family is omitted — callers can use get_distance(...) < limit directly.
  • get_cap_bound and get_rect_bound are deferred until S2Cap and S2LatLngRect are bound.
  • Encode / Decode are intentionally omitted per the serialization policy in the pybind README.

(Part of a series addressing #522)

Test plan

bazel test //python/... — all suites pass.

deustis added 6 commits May 26, 2026 15:17
Binds S2Cell's constructors, factory methods, geometric accessors
(vertex, edge, center, area), boundary helpers, containment and
intersection predicates, and subdivide. Adds 43 unit tests.

Distance methods and the S2Cap/S2LatLngRect-returning region methods
are deferred with a TODO; they depend on S1ChordAngle/S2Cap/S2LatLngRect
bindings which are not yet in place.

(Stacked on deustis/s2cell_id_bindings / PR google#593.)
- Drop MaybeThrowNotValidCellId from S2Cell(S2CellId) constructor; all
  S2CellId objects reachable from Python are already validated
- Remove instance average_area(); keep only static average_area_for_level()
- Add S2CellId::kMaxPosition constant and validate pos in from_face_pos_level
  for both S2CellId and S2Cell bindings
- Switch constants to cls.attr() pattern in both S2CellId and S2Cell bindings;
  update README to document this style
- Fix BUILD: add absl/strings dep to s2cell_id_bindings
- Remove misleading encode/decode comment from s2cell_bindings.cc
- Fix magic constant 1<<30 -> 1<<S2CellId.MAX_LEVEL in s2cell_test.py
Consistent with other MaybeThrow* helpers in both binding files.
- Fix s2cell deps comment in module.cc to include s1chord_angle
- Clarify orientation docstring (bitmask, values 0-3)
- Drop _raw variants (vertex_raw, edge_raw, center_raw): Python callers
  don't feed unnormalized vectors into exact C++ predicates
@deustis

deustis commented May 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@jmr, I've done a few rounds of self/LLM review here. Happy to make any changes as usual.

Comment thread src/python/s2cell_bindings.cc Outdated
Comment thread src/python/s2cell_bindings.cc Outdated
Comment thread src/python/s2cell_bindings.cc Outdated
Comment thread src/python/s2cell_bindings.cc
Comment thread src/python/s2cell_bindings.cc
Comment thread src/python/s2cell_bindings.cc
@jmr

jmr commented May 29, 2026

Copy link
Copy Markdown
Member

Tests are failing.

deustis added 3 commits May 29, 2026 15:59
- Drop get_ prefix from all Python method names across S2Cell, S2CellId,
  and S2LatLng bindings; document convention in README
- Use py::enum_<S2Cell::Boundary> with py::arithmetic() instead of raw
  cls.attr() integer assignments
- Use absl::HashOf in __hash__; add absl/hash include and BUILD dep
- Add validity comment on S2LatLng constructor; comment duplicated helpers
- Add tests for all seven distance methods on S2Cell
@deustis
deustis requested a review from jmr May 29, 2026 16:15
@deustis

deustis commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Tests are failing.

Fixed

@deustis

deustis commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@jmr, this one is also ready for another review

@jmr
jmr merged commit 2c5771c into google:master Jun 9, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants