S2
shorturl.at/DIUV4
@dmarcous
“flat two-dimensional projections (similar to an atlas) on a
three-dimensional sphere (similar to a globe).”
“While the Earth is not quite spherical, it is much closer to
being a sphere than it is to being flat!”
What
Or is it ?!
No, it isn’t.
● Index geo data
○ [40.779717,-73.947738] is in cell : 89c258b
● Fast geo operations
○ Measuring distances
○ Computing centroids
○ Finding neighbours
● Aggregate geo data
○ {Cell : 89c258b, MAU : 10k}
● Awesome DS features (slides)
Why
How does it work?
● Flexible (arbitrary shapes)
● Fast (geo ops, en/decoding)
● Granular (user’s choice)
● Evenly sized (as much as possible)
● Easy to use (cubes, straight lines)
● Shortest paths based (spherical geodesics)
Design Principles
Project a cube onto a sphere
Faces
Quadtree
Each node has 4 children
Cells
Granularity (S2 statistics)
Hilbert Space Filling Curve
● Map points in multidimensional space to a one-dimensional space
● spatially similar objects are given close numeric indices
● Fractal
Planetary View
Cell Enumeration
● hierarchy (is cell x contained by cell y?)
● neighbour queries (return all adjacent cells on the same level)
O(1) Time & Space Complexity
O(1) is a constant number of ops, not dependant on N, but usually > 1.
That's still considered very fast for geo ops, especially neighbour queries.
Bit Operations
1. Projecting six faces of a cube onto the unit sphere
2. Partitioning each cell into four children recursively using a quadtree
3. For all of the cells of a given level :
a. Enumerate cell Ids along a hilbert space filling curve
Building a S2 cell hierarchy
● S2Builder
● S2ShapeIndex - Spatial Indexing
○ A.K.A “S2 Cover” / “Region Coverer”
○ Building a S2 cell hierarchy
Main Clases
Region Coverer
Hawaii = Collection of 22 Cells
Usage
Impl
google/s2geometry (cpp) - try installing on mac… I dare you...
sidewalklabs/s2sphere (Python)
dmarcous/S2Utilities (JVM) - functional
S2 Utilities
wicket - WKT
region-coverer - S2 Covers
Viz
● Growth Prediction
○ Learning “how many # (users/sessions/events) will I have in the future” on a cell level.
○ Given a future date & cell, what will be the #amount?
● Reverse Geocoding
○ A wrapper of S2ShapeIndex
○ Inclusion queries over coordinates
○ Given (longitude,latitude) what city (polygon) does it belong to?
Use Cases
Alternatives
Geohash H3 (uber)

S2

  • 1.
  • 2.
    “flat two-dimensional projections(similar to an atlas) on a three-dimensional sphere (similar to a globe).” “While the Earth is not quite spherical, it is much closer to being a sphere than it is to being flat!” What
  • 3.
    Or is it?! No, it isn’t.
  • 5.
    ● Index geodata ○ [40.779717,-73.947738] is in cell : 89c258b ● Fast geo operations ○ Measuring distances ○ Computing centroids ○ Finding neighbours ● Aggregate geo data ○ {Cell : 89c258b, MAU : 10k} ● Awesome DS features (slides) Why
  • 6.
  • 7.
    ● Flexible (arbitraryshapes) ● Fast (geo ops, en/decoding) ● Granular (user’s choice) ● Evenly sized (as much as possible) ● Easy to use (cubes, straight lines) ● Shortest paths based (spherical geodesics) Design Principles
  • 8.
    Project a cubeonto a sphere
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    Hilbert Space FillingCurve ● Map points in multidimensional space to a one-dimensional space ● spatially similar objects are given close numeric indices ● Fractal
  • 14.
  • 15.
  • 16.
    ● hierarchy (iscell x contained by cell y?) ● neighbour queries (return all adjacent cells on the same level) O(1) Time & Space Complexity O(1) is a constant number of ops, not dependant on N, but usually > 1. That's still considered very fast for geo ops, especially neighbour queries. Bit Operations
  • 17.
    1. Projecting sixfaces of a cube onto the unit sphere 2. Partitioning each cell into four children recursively using a quadtree 3. For all of the cells of a given level : a. Enumerate cell Ids along a hilbert space filling curve Building a S2 cell hierarchy
  • 18.
    ● S2Builder ● S2ShapeIndex- Spatial Indexing ○ A.K.A “S2 Cover” / “Region Coverer” ○ Building a S2 cell hierarchy Main Clases
  • 19.
  • 20.
  • 21.
  • 22.
    Impl google/s2geometry (cpp) -try installing on mac… I dare you... sidewalklabs/s2sphere (Python) dmarcous/S2Utilities (JVM) - functional
  • 23.
  • 24.
  • 25.
    ● Growth Prediction ○Learning “how many # (users/sessions/events) will I have in the future” on a cell level. ○ Given a future date & cell, what will be the #amount? ● Reverse Geocoding ○ A wrapper of S2ShapeIndex ○ Inclusion queries over coordinates ○ Given (longitude,latitude) what city (polygon) does it belong to? Use Cases
  • 26.