diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..d84fc3c --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Jon Atkins (http://www.jonatkins.com/) +AJ ONeal (https://coolaj86.com) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e92fbcd --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +ISC License (ISC) + +Copyright (c) 2012-2016, Jon Atkins +Copyright (c) 2016, AJ ONeal + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index 2585327..51012a5 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,19 @@ -s2-geometry-javascript +s2-geometry (JavaScript/ES5.1) ====================== -Porting Google's S2 Geometry Library to Javascript - +A pure JavaScript/ES5.1 port of Google/Niantic's S2 Geometry library (used by Ingress, Pokemon GO) Currently contains basic support for S2Cell -More details and examples to come later. - -What you need to know for Pokemon GO clients: +Simple Examples +--------------- ```javascript -var cell = S2.S2Cell.FromLatLng({ lat: 40.2574448, lng: -111.7089464 }, 15); +var level = 15; +var latlng = { lat: 40.2574448, lng: -111.7089464 }; +var cell = S2.S2Cell.FromLatLng(latlng, level); -cell.getNeighbors(); // [ cellLeft, cellDown, cellRight, cellUp ] +cell.getNeighbors(); // [ cellLeft, cellDown, cellRight, cellUp ] -cell.getLatLng(); // { lat: 40.2574448, lng: -111.7089464 } +cell.getLatLng(); // { lat: 40.2574448, lng: -111.7089464 } ``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..220f766 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "s2-geometry", + "version": "1.0.0", + "description": "A pure JavaScript/ES5.1 port of Google/Niantic's S2 Geometry library (used by Ingress, Pokemon GO)", + "main": "src/s2geometry.js", + "scripts": { + "test": "node tests/cellid.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/coolaj86/s2-geometry-javascript.git" + }, + "keywords": [ + "s2", + "geometry", + "s2geometry", + "Niantic", + "Ingress", + "Pokemon", + "GO", + "PokemonGO", + "cellid", + "s2cell", + "s2cellid", + "latitude", + "longitude", + "lat", + "lng" + ], + "author": "AJ ONeal (https://coolaj86.com/)", + "license": "ISC", + "bugs": { + "url": "https://github.com/coolaj86/s2-geometry-javascript/issues" + }, + "homepage": "https://github.com/coolaj86/s2-geometry-javascript#readme" +} diff --git a/src/s2geometry.js b/src/s2geometry.js index eb76722..a25a2ba 100644 --- a/src/s2geometry.js +++ b/src/s2geometry.js @@ -22,10 +22,10 @@ // - i,j: they always use 30 bits, adjusting as needed. we use 0 to (1<= 0.5) { + return (1/3.0) * (4*st*st - 1); + } else { + return (1/3.0) * (1 - (4*(1-st)*(1-st))); + } +}; var STToUV = function(st) { - var singleSTtoUV = function(st) { - if (st >= 0.5) { - return (1/3.0) * (4*st*st - 1); - } else { - return (1/3.0) * (1 - (4*(1-st)*(1-st))); - } - } - return [singleSTtoUV(st[0]), singleSTtoUV(st[1])]; }; - -var UVToST = function(uv) { - var singleUVtoST = function(uv) { - if (uv >= 0) { - return 0.5 * Math.sqrt (1 + 3*uv); - } else { - return 1 - 0.5 * Math.sqrt (1 - 3*uv); - } +var singleUVtoST = function(uv) { + if (uv >= 0) { + return 0.5 * Math.sqrt (1 + 3*uv); + } else { + return 1 - 0.5 * Math.sqrt (1 - 3*uv); } - +}; +var UVToST = function(uv) { return [singleUVtoST(uv[0]), singleUVtoST(uv[1])]; }; @@ -159,7 +156,7 @@ var IJToST = function(ij,order,offsets) { (ij[0]+offsets[0])/maxSize, (ij[1]+offsets[1])/maxSize ]; -} +}; // hilbert space-filling curve // based on http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves @@ -171,7 +168,7 @@ var pointToHilbertQuadList = function(x,y,order) { 'a': [ [0,'d'], [1,'a'], [3,'b'], [2,'a'] ], 'b': [ [2,'b'], [1,'b'], [3,'a'], [0,'c'] ], 'c': [ [2,'c'], [3,'d'], [1,'c'], [0,'b'] ], - 'd': [ [0,'a'], [3,'c'], [1,'d'], [2,'d'] ] + 'd': [ [0,'a'], [3,'c'], [1,'d'], [2,'d'] ] }; var currentSquare='a'; @@ -212,8 +209,6 @@ S2.S2Cell.FromLatLng = function(latLng,level) { var ij = STToIJ(st,level); return S2.S2Cell.FromFaceIJ (faceuv[0], ij, level); - - return result; }; S2.S2Cell.FromFaceIJ = function(face,ij,level) { @@ -235,7 +230,7 @@ S2.S2Cell.prototype.getLatLng = function() { var uv = STToUV(st); var xyz = FaceUVToXYZ(this.face, uv); - return XYZToLatLng(xyz); + return XYZToLatLng(xyz); }; S2.S2Cell.prototype.getCornerLatLngs = function() { @@ -304,14 +299,20 @@ S2.S2Cell.prototype.getNeighbors = function() { }; -})(); +})('undefined' !== typeof window ? window : module.exports); -(function () { +(function (exports) { 'use strict'; // Adapted from Leafletjs https://searchcode.com/codesearch/view/42525008/ var L = {}; + var S2 = exports.S2; + + if (!exports.L) { + exports.L = L; + } + S2.L = L; L.LatLng = function (/*Number*/ rawLat, /*Number*/ rawLng, /*Boolean*/ noWrap) { var lat = parseFloat(rawLat, 10); @@ -328,12 +329,7 @@ S2.S2Cell.prototype.getNeighbors = function() { return { lat: lat, lng: lng }; }; - + L.LatLng.DEG_TO_RAD = Math.PI / 180; L.LatLng.RAD_TO_DEG = 180 / Math.PI; - - if (!window.L) { - window.L = L; - } - S2.L = L; -})(); +})('undefined' !== typeof window ? window : module.exports); diff --git a/tests/cellid.js b/tests/cellid.js new file mode 100644 index 0000000..13c33fd --- /dev/null +++ b/tests/cellid.js @@ -0,0 +1,21 @@ +'use strict'; + +var S2 = require('../src/s2geometry.js').S2; + +var level = 15; +var latlng = { lat: 40.2574448, lng: -111.7089464 }; +var cell = S2.S2Cell.FromLatLng(latlng, level); + +cell.getNeighbors(); // [ cellLeft, cellDown, cellRight, cellUp ] + +latlng = cell.getLatLng(); // { lat: 40.2574448, lng: -111.7089464 } + +if (40 === Math.round(latlng.lat) && -112 === Math.round(latlng.lng)) { + console.log('OK'); + process.exit(0); +} +else { + console.log('[ERROR] latitude and longitude were not the expected values:'); + console.log(latlng); + process.exit(1); +}