S2 and H3

Updated June 3, 2026
M
Magic Magnets Team
7 min read

If you’ve explored Geohashes and Quad Trees, you might be thinking, "Great, grids of squares solve everything." But there's a huge geometric problem staring us in the face: The Earth is round, and squares are flat.

If you try to wrap a square grid around a sphere, the squares get warped. Near the equator, your grid squares might be perfect 1km x 1km blocks. But as you move toward the North Pole, those squares distort and shrink. This distortion causes massive headaches for engineers trying to calculate accurate distances at scale.

Enter Google’s S2 and Uber’s H3, two incredibly smart spatial indexing systems designed specifically for a spherical world.

Quiz Time

Why do square grids fail when applied directly to the Earth?

S2: The Mathematical Marvel

Google created S2 to solve the flat-square problem. Instead of drawing a grid on a 2D map, S2 wraps a giant cube around the Earth and projects the Earth's surface onto the six faces of that cube.

Then, it divides each face into smaller and smaller squares using a Quad Tree structure.

Why go through this trouble? Because this mathematical projection (using something called the Hilbert Curve) ensures that the area of the cells remains highly consistent, whether you are at the equator or the poles. S2 also converts the 2D coordinates of these cells into a single, highly efficient 64-bit integer.

When you use Google Maps to search for "coffee near me," the database isn’t doing complex spatial math. It’s comparing simple numbers. 64-bit integers are insanely fast for databases to index and search.

Quiz Time

How does S2 achieve consistent cell sizes across the globe?

Quiz Time

S2 represents spatial cells as 64-bit integers primarily to enable fast database indexing.

H3: The Power of Hexagons

Uber loved the idea of spatial indexing, but they found a flaw with squares.

Think about a square. It has four neighbors that share a flat edge (North, South, East, West) and four neighbors that share a pointy corner (the diagonals). The distance from the center of a square to its edge-neighbor is shorter than the distance to its corner-neighbor.

For a ride-sharing app estimating travel times and smoothing out pricing zones, this difference in distance introduces annoying mathematical errors.

Uber’s solution? Hexagons.

H3 covers the Earth in a grid of hexagons. A hexagon has a magical property: the distance from its center to the center of all six of its neighbors is exactly the same.

This uniform distance makes algorithms for calculating radius, smoothing out surge pricing, and tracking vehicle movement incredibly smooth and simple.

Quiz Time

What geometric property makes hexagons superior to squares for movement and radius calculations?

Like Geohash and S2, H3 is hierarchical. You can have a massive hexagon covering a city, and if you zoom in, it’s made up of smaller hexagons covering neighborhoods.

Quiz Time

H3 is hierarchical, meaning a large hexagon covering a city can be subdivided into smaller hexagons covering neighborhoods.

Which one should you use?

  • Use Geohash if you need something simple, lightweight, and string-based for a small-scale app.
  • Use S2 if you need pixel-perfect precision (down to a few millimeters) and want to use blazing-fast 64-bit integer indexing. It’s ideal for point-in-polygon queries.
  • Use H3 if you are analyzing movement, doing data science, or dealing with radiuses. The uniform neighbors make aggregating data (like "average surge price in this area") much cleaner.
Quiz Time

Which system is the best fit for a point-in-polygon query requiring millimeter-level precision?

Real-World Examples

  • Google Maps & Pokemon Go: Use S2 geometry to figure out where streets, buildings, and Pokemon spawn points are located in the real world.
  • Uber: Invented and open-sourced H3. They use it to calculate surge pricing, visualize ride demand, and track driver availability across regions.

Summary

When mapping a 3D globe, flat squares distort. S2 and H3 are advanced spatial indexing systems that solve this. Google's S2 projects the Earth onto a cube for highly consistent, fast integer-based searches. Uber's H3 uses hexagons, which provide uniform distances to neighbors, making it the superior choice for tracking movement, routing, and analyzing localized data.

Bloom Filters

How helpful was this content?

Comments

0/2000

Sign in to join the discussion

Saved on this device only

Sign in to sync progress across devices