← Lessons

quiz vs the machine

Gold1460

System Design

The S2 Cell Hierarchy

Projecting the sphere onto a cube and numbering cells along a space filling curve.

5 min read · core · beat Gold to climb

What S2 does

The S2 system projects the Earth onto the six faces of a cube, then recursively subdivides each face into four. Cells are numbered along a Hilbert space filling curve, which gives every cell a single 64 bit id where nearby ids tend to mean nearby cells.

Why it beats a flat grid

  • Low distortion. The cube projection keeps cell areas more uniform than a plain latitude and longitude grid, which warps badly near the poles.
  • Multiple levels. A region can be covered by a small set of cells at mixed resolutions, large cells in the middle and small ones along the edges.
  • One dimensional ids. Because the Hilbert curve preserves locality, range scans on the id approximate spatial ranges.

Covering a region

A region cover picks the fewest cells that fully contain a shape within level limits. That cover becomes a set of key ranges you can scan in an ordinary sorted index.

Key idea

S2 maps the sphere to cube faces and a Hilbert curve so spatial proximity becomes proximity of compact integer cell ids.

Check yourself

Answer to earn rating on the learn ladder.

1. What shape does S2 project the Earth onto before subdividing?

2. Why does the Hilbert curve numbering help?