jonatkins / s2-geometry-javascript

Porting Google's S2 Geometry Library to Javascript
31 stars 17 forks source link

How to convert Face,Quadtree to CellId? #9

Closed coolaj86 closed 8 years ago

coolaj86 commented 8 years ago

Is it possible to directly derive a CellId, such as 9749618446378729472 from the face,quadtree pair, such as 4/032212303102210?

face encoding

position encoding

level encoding

id encoding

Note that + means concat and NOT add

// id          9749618446378729472
// quadkey     4/032212303102210
// base 4      10    032212303102210                   1000000000000000
// base 10    100    001110100110110011010010100100    1000000000000000000000000000000

Note that + means concat and NOT add

// psuedo-code
// This WON'T work because JavaScript uses 31-bit integers (53-bit floats)
var faceBits = 3;
var maxLevel = 30;
var maxBits = (maxLevel * 2) + 1; // one bit is lost to the marker

var str = face.toString(2) + position.toString(2) + '1';
str = padRight(str, maxBits, '0');
//  4/032212303102210
//  4    032212303102210                    1000000000000000
// 100   0001110100110110011010010100100    1000000000000000000000000000000
Long.fromString('9749618446378729472', true, 10).toString('4');
'20131031212211021000000000000000'
Long.fromString('9749618446378729472', true, 10).toString('2');
'1000011101001101100110100101001001000000000000000000000000000000'

Expected:

9749618446378729472
20131031212211021000000000000000
1000011101001101100110100101001001000000000000000000000000000000
coolaj86 commented 8 years ago

Now works