Problem
Let's say I know the Hilbert Curve Face and Quadtree, such as 4/032212303102122
(face 4, level 15).
Or perhaps I know the S2 Geometry CellId, such as 9749618424903892992
.
How can I convert from the one to the other?
Application
(this is the kind of thing you need to do for Pokemon GO and Ingress maps)
Exploration
I'm trying to do this in JavaScript and a library exists for manipulating 64-bit integers (long.js
) as well as for S2CellIds (s2-geometry.js
).
Also, I'm feeling pretty good about walking the hilbert curve simply by adding or subtracting the base four numbers (except when crossing faces, but that happens rarely enough that I'll be fine... for a while...), just not sure how to go back and forth with the 64-bit id.
It turns out that it's much, much, much easier to do it with strings than with binary - and since this is JavaScript where bitshifting with the
long.js
would take significantly more time, it's actually faster!Code Example:
From
s2-geometry-javascript
:Explanation:
Here's a quick 'n' dirty breakdown of the bits
id encoding
Note that + means concat and NOT add
(padding + face bits) + (padding + position bits) + (lsb marker + padding)
face encoding
position encoding
level encoding