#6 fix padding issue converting from Hilbert Quadkey to CellId

This commit is contained in:
AJ ONeal 2016-07-29 09:31:39 -04:00
parent e461dcfd5c
commit 113e375824
1 changed files with 18 additions and 9 deletions

View File

@ -320,6 +320,9 @@ S2.POS_BITS = (2 * S2.MAX_LEVEL) + 1; // 61 (60 bits of data, 1 bit lsb marker)
S2.fromFacePosLevel = function (faceN, posS, levelN) { S2.fromFacePosLevel = function (faceN, posS, levelN) {
var Long = exports.dcodeIO && exports.dcodeIO.Long || require('long'); var Long = exports.dcodeIO && exports.dcodeIO.Long || require('long');
var faceB;
var posB;
var bin;
if (!levelN) { if (!levelN) {
levelN = posS.length; levelN = posS.length;
@ -328,16 +331,22 @@ S2.fromFacePosLevel = function (faceN, posS, levelN) {
posS = posS.substr(0, levelN); posS = posS.substr(0, levelN);
} }
var posB = Long.fromString(posS, true, 4).toString(2); // 3-bit face value
faceB = Long.fromString(faceN.toString(10), true, 10).toString(2);
while (faceB.length < S2.FACE_BITS) {
faceB = '0' + faceB;
}
// 60-bit position value
posB = Long.fromString(posS, true, 4).toString(2);
while (posB.length < (2 * levelN)) { while (posB.length < (2 * levelN)) {
posB = '0' + posB; posB = '0' + posB;
} }
var bin = Long.fromString(faceN.toString(10), true, 10).toString(2);
while (bin.length < S2.FACE_BITS) { bin = faceB + posB;
bin = '0' + bin; // 1-bit lsb marker
}
bin += posB;
bin += '1'; bin += '1';
// n-bit padding to 64-bits
while (bin.length < (S2.FACE_BITS + S2.POS_BITS)) { while (bin.length < (S2.FACE_BITS + S2.POS_BITS)) {
bin += '0'; bin += '0';
} }
@ -362,10 +371,10 @@ S2.toKey = S2.fromId = S2.fromCellId = S2.toHilbertQuadkey = function (idS) {
// MUST come AFTER binstr has been left-padded with '0's // MUST come AFTER binstr has been left-padded with '0's
var lsbIndex = bin.lastIndexOf('1'); var lsbIndex = bin.lastIndexOf('1');
// substr(start, len) // substr(start, len)
// substring(start, end) // substring(start, end) // includes start, does not include end
var faceB = bin.substr(0, 3); var faceB = bin.substring(0, 3);
// posB will always be a multiple of 2 (or it's invalid) // posB will always be a multiple of 2 (or it's invalid)
var posB = bin.substring(4, lsbIndex); var posB = bin.substring(3, lsbIndex);
var levelN = posB.length / 2; var levelN = posB.length / 2;
var faceS = Long.fromString(faceB, true, 2).toString(10); var faceS = Long.fromString(faceB, true, 2).toString(10);