
Google’s S2, geometry on the sphere, cells and Hilbert curve - perone
http://blog.christianperone.com/?p=2840&a=2
======
jacobolus
The linked presentation slides give a bit better explanation IMO:
[https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-
pSn...](https://docs.google.com/presentation/d/1Hl4KapfAENAOf4gv-
pSngKwvS_jwNVHRPZTTDzXXn6Q/view?pli=1)

Too bad there aren’t more general docs for the library.

I still don’t quite understand the benefit of using a single number instead of
a pair of numbers for describing a cell in a two-dimensional space. Seems like
a pair of 32-bit fixed point numbers would be just as descriptive as a single
double-precision float along the hilbert curve.

The way they project the sphere onto a cube also leads to cell shapes that
aren’t especially relevant to typical human purposes, whereas there are many
shapes on a map which align with the latitude/longitude grid.

~~~
danbruc
The purpose of the space filling curve is to preserve locality when you store
the two dimensional data on a one dimensional storage medium. If you used x
and y coordinates and stored the data like an image row by row, then data of
cells adjacent in the x direction would get stored close together but data of
cells adjacent in the y direction would get stored quite a bit away because of
the other cells of the rows in between. On the other hand the Hilbert curve
keeps cells adjacent in both directions closer together and therefore
preserves locality better which in turn means less read head repositioning on
spinning disks or a better hit probability when you prefetch adjacent data.

~~~
david-given
It also allows you to do the opposite, and turn a single-dimensional quantity
into two-dimensional coordinates in a useful manner.

The most famous example I know is, of course, XKCD's Map of the Internet:

[https://xkcd.com/195/](https://xkcd.com/195/)

------
dnautics
Correct me if I'm wrong but isn't locality unidirectional? Two close points in
the plane might by chance be encoded far away on the line if they're unluckily
next to a seam.

~~~
guard-of-terra
Theoretically you can try and find projection where most major seams are in
un-navigable oceans and it won't hurt that much.

My take is that they should allow height to be coded in too.

~~~
techdragon
I had the fortune to be introduced to this by a Google employee and while they
were not from the maps team, they had soaked up enough of the general Google
way of doing things that their explanation was able to "hit home" very
quickly.

As a result one of the first things I asked them was "where are the corners?"
and they confirmed my suspicions that they have "tilted" the cube slightly
(the vertical edges are not parallel to meridians at the equator) and that a
very rough approximate location for two of the corners is one is roughly in
the great Australian bight on the South Australian coast line, and another one
is roughly in the sea of Okhotsk between Japan and the far eastern half of
Siberian region of Russia.

TL;DR. Yes, yes they do try to put the vertices in areas where their
distortion will be least noticed.

------
wfunction
Would someone please explain to me how you one convert between the 1D and 2D
coordinates of a Hilbert curve? Is there a formula for it? The drawings look
nice but they don't tell you how to actually do the conversion, which seems to
be the crucial piece of the data structure.

~~~
jonas21
Here is Google's code for doing it:

[https://code.google.com/p/s2-geometry-
library/source/browse/...](https://code.google.com/p/s2-geometry-
library/source/browse/geometry/s2cellid.cc#205)

The lookup table it references is generated up here:

[https://code.google.com/p/s2-geometry-
library/source/browse/...](https://code.google.com/p/s2-geometry-
library/source/browse/geometry/s2cellid.cc#28)

~~~
wfunction
Thanks!

------
crazy1van
Is it faster for spatial queries to store the S2 cells in a Btree index in a
database like the article mentions or to just use something like PostGIS with
its gist spatial indices -- specifically, for determining whether points are
inside or outside of a polygon?

------
eveningcoffee
I think this is also a great example how Google abandoning the Google Code
will affect the availability of previously published work.

What I want to tell is that this library is not (yet according to some
googling) migrated to the new platform even when it is from people from
Google.

------
jkot
There is also Healpix and other libraries

------
dsymonds
There's also a Go port of S2:
[http://godoc.org/github.com/golang/geo/s2](http://godoc.org/github.com/golang/geo/s2)

~~~
logician76
Yeah, it's really awesome. However not complete, but I've added a lot of
functionality especially the coverer, there's a pull request for it if you
want to take a look.

------
thrownaway2424
[http://webcache.googleusercontent.com/search?q=cache:AhGDevV...](http://webcache.googleusercontent.com/search?q=cache:AhGDevVVT_MJ:blog.christianperone.com/%3Fp%3D2840&hl=en&gl=us&strip=1&vwsrc=0)

~~~
perone
Thanks. I solved the problem, lots of visits and missing swap space lol.

