From 68274eb677be0338984b0c7d59290ec323c47e67 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 3 Aug 2016 00:04:26 -0600 Subject: [PATCH] use correct starting square for face #6 #8 #9 --- src/s2geometry.js | 26 +++++++++++++-- tests/debug.js | 83 +++++++++++++++++++++++++++-------------------- 2 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/s2geometry.js b/src/s2geometry.js index 946aee2..9667bd2 100644 --- a/src/s2geometry.js +++ b/src/s2geometry.js @@ -186,7 +186,7 @@ S2.IJToST = function(ij,order,offsets) { // note: rather then calculating the final integer hilbert position, we just return the list of quads // this ensures no precision issues whth large orders (S3 cell IDs use up to 30), and is more // convenient for pulling out the individual bits as needed later -var pointToHilbertQuadList = function(x,y,order) { +var pointToHilbertQuadList = function(x,y,order,sq) { var hilbertMap = { 'a': [ [0,'d'], [1,'a'], [3,'b'], [2,'a'] ], 'b': [ [2,'b'], [1,'b'], [3,'a'], [0,'c'] ], @@ -194,7 +194,7 @@ var pointToHilbertQuadList = function(x,y,order) { 'd': [ [0,'a'], [3,'c'], [1,'d'], [2,'d'] ] }; - var currentSquare='a'; + var currentSquare=sq||'a'; var positions = []; for (var i=order-1; i>=0; i--) { @@ -286,7 +286,27 @@ S2.S2Cell.prototype.getCornerLatLngs = function() { S2.S2Cell.prototype.getFaceAndQuads = function () { - var quads = pointToHilbertQuadList(this.ij[0], this.ij[1], this.level); + var sq; + + switch(this.face) { + case 0: + /* fallthru */ + case 2: + /* fallthru */ + case 4: + sq = 'a'; + break; + + case 1: + /* fallthru */ + case 3: + /* fallthru */ + case 5: + sq = 'd'; + break; + } + + var quads = pointToHilbertQuadList(this.ij[0], this.ij[1], this.level, sq); return [this.face,quads]; }; diff --git a/tests/debug.js b/tests/debug.js index 738c3c0..4b30f2f 100644 --- a/tests/debug.js +++ b/tests/debug.js @@ -8,43 +8,54 @@ var lat = -43.525166; var lng = 172.655096; //var id = '8678661352471920640'; -console.log(''); -console.log('Lat/Lng'); -console.log('=', lat + ',' + lng); +var tests = [ + [ -13.846153846153854, -41.53846153846155 ] // face 0 +, [ -13.846153846153854, 96.92307692307692 ] // face 1 +, [ 41.53846153846153, -124.61538461538463 ] // face 2 +, [ -152.30769230769232, 41.53846153846153 ] // face 3 +, [ -152.30769230769232, 69.23076923076923 ] // face 4 +, [ -124.61538461538463, -69.23076923076924 ] // face 5 +]; -// -// Lat / Lng to XYZ -// -var nS2LatLng = new nS2.S2LatLng(lat, lng).toPoint(); -var nXyz = [ nS2LatLng.x(), nS2LatLng.y(), nS2LatLng.z() ]; -//var oXyz = oS2.LatLngToXYZ({ lat: lat, lng: lng }); -var jXyz = jS2.LatLngToXYZ({ lat: lat, lng: lng }); -console.log(''); -console.log('XYZ'); -console.log('=', nXyz); -//console.log('o', oXyz); -console.log('j', jXyz); +tests.forEach(function (pair, i) { + var lat = pair[0]; + var lng = pair[1]; + + console.log(''); + console.log(''); + console.log('FACE', i); + + console.log(''); + console.log('Lat/Lng'); + console.log('=', lat + ',' + lng); + + // + // Lat / Lng to XYZ + // + var nS2LatLng = new nS2.S2LatLng(lat, lng).toPoint(); + //var nXyz = [ nS2LatLng.x(), nS2LatLng.y(), nS2LatLng.z() ]; + //var jXyz = jS2.LatLngToXYZ({ lat: lat, lng: lng }); + /* + console.log(''); + console.log('XYZ'); + console.log('=', nXyz); + console.log('j', jXyz); + */ -var nCell = new nS2.S2CellId(nS2LatLng).parent(15); -//var oCell = oS2.S2Cell.FromLatLng({ lat: lat, lng: lng }, 15); -var jCell = jS2.S2Cell.FromLatLng({ lat: lat, lng: lng }, 15); -console.log(''); -console.log('F,IJ,L'); -//console.log('=', cellN); -//console.log('o', oCell.toString()); -console.log('j', jCell.toString()); + var nCell = new nS2.S2CellId(nS2LatLng).parent(15); + var jCell = jS2.S2Cell.FromLatLng({ lat: lat, lng: lng }, 15); + /* + console.log(''); + console.log('F,IJ,L'); + console.log('j', jCell.toString()); + */ -var nKey = nCell.toString(); -//var oQuad = oCell.getFaceAndQuads(); -//var oKey = oQuad[0] + '/' + oQuad[1].join(''); -var jQuad = jCell.getFaceAndQuads(); -var jKey = jQuad[0] + '/' + jQuad[1].join(''); -console.log(''); -console.log('Quadkey'); -console.log('=', nKey); -//console.log('o', oKey); -console.log('j', jKey); - -//var nCellId = new nS2.S2CellId(id); -//console.log(nCellId.toLatLng().toString()); + var nKey = nCell.toString(); + var jQuad = jCell.getFaceAndQuads(); + var jKey = jQuad[0] + '/' + jQuad[1].join(''); + console.log(''); + console.log('Quadkey'); + console.log('=', nKey); + console.log('j', jKey); +});