More developers should use S2 Geometry
S2 Geometry is Google’s library for indexing spherical geometry, like points and areas on the earth.
The OSM Express storage engine uses S2 to index all of OpenStreetMap, update it once a minute, and slice regions on-demand; all the Niantic products including Pokémon Go seem to use S2 to divide the game world into areas, so analyzing cells is useful for collecting objectives in-game.
S2 Geometry has also been dumbed down into the PMTiles version 3 design. Instead of a cube of six Hilbert curves approximating a sphere, PMTiles is a single faced Hilbert curve covering the Web Mercator square. The space-filling curve in both systems is key for representing large areas as compressed sets of cells.
Web Mercator is a flawed projection system that is very useful for making 2D maps limited to 85°, north and south; if you want to represent the entire globe you need a true discrete global grid system like S2.
Other DGGS:
- H3 - Hexagon cells. They look great for visualization, but do not nest perfectly into parent/child cells like S2.
- A5 - Similar to H3 but more consistent cell sizes.
S2 fits a sweet spot for indexing spatial data, not visualization or aggregate statistics. The Google reference implementation is in C++, which makes it difficult to use for web maps. Enter Peter Johnson’s s2js, a from-scratch implementation of S2 Geometry in pure JavaScript, with most major features such as RegionCoverer and boolean operations.
To see it in action, I built a S2js region coverer (source).
Try it out at bdon.github.io/s2js-demos/
This is inspired by the Sidewalk Labs Region Coverer that’s offline now. The s2js demo is a single-page app that works client-side. Other components include the Protomaps hosted basemap API, MapLibre GL JS, and the Terra Draw drawing library.