further cleanup and testing for #16
This commit is contained in:
parent
5495dd0a53
commit
3a962c1956
16
README.md
16
README.md
|
@ -118,6 +118,13 @@ var key = S2.idToKey(id);
|
||||||
// '9749618446378729472'
|
// '9749618446378729472'
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Convert between Quadkey and Id
|
||||||
|
//
|
||||||
|
var latlng = S2.keyToLatLng(key);
|
||||||
|
var latlng = S2.idToLatLng(id);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Neighbors
|
// Neighbors
|
||||||
|
@ -198,3 +205,12 @@ var hilbertQuadkey = S2.idToKey(cellId);
|
||||||
|
|
||||||
console.log(hilbertQuadkey);
|
console.log(hilbertQuadkey);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Convert Key and Id to LatLng
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var latlng = S2.keyToLatLng('4/032212303102210');
|
||||||
|
|
||||||
|
var latlng = S2.idToLatLng('9749618446378729472');
|
||||||
|
```
|
||||||
|
|
|
@ -272,50 +272,51 @@ S2.S2Cell.FromHilbertQuadKey = function(hilbertQuadkey) {
|
||||||
var point = {
|
var point = {
|
||||||
x : 0,
|
x : 0,
|
||||||
y: 0
|
y: 0
|
||||||
}
|
};
|
||||||
|
var i;
|
||||||
|
var level;
|
||||||
|
var bit;
|
||||||
|
var rx, ry;
|
||||||
|
var val;
|
||||||
|
|
||||||
for(var i = maxLevel - 1; i >= 0; i--)
|
for(i = maxLevel - 1; i >= 0; i--) {
|
||||||
{
|
|
||||||
|
level = maxLevel - i;
|
||||||
var level = maxLevel - i;
|
bit = position[i];
|
||||||
var bit = position[i];
|
rx = 0;
|
||||||
var rx = 0, ry = 0;
|
ry = 0;
|
||||||
if(bit == '1')
|
if (bit === '1') {
|
||||||
{
|
|
||||||
ry = 1;
|
ry = 1;
|
||||||
}
|
}
|
||||||
else if(bit == '2')
|
else if (bit === '2') {
|
||||||
{
|
|
||||||
rx = 1;
|
rx = 1;
|
||||||
ry = 1;
|
ry = 1;
|
||||||
}
|
}
|
||||||
else if(bit == '3')
|
else if (bit === '3') {
|
||||||
{
|
|
||||||
rx = 1;
|
rx = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var val = Math.pow(2, level - 1)
|
val = Math.pow(2, level - 1);
|
||||||
rotateAndFlipQuadrant(val, point, rx, ry);
|
rotateAndFlipQuadrant(val, point, rx, ry);
|
||||||
|
|
||||||
point.x += val * rx;
|
point.x += val * rx;
|
||||||
point.y += val * ry;
|
point.y += val * ry;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(face % 2 == 1)
|
if (face % 2 === 1) {
|
||||||
{
|
|
||||||
var t = point.x;
|
var t = point.x;
|
||||||
point.x = point.y;
|
point.x = point.y;
|
||||||
point.y = t;
|
point.y = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return S2.S2Cell.FromFaceIJ(parseInt(face),[point.x, point.y],level)
|
return S2.S2Cell.FromFaceIJ(parseInt(face), [point.x, point.y], level);
|
||||||
|
};
|
||||||
|
|
||||||
}
|
|
||||||
//static method to construct
|
//static method to construct
|
||||||
S2.S2Cell.FromLatLng = function(latLng, level) {
|
S2.S2Cell.FromLatLng = function(latLng, level) {
|
||||||
if (latLng.lat == null || latLng.lng == null) {
|
if ((!latLng.lat && latLng.lat !== 0) || (!latLng.lng && latLng.lng !== 0)) {
|
||||||
throw new Error("Pass { lat: lat, lng: lng } to S2.S2Cell.FromLatLng");
|
throw new Error("Pass { lat: lat, lng: lng } to S2.S2Cell.FromLatLng");
|
||||||
}
|
}
|
||||||
var xyz = S2.LatLngToXYZ(latLng);
|
var xyz = S2.LatLngToXYZ(latLng);
|
||||||
|
@ -327,6 +328,7 @@ S2.S2Cell.FromLatLng = function(latLng, level) {
|
||||||
|
|
||||||
return S2.S2Cell.FromFaceIJ (faceuv[0], ij, level);
|
return S2.S2Cell.FromFaceIJ (faceuv[0], ij, level);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
S2.faceIjLevelToXyz = function (face, ij, level) {
|
S2.faceIjLevelToXyz = function (face, ij, level) {
|
||||||
var st = S2.IJToST(ij, level, [0.5, 0.5]);
|
var st = S2.IJToST(ij, level, [0.5, 0.5]);
|
||||||
|
@ -517,8 +519,21 @@ S2.idToKey = S2.S2Cell.idToKey
|
||||||
return faceS + '/' + posS;
|
return faceS + '/' + posS;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
S2.keyToLatLng = S2.S2Cell.keyToLatLng = function (key) {
|
||||||
|
var cell2 = S2.S2Cell.FromHilbertQuadKey(key);
|
||||||
|
return cell2.getLatLng();
|
||||||
|
};
|
||||||
|
|
||||||
|
S2.idToLatLng = S2.S2Cell.idToLatLng = function (id) {
|
||||||
|
var key = S2.idToKey(id);
|
||||||
|
return S2.keyToLatLng(key);
|
||||||
|
};
|
||||||
|
|
||||||
S2.S2Cell.latLngToKey = S2.latLngToKey
|
S2.S2Cell.latLngToKey = S2.latLngToKey
|
||||||
= S2.latLngToQuadkey = function (lat, lng, level) {
|
= S2.latLngToQuadkey = function (lat, lng, level) {
|
||||||
|
if (isNaN(level) || level < 1 || level > 30) {
|
||||||
|
throw new Error("'level' is not a number between 1 and 30 (but it should be)");
|
||||||
|
}
|
||||||
// TODO
|
// TODO
|
||||||
//
|
//
|
||||||
// S2.idToLatLng(id)
|
// S2.idToLatLng(id)
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
var S2 = require('../src/s2geometry.js').S2;
|
var S2 = require('../src/s2geometry.js').S2;
|
||||||
|
|
||||||
|
for(var level = 1; level <= 20; level++) {
|
||||||
|
|
||||||
|
|
||||||
for(var level = 1; level <= 20; level++)
|
|
||||||
{
|
|
||||||
var success = 0;
|
var success = 0;
|
||||||
var total = 0;
|
var total = 0;
|
||||||
for (var x = -180.0; x < 180; x += 0.5) {
|
for (var x = -180.0; x < 180; x += 0.5) {
|
||||||
|
@ -15,7 +13,7 @@ for(var level = 1; level <= 20; level++)
|
||||||
var quadKey = cell.toHilbertQuadkey();
|
var quadKey = cell.toHilbertQuadkey();
|
||||||
var cell2 = S2.S2Cell.FromHilbertQuadKey(quadKey);
|
var cell2 = S2.S2Cell.FromHilbertQuadKey(quadKey);
|
||||||
|
|
||||||
if(cell.face != cell2.face ||
|
if(cell.face != cell2.face ||
|
||||||
cell.ij[0] != cell2.ij[0] ||
|
cell.ij[0] != cell2.ij[0] ||
|
||||||
cell.ij[1] != cell2.ij[1] ||
|
cell.ij[1] != cell2.ij[1] ||
|
||||||
cell.level != cell2.level)
|
cell.level != cell2.level)
|
||||||
|
@ -23,7 +21,7 @@ for(var level = 1; level <= 20; level++)
|
||||||
/*console.log({
|
/*console.log({
|
||||||
cell: cell,
|
cell: cell,
|
||||||
cell2: cell2})*/
|
cell2: cell2})*/
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -34,10 +32,5 @@ for(var level = 1; level <= 20; level++)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("level:" + level + "\t total:" + total + "\t success:" + success)
|
console.log("level:" + level + "\t total:" + total + "\t success:" + success);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var S2 = require('../src/s2geometry.js').S2;
|
||||||
|
var x, y;
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
function refCheck() {
|
||||||
|
var refKey = '4/032212303102210';
|
||||||
|
var latlng = {
|
||||||
|
'lat': 40.2574448
|
||||||
|
, 'lng': -111.7089464
|
||||||
|
};
|
||||||
|
|
||||||
|
var key = S2.latLngToKey(latlng.lat, latlng.lng, 15);
|
||||||
|
if (key !== refKey) {
|
||||||
|
throw new Error("reference doesn't match");
|
||||||
|
}
|
||||||
|
|
||||||
|
var latlng1 = S2.keyToLatLng('4/032212303102210');
|
||||||
|
var key1 = S2.latLngToKey(latlng1.lat, latlng1.lng, 15);
|
||||||
|
if (key1 !== refKey) {
|
||||||
|
throw new Error("reference 1 doesn't match");
|
||||||
|
}
|
||||||
|
|
||||||
|
var latlng2 = S2.idToLatLng('9749618446378729472');
|
||||||
|
var key2 = S2.latLngToKey(latlng2.lat, latlng2.lng, 15);
|
||||||
|
if (key2 !== refKey) {
|
||||||
|
throw new Error("reference 2 doesn't match");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkReal(lat, lng) {
|
||||||
|
var key = S2.latLngToKey(lat, lng, 15);
|
||||||
|
var latlng = S2.keyToLatLng(key);
|
||||||
|
var key2 = S2.latLngToKey(latlng.lat, latlng.lng, 15);
|
||||||
|
if (key !== key2) {
|
||||||
|
throw new Error("keys do not match", latlng, key, key2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (x = -180; x <= 180; x += (0 + Math.random())) {
|
||||||
|
for (y = -180; y <= 180; y += (0 + Math.random())) {
|
||||||
|
count += 1;
|
||||||
|
checkReal(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('PASS:', count, 'random locations without any key mismatches');
|
Loading…
Reference in New Issue