Google S2 Cells

From NoskeWiki
Jump to navigation Jump to search

About

NOTE: This page is a daughter page of: Google and Google Maps


Google S2 Cells are a geospatial indexing system developed by Google. They represent the Earth’s surface as a hierarchy of cells, which allows for efficient querying, mapping, and global-scale data analysis. S2 Cells are used in Google Maps, geospatial search systems, and various scientific and commercial applications.

S2 Cells divide the Earth into a cube-to-sphere projection with six base faces. Each face is recursively subdivided into smaller cells, which can approximate the Earth’s curved surface. Importantly, the cells are not squares. Think of them as spherical polygons—approximately square-shaped on the curved surface of a sphere. Each cell has four edges, but those edges are geodesics (shortest paths on the sphere), so they appear slightly curved when projected onto a flat map.

Overview of how S2 cells work

To give you an idea of size... at:

  • level 15, the cells are approximately 500 meters across,
  • level 20 is ~10 m, and
  • level 30 is ~1 cm!

... so for most urban-scale geospatial analysis, you wouldn't need to go beyond ~17 levels (~150 m) deep. The beauty of a hierarchical system, is you only need to break down the cells into smaller parts where there is lots of details, so for most applications you wouldn't need to go very deep over oceans.

First level of cells


Technical Details

For detailed technical information, see the S2 Cell documentation.

One thing that makes S2 cells efficient is that they are ordered in memory using a space-filling curve called the Hilbert Curve, increasing the chance that spatially close points/roads/buildings are nearby in memory also.

An illustration of how the Hilbert Curve works on S2 cells at ~level 4.

Competitors

S2 Cells compete with other geospatial indexing systems, including:

  • H3 (Hexagonal Hierarchical Spatial Index): Developed by Uber, uses hexagonal cells and is ideal for city-scale and mobility analysis.
  • Geohash: Encodes latitude and longitude into a string, offering simple but less precise grid-based indexing.
  • Quadtree-based Systems: Frequently used in game development and spatial data management, but usually just basic squares - not for a spherical earth.

Each system has unique strengths, but S2 Cells are particularly notable for their hierarchical flexibility and ability to represent the Earth's curvature. S2 is the most popular system for hierarchical applications.

Supported Languages

Libraries for working with S2 Cells are available in multiple languages:

  • C++: Official implementation from Google.
  • Python: Bindings for geospatial queries and analysis.
  • Java: Common in backend systems.
  • Go: Used in modern microservices.
  • JavaScript/TypeScript: Suitable for frontend geospatial applications.
  • Rust: For performance-critical tasks.

Links