2016-07-26 04:42:53 +00:00
|
|
|
s2-geometry (JavaScript/ES5.1)
|
2014-01-23 17:05:56 +00:00
|
|
|
======================
|
|
|
|
|
2016-07-28 09:23:39 +00:00
|
|
|
A pure JavaScript/ES5.1 port of Google/Niantic's S2 Geometry library (as used by **Ingress**, **Pokemon GO**)
|
2014-01-23 17:10:43 +00:00
|
|
|
|
|
|
|
Currently contains basic support for S2Cell
|
|
|
|
|
2016-08-03 22:48:49 +00:00
|
|
|
### Face 0 - Africa (i.e. The Congo)
|
|
|
|
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td></td>
|
|
|
|
<td>
|
|
|
|
Face 2
|
|
|
|
|
|
|
|
[![Face 2](http://i.imgur.com/SODO4bTt.jpg)](http://i.imgur.com/SODO4bT.jpg)
|
|
|
|
</td>
|
|
|
|
<td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
Face 0 Africa
|
|
|
|
|
|
|
|
[![Face 0](http://i.imgur.com/dLI5Zd1t.jpg)](http://i.imgur.com/dLI5Zd1t.jpg)
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
Face 1 Asia
|
|
|
|
|
|
|
|
[![Face 1](http://i.imgur.com/duTLDTVt.jpg)](http://i.imgur.com/duTLDTV.jpg)
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
|
|
|
|
Face 3
|
|
|
|
|
|
|
|
[![Face 3](http://i.imgur.com/6Ho35Tct.jpg)](http://i.imgur.com/6Ho35Tc.jpg)
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
Face 4
|
|
|
|
|
|
|
|
[![Face 4](http://i.imgur.com/3IBAfqjt.jpg)](http://i.imgur.com/3IBAfqj.jpg)
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td></td>
|
|
|
|
<td></td>
|
|
|
|
<td></td>
|
|
|
|
<td>
|
|
|
|
Face 5
|
|
|
|
|
|
|
|
[![Face 5](http://i.imgur.com/HZCBvgyt.jpg)](http://i.imgur.com/HZCBvgy.jpg)
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2016-07-28 09:23:39 +00:00
|
|
|
Where is this being used?
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
* [pokemap-webapp](https://github.com/Daplie/pokemap-webapp)
|
|
|
|
* [node-pokemap](https://github.com/Daplie/node-pokemap)
|
|
|
|
* [Pokemon-GO-node-api](https://github.com/Daplie/Pokemon-GO-node-api)
|
|
|
|
|
2016-07-26 04:42:53 +00:00
|
|
|
Simple Examples
|
|
|
|
---------------
|
2016-07-26 01:36:32 +00:00
|
|
|
|
|
|
|
```javascript
|
2016-07-26 04:42:53 +00:00
|
|
|
var level = 15;
|
|
|
|
var latlng = { lat: 40.2574448, lng: -111.7089464 };
|
|
|
|
var cell = S2.S2Cell.FromLatLng(latlng, level);
|
2016-07-26 01:36:32 +00:00
|
|
|
|
2016-07-26 04:42:53 +00:00
|
|
|
cell.getNeighbors(); // [ cellLeft, cellDown, cellRight, cellUp ]
|
2016-07-26 01:36:32 +00:00
|
|
|
|
2016-07-26 04:42:53 +00:00
|
|
|
cell.getLatLng(); // { lat: 40.2574448, lng: -111.7089464 }
|
2016-07-28 06:25:17 +00:00
|
|
|
|
|
|
|
var key = cell.toHilbertQuadkey();
|
|
|
|
```
|
|
|
|
|
|
|
|
Previous and Next
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
You can get the previous and next S2CellId from any given Key:
|
|
|
|
|
|
|
|
1. Convert from Lat/Lng to Key (Face and Hilbert Curve Quadtree)
|
|
|
|
2. Get the Previous or Next Key
|
|
|
|
3. Convert the Key to an Id (uint64 string)
|
|
|
|
|
2016-07-28 09:17:53 +00:00
|
|
|
```javascript
|
2016-07-28 09:21:24 +00:00
|
|
|
var key = S2.latLngToKey(40.2574448, -111.7089464); // '4/032212303102210'
|
|
|
|
var id = S2.toId(key); // '9749618446378729472'
|
2016-07-28 06:25:17 +00:00
|
|
|
|
|
|
|
var nextKey = S2.nextKey(key);
|
|
|
|
var nextId = S2.toId(nextKey);
|
|
|
|
|
|
|
|
var prevKey = S2.prevKey(key);
|
|
|
|
var prevId = S2.toId(prevKey);
|
|
|
|
|
|
|
|
// See it
|
2016-07-28 09:21:24 +00:00
|
|
|
console.log(prevKey); // '4/032212303102203'
|
|
|
|
console.log(key); // '4/032212303102210'
|
|
|
|
console.log(nextKey); // '4/032212303102211'
|
2016-07-28 06:25:17 +00:00
|
|
|
console.log(nextId);
|
2016-07-26 01:36:32 +00:00
|
|
|
```
|
2016-07-28 00:41:04 +00:00
|
|
|
|
|
|
|
convert Cell Id to Quadkey
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Convert from base 10 (decimal) `S2 Cell Id` to base 4 `quadkey` (aka hilbert curve quadtree id)
|
|
|
|
|
|
|
|
Example '4/032212303102210' becomes '9749618446378729472'
|
|
|
|
|
2016-07-28 09:17:53 +00:00
|
|
|
```javascript
|
2016-07-28 00:41:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var quadkey = '4/032212303102210'
|
|
|
|
var parts = quadkey.split('/');
|
|
|
|
var face = parts[0]; // 4
|
|
|
|
var position = parts[1]; // '032212303102210';
|
|
|
|
var level = '032212303102210'.length; // 15
|
|
|
|
|
|
|
|
var cellId = S2.fromFacePosLevel(face, position, level);
|
|
|
|
|
|
|
|
console.log(cellId);
|
|
|
|
```
|
|
|
|
|
|
|
|
Convert from hilbert quadtree id to s2 cell id:
|
|
|
|
|
|
|
|
Example '9749618446378729472' becomes '4/032212303102210'
|
|
|
|
|
2016-07-28 09:21:24 +00:00
|
|
|
```javascript
|
2016-07-28 00:41:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var cellId = '9749618446378729472';
|
|
|
|
|
|
|
|
var hilbertQuadkey = S2.toHilbertQuadkey(cellId);
|
|
|
|
|
|
|
|
console.log(hilbertQuadkey);
|
|
|
|
```
|