s2sphere

0.2.5 · active · verified Thu Apr 16

s2sphere is a Python implementation of a part of Google's C++ S2 Geometry Library. It provides tools for working with spherical geometry, such as mapping points and regions on a sphere to a 1D index, enabling scalable proximity searches on distributed indexes. The current version is 0.2.5, and it maintains an active release cadence, primarily via updates to its GitHub repository and PyPI package. [1, 2, 3]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `s2sphere` to find S2 cells covering a geographical region defined by two latitude-longitude points. It then shows how to convert one of these cell IDs back to its center latitude and longitude. [1, 3]

import s2sphere

# Define two points using latitude and longitude
p1 = s2sphere.LatLon.from_degrees(33, -122)
p2 = s2sphere.LatLon.from_degrees(33.1, -122.1)

# Create a region coverer
r = s2sphere.RegionCoverer()

# Get the S2 cells covering a LatLon rectangle defined by the two points
cell_ids = r.get_covering(s2sphere.LatLonRect.from_point_pair(p1, p2))

# Print the resulting cell IDs
print(f"S2 Cell IDs covering the region: {cell_ids}")

# Example: Convert a CellId to LatLon
if cell_ids:
    first_cell_id = cell_ids[0]
    center_latlon = s2sphere.LatLng.from_point(s2sphere.Cell(first_cell_id).get_center())
    print(f"Center of the first cell ({first_cell_id.to_token()}): {center_latlon.degrees()}, {center_latlon.lngdegrees()}")

view raw JSON →