Top Banner
Geometry on the Sphere: Google's S2 Library Octavian Procopiuc [email protected]
18

Geometry on the Sphere_ Google's S2 Library

Jan 17, 2016

Download

Documents

Jerry Xu

Google GEO library
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Geometry on the Sphere_ Google's S2 Library

Geometry on the Sphere: Google's S2 Library

Octavian Procopiuc [email protected]

Page 2: Geometry on the Sphere_ Google's S2 Library

Library Overview

• C++ library, open source (Apache License 2) • Designed and written by Eric Veach

• Basic representations of lat/lng points and 3d vectors • Shapes on the unit sphere: o caps, o lat/lng rectangles, o polygons, polygonal lines.

• Hierarchical decomposition of the sphere into "cells". • Ability to approximate regions using cells.

Page 3: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy

• Hierarchical division of the sphere. • Goals: o Enough resolution for indexing geographic features o Compact representation of each cell o Fast methods for querying with arbitrary regions o All cells at a given level should have similar area.

• One solution: Quad-tree.

Page 4: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

Overview: • Enclose sphere in cube

[-1,1] x [-1,1] x [-1,1] • Project p on the cube • Build a quad-tree on each

cube face • Find quad-tree cell that

contains the projection of p Step 1: p=(lat,lng) => (x,y,z)

p

Page 5: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

• Step 2:(x,y,z) => (face,u,v)

• Problem: same-area cells on

the cube have different sizes on the sphere. Ratio of highest to lowest area: 5.2

Page 6: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

• Solution: non-linear transform (face,u,v) => (face,s,t)

• s,t in [0,1]

Page 7: Geometry on the Sphere_ Google's S2 Library

An Aside - Projection Trade-offs

• Choices for the (u,v) => (s,t) projection. o Linear: fast, but cell sizes vary widely o Tangent: uses atan() to make sizes more uniform; slow o Quadratic: much faster and almost as good as tangent.

Area Ratio Cell -> Point Point -> Cell

Linear 5.20 0.087 0.085 Tangent 1.41 0.299 0.258 Quadratic 2.08 0.096 0.108

microseconds

Page 8: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

Story so far: • (lat,lng) • (x,y,z) • (face,u,v) • (face,s,t) Discretize (s,t) • (face,i,j) Quad-tree cell: most significant bits of i and j

one of 6 faces

Page 9: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

Last step: (face,i,j)=> S2CellId [ID is a 64-bit integer] Enumerate cells along a Hilbert space-filling curve • fast to encode and

decode (bit flipping) • preserves spatial

locality

one of 6 faces

Page 10: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

one of 6 faces

Last step: (face,i,j)=> S2CellId [ID is a 64-bit integer] Enumerate cells along a Hilbert space-filling curve • fast to encode and

decode (bit flipping) • preserves spatial

locality

Page 11: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

S2 Cell ID of a leaf cell (level 30):

face (in [0,5])

64 bits

position along the Hilbert curve on the [0,230-1] x [0,230-1] grid (60 bits)

1

Page 12: Geometry on the Sphere_ Google's S2 Library

S2 Cell Hierarchy - Construction

S2 Cell ID of a level-2 cell:

face (in [0,5])

64 bits

position along the Hilbert curve on the [0,22-1] x [0,22-1] grid (4 bits)

0 1 0 0 0 0 0 0 0 0 0 0

Page 13: Geometry on the Sphere_ Google's S2 Library

One S2 Cell

Id: 0x89ace41000000000 (0b1000100110101100111001000001000...), Level: 12

Page 14: Geometry on the Sphere_ Google's S2 Library

S2 Cells - Stats

Level Min Area Max Area

0 85,011,012 km2 85,011,012 km2

1 21,252,753 km2 21,252,753 km2

12 3.31 km2 6.38 km2

30 0.48 cm2 0.93 cm2

smallest cell

Every cm2 on Earth can be represented using a 64-bit integer.

Page 15: Geometry on the Sphere_ Google's S2 Library

Approximating Regions Using S2 Cells

Given a region, find a (small) set of cells that cover it. Parameters: • max number of cells • max cell level • min cell level

Max # cells

Median ratio (covering area /

region area)

Worst ratio

4 3.31 15.83

8 1.98 4.03

20 1.42 1.94

100 1.11 1.19

default

Page 16: Geometry on the Sphere_ Google's S2 Library

What Else Is In the Library

• CCW: Given three points on the sphere, are they counter-clockwise? o Multiple implementations, with various tradeoffs.

• Polygons o Containment, intersection, union, difference, simplification,

centroid computation, etc. o Serialization.

• Polygonal lines, Spherical caps. • Extensive tests and micro-benchmarks.

Page 17: Geometry on the Sphere_ Google's S2 Library

Other Similar Libraries

• Hierarchical Triangular Mesh (http://skyserver.org/HTM).The lat/lng <-> triangle id conversion is ~100 slower than the lat/lng <-> s2 cell id conversion.

• HEALPix (http://healpix.jpl.nasa.gov). Cell boundaries are

not geodesics; structure is more complicated. • COBE Quadrilateralized Spherical Cube (

http://lambda.gsfc.nasa.gov/product/cobe/skymap_info_new.cfm). Similar decomposition of sphere. But does not use space-filling curve, edges are not geodesics, and projection is more complicated.

Page 18: Geometry on the Sphere_ Google's S2 Library

The Code

http://code.google.com/p/s2-geometry-library/