diff --git a/src/s2geometry.js b/src/s2geometry.js index c68d313..b8f78ef 100644 --- a/src/s2geometry.js +++ b/src/s2geometry.js @@ -28,14 +28,25 @@ var S2 = exports.S2 = {}; var LatLngToXYZ = function(latLng) { - var d2r = S2.L.LatLng.DEG_TO_RAD; + // http://stackoverflow.com/questions/8981943/lat-long-to-x-y-z-position-in-js-not-working + var lat = latLng.lat; + var lon = latLng.lng; + var DEG_TO_RAD = Math.PI / 180.0; - var phi = latLng.lat*d2r; - var theta = latLng.lng*d2r; + var phi = lat * DEG_TO_RAD; + var theta = lon * DEG_TO_RAD; - var cosphi = Math.cos(phi); + var cosLat = Math.cos(phi); + var sinLat = Math.sin(phi); + var cosLon = Math.cos(theta); + var sinLon = Math.sin(theta); + var rad = 500.0; - return [Math.cos(theta)*cosphi, Math.sin(theta)*cosphi, Math.sin(phi)]; + return [ + rad * cosLat * cosLon + , rad * cosLat * sinLon + , rad * sinLat + ]; }; var XYZToLatLng = function(xyz) { @@ -192,8 +203,6 @@ var pointToHilbertQuadList = function(x,y,order) { }; - - // S2Cell class S2.S2Cell = function(){}; @@ -253,11 +262,16 @@ S2.S2Cell.prototype.getCornerLatLngs = function() { }; -S2.S2Cell.prototype.getFaceAndQuads = function() { +S2.S2Cell.prototype.getFaceAndQuads = function () { var quads = pointToHilbertQuadList(this.ij[0], this.ij[1], this.level); return [this.face,quads]; }; +S2.S2Cell.prototype.toHilbertQuadkey = function () { + var quads = pointToHilbertQuadList(this.ij[0], this.ij[1], this.level); + + return this.face.toString(10) + '/' + quads.join(''); +}; S2.S2Cell.prototype.getNeighbors = function() { diff --git a/tests/example.provo.txt b/tests/example.provo.txt new file mode 100644 index 0000000..709976c --- /dev/null +++ b/tests/example.provo.txt @@ -0,0 +1,21 @@ +-10 4 '9749618424903892992' '4/032212303102122' '40.253289,-111.712279' 15 +-9 4 '9749618427051376640' '4/032212303102123' '40.253874,-111.709298' 15 +-8 4 '9749618429198860288' '4/032212303102130' '40.254460,-111.706317' 15 +-7 4 '9749618431346343936' '4/032212303102131' '40.252022,-111.706317' 15 +-6 4 '9749618433493827584' '4/032212303102132' '40.252607,-111.703336' 15 +-5 4 '9749618435641311232' '4/032212303102133' '40.255045,-111.703336' 15 +-4 4 '9749618437788794880' '4/032212303102200' '40.257483,-111.703336' 15 +-3 4 '9749618439936278528' '4/032212303102201' '40.259920,-111.703336' 15 +-2 4 '9749618442083762176' '4/032212303102202' '40.259335,-111.706317' 15 +-1 4 '9749618444231245824' '4/032212303102203' '40.256897,-111.706317' 15 +0 4 '9749618446378729472' '4/032212303102210' '40.256312,-111.709298' 15 +1 4 '9749618448526213120' '4/032212303102211' '40.255727,-111.712279' 15 +2 4 '9749618450673696768' '4/032212303102212' '40.258165,-111.712279' 15 +3 4 '9749618452821180416' '4/032212303102213' '40.258750,-111.709298' 15 +4 4 '9749618454968664064' '4/032212303102220' '40.261188,-111.709298' 15 +5 4 '9749618457116147712' '4/032212303102221' '40.260602,-111.712279' 15 +6 4 '9749618459263631360' '4/032212303102222' '40.263040,-111.712279' 15 +7 4 '9749618461411115008' '4/032212303102223' '40.263625,-111.709298' 15 +8 4 '9749618463558598656' '4/032212303102230' '40.264210,-111.706317' 15 +9 4 '9749618465706082304' '4/032212303102231' '40.261773,-111.706317' 15 +10 4 '9749618467853565952' '4/032212303102232' '40.262358,-111.703336' 15 diff --git a/tests/example.startup.txt b/tests/example.startup.txt new file mode 100644 index 0000000..50c94b0 --- /dev/null +++ b/tests/example.startup.txt @@ -0,0 +1,21 @@ +-10 4 '9749615149991329792' '4/032212302322211' '40.235133,-111.655639' 15 +-9 4 '9749615152138813440' '4/032212302322212' '40.234549,-111.658620' 15 +-8 4 '9749615154286297088' '4/032212302322213' '40.232110,-111.658620' 15 +-7 4 '9749615156433780736' '4/032212302322220' '40.231526,-111.661601' 15 +-6 4 '9749615158581264384' '4/032212302322221' '40.233965,-111.661601' 15 +-5 4 '9749615160728748032' '4/032212302322222' '40.233381,-111.664582' 15 +-4 4 '9749615162876231680' '4/032212302322223' '40.230942,-111.664582' 15 +-3 4 '9749615165023715328' '4/032212302322230' '40.228503,-111.664582' 15 +-2 4 '9749615167171198976' '4/032212302322231' '40.229087,-111.661601' 15 +-1 4 '9749615169318682624' '4/032212302322232' '40.226647,-111.661601' 15 +0 4 '9749615171466166272' '4/032212302322233' '40.226063,-111.664582' 15 +1 4 '9749615173613649920' '4/032212302322300' '40.223624,-111.664582' 15 +2 4 '9749615175761133568' '4/032212302322301' '40.221184,-111.664582' 15 +3 4 '9749615177908617216' '4/032212302322302' '40.221768,-111.661601' 15 +4 4 '9749615180056100864' '4/032212302322303' '40.224208,-111.661601' 15 +5 4 '9749615182203584512' '4/032212302322310' '40.224791,-111.658620' 15 +6 4 '9749615184351068160' '4/032212302322311' '40.225375,-111.655639' 15 +7 4 '9749615186498551808' '4/032212302322312' '40.222935,-111.655639' 15 +8 4 '9749615188646035456' '4/032212302322313' '40.222352,-111.658620' 15 +9 4 '9749615190793519104' '4/032212302322320' '40.219912,-111.658620' 15 +10 4 '9749615192941002752' '4/032212302322321' '40.220496,-111.655639' 15 diff --git a/tests/latlng2cell.js b/tests/latlng2cell.js index 13c33fd..044c799 100644 --- a/tests/latlng2cell.js +++ b/tests/latlng2cell.js @@ -3,13 +3,37 @@ var S2 = require('../src/s2geometry.js').S2; var level = 15; -var latlng = { lat: 40.2574448, lng: -111.7089464 }; + +// Provo, UT (Center St) +// '9749618446378729472' '4/032212303102210' '40.256312,-111.709298' 15 +// 4/032212303102210 +//var lat = 40.2574448; +//var lng = -111.7089464; +var latlng = { lat: 40.256312, lng: -111.709298 }; var cell = S2.S2Cell.FromLatLng(latlng, level); +console.log(cell.toHilbertQuadkey(), '4/032212303102210' === cell.toHilbertQuadkey()); +console.log(cell.getLatLng(), '40.256312,-111.709298' === cell.getLatLng(), '40.256312,-111.709298'); + +// Startup Building in Provo +// '9749615171466166272' '4/032212302322233' '40.226063,-111.664582' 15 +// 4/032212302322233 +//var lat = 40.2262363; +//var lng = -111.6630927; +var latlng = { lat: 40.226063, lng: -111.664582 }; +var cell = S2.S2Cell.FromLatLng(latlng, level); + +console.log(cell.toHilbertQuadkey(), '4/032212302322233' === cell.toHilbertQuadkey()); +console.log(cell.getLatLng(), '40.226063,-111.664582' === cell.getLatLng(), '40.226063,-111.664582'); + +/* cell.getNeighbors(); // [ cellLeft, cellDown, cellRight, cellUp ] latlng = cell.getLatLng(); // { lat: 40.2574448, lng: -111.7089464 } +console.log(orig); +console.log(latlng); + if (40 === Math.round(latlng.lat) && -112 === Math.round(latlng.lng)) { console.log('OK'); process.exit(0); @@ -19,3 +43,4 @@ else { console.log(latlng); process.exit(1); } +*/ diff --git a/tests/s2-node-hilbert.js b/tests/s2-node-hilbert.js index 8304800..7f1d0a8 100644 --- a/tests/s2-node-hilbert.js +++ b/tests/s2-node-hilbert.js @@ -1,9 +1,14 @@ var S2 = require('s2geometry-node'); -var lat = 40.2574448; -var lng = -111.7089464; + +// Provo, UT (Center St) +//var lat = 40.2574448; +//var lng = -111.7089464; + +// Startup Building in Provo +var lat = 40.2262363; +var lng = -111.6630927; var s2latlng = new S2.S2LatLng(lat, lng); -var point; var cellId = new S2.S2CellId(s2latlng).parent(15); var cell;