Use the S2.latLngToKey(lat,lon,level)
to obtain YourCoolValuekey=YourCoolValue of your point
The keykey
is like a base4 Geohash or a tile-quadkey. It is a string with the face
of the S2 Cube and face_pos
, the hierarchical position (base4) of the cell in the hierarchical (Hilbert) grid. The result of this example is
g Sample Cell_id_base16idHex Face/KeyFace_pos
--- Level 13
A1 1 Washington M: 89b7b7a400000000 4/1031233123310
A2 2 Josephine Sh: 89c259ac00000000 4/1032010230311
A3 3 Delacorte Th: 89c2589400000000 4/1032010230102
A3 4 Central Park: 89c2589c00000000 4/1032010230103
A3 5 Alexander Ha: 89c2589400000000 4/1032010230102
B 6 Merlion: 31da190c00000000 1/2032310030201
B 7 Merlion Park: 31da190c00000000 1/2032310030201
--- Level 20
A1 1 Washington M: 89b7b7a1bdf00000 4/10312331233100313233
A2 2 Josephine Sh: 89c259aa96100000 4/10320102303111102300
A3 3 Delacorte Th: 89c25891ac900000 4/10320102301020311210
A3 4 Central Park: 89c2589a08900000 4/10320102301031001010
A3 5 Alexander Ha: 89c2589747d00000 4/10320102301023220332
B 6 Merlion: 31da19085c300000 1/20323100302010023201
B 7 Merlion Park: 31da190860700000 1/20323100302010030003
The column Cell_id_base16idHex
is the standard hexadecimal (base16) representation of the S2 Geometry Cell identifier (cell ID). The column Face/Key
is an alternative representation of the same cell ID.
Each cell of the S2 Geometry Cell IDgrid have an unique number (64 bits unsigned integer) used to identify ifself, the internallythat is a 64structure of bits unsigned integerexpressing the position in the Hilbert curve (Face_pos) of a cube face.
You can use as geocode (said "MyCoolValue" in the question) any one, the Cell_id_base16idHex
or the key=Face/KeyFace_pos
.
Cell IDCell ID and cell KeyCell Key have the same information, but ID mix the face informationand face_pos, and Keykey not. Face is explicit and each digit of face_pos is a level in the pure cell positiongrid structure.
There are some (minor) advantages of Hilbert curve (S2) over Z-order curve (Geohash), but no one is perfect... S2 Geometry indexation system is not oriented to human-readable codes, if the prefix of to cells are not the same In both, it is not guaranteenpossible two neighbors in the grid that have identifiers (!keys). There are a chance that the points are neighbors and its keys very different.
About convert "Face/Key"Face_pos" to hexadecimal, is possible for example convert the base4 10002200
to hexa 40a0
, but base4 with more one digit will result in entirely different (or invalid) hexadecimal; for example 100022003
results in 102801
... To avoid this problem, an extra digit must be added by a extend base16 algorithm The "more one digit" ocurrs when you compare for example level 18 keys with level 19 keys.
To avoid this problem, an extra digit must be added by a extend base16 algorithm.
function base4_to_base16(str) {
const tr = {"0":"00","1":"01","2":"10","3":"11"}
let strBin=''
for(let i of str.split('')) strBin += tr[i]
return BigInt('0b'+strBin).toString(16)
}
function base4_to_base16h(str) {
const tr = {"0":"J","1":"K","2":"L","3":"M"}
let len = str.length
if (len % 2 == 0)
return base4_to_base16(str);
let h = base4_to_base16( str.slice(0,-1) ) // cut last
return h+tr[ str.slice(-1) ]
}
var key = S2.latLngToKey(i.lat, i.lon, level);
[face,face_pos] = key.split('/');
keyHex = face+base4_to_base16hface + base4_to_base16h(face_pos)
g Sample Face/Key Face_pos = keyHex bugKeyidHex
--- Level 18
A1 1 Washington M: 4/103123312331003132 = 44dbdbd0de 4dbdbd0de89b7b7a1bd000000
A2 2 Josephine Sh: 4/103201023031111023 = 44e12cd54b 4e12cd54b89c259aa97000000
A3 3 Delacorte Th: 4/103201023010203112 = 44e12c48d6 4e12c48d689c25891ad000000
A3 4 Central Park: 4/103201023010310010 = 44e12c4d04 4e12c4d0489c2589a09000000
A3 5 Alexander Ha: 4/103201023010232203 = 44e12c4ba3 4e12c4ba389c2589747000000
B 6 Merlion: : 1/203231003020100232 = 18ed0c842e 8ed0c842e31da19085d000000
B 7 Merlion Park: 1/203231003020100300 = 18ed0c8430 8ed0c843031da190861000000
--- Level 19 (compare bugKey here!)
A1 1 Washington M: 4/1031233123310031323 = 44dbdbd0deM 136f6f437b89b7b7a1bdc00000
A2 2 Josephine Sh: 4/1032010230311110230 = 44e12cd54bJ 1384b3552c89c259aa96400000
A3 3 Delacorte Th: 4/1032010230102031121 = 44e12c48d6K 1384b1235989c25891acc00000
A3 4 Central Park: 4/1032010230103100101 = 44e12c4d04K 1384b1341189c2589a08c00000
A3 5 Alexander Ha: 4/1032010230102322033 = 44e12c4ba3M 1384b12e8f89c2589747c00000
B 6 Merlion: : 1/2032310030201002320 = 18ed0c842eJ 23b43210b831da19085c400000
B 7 Merlion Park: 1/2032310030201003000 = 18ed0c8430J 23b43210c031da190860400000
--- Level 22
A1 1 Washington M: 4/1031233123310031323331 = 44dbdbd0defd 4dbdbd0defd89b7b7a1bdfb0000
A2 2 Josephine Sh: 4/1032010230311110230000 = 44e12cd54b00 4e12cd54b0089c259aa96010000
A3 3 Delacorte Th: 4/1032010230102031121011 = 44e12c48d645 4e12c48d64589c25891ac8b0000
A3 4 Central Park: 4/1032010230103100101000 = 44e12c4d0440 4e12c4d044089c2589a08810000
A3 5 Alexander Ha: 4/1032010230102322033211 = 44e12c4ba3e5 4e12c4ba3e589c2589747cb0000
B 6 Merlion: : 1/2032310030201002320132 = 18ed0c842e1e 8ed0c842e1e31da19085c3d0000
B 7 Merlion Park: 1/2032310030201003000332 = 18ed0c84303e 8ed0c84303e31da1908607d0000
The idHex
can be reduced with the trailing 0's removed (e.g. 89b7b7a1bd000000
reduced to 89b7b7a1bd
). The information into keyHex
and idHex
are same, differing only in one bit.
The keyHex
is better: represents the same cell with the same number of digits, but with a evident relationship with "Face/Face_pos" and always preserving prefix, with no distortions. The hexadecimal (base16) representation is aligned with the base4.
Notice the folowing idHex
in the table, to see the distortions:
- sample A1 (no distorcion). Level 18 is
89b7b7a1bd
and level 19 is89b7b7a1bdc
, so concatenating one digitc
. - sample A2 (distorcion). Level 18 is
89c259aa97
and level 19 is89c259aa964
, changed final7
to6
and concatenate4
.