s2-geometry (JavaScript/ES5.1)
| Sponsored by ppl
A pure JavaScript/ES5.1 port of Google/Niantic's S2 Geometry library (as used by Ingress, Pokemon GO)
Currently contains basic support for S2Cell
Face 2
Orientation A The North Pole (and Canada / Europe) |
|||
Face 0
Orientation A Africa |
Face 1
Orientation D Asia |
Face 3
Orientation D Nothing (and Australia) |
Face 4
Orientation A The Americas (and Provo, UT) |
Face 5
Orientation D Antarctica |
Where is this being used?
Simple Examples
'use strict'; var S2 = S2; var lat = 402574448;var lng = -1117089464;var level = 15; //// Convert from Lat / Lng//var key = S2;// '4/032212303102210' //// Convert between Hilbert Curve Quadtree Key and S2 Cell Id//var id = S2;// '9749618446378729472' var key = S2;// '9749618446378729472' //// Convert between Quadkey and Id//var latlng = S2;var latlng = S2; //// Neighbors//var neighbors = S2;// [ keyLeft, keyDown, keyRight, keyUp ] //// Previous, Next, and Step//var nextKey = S2;var prevKey = S2; var backTenKeys = S2;
Previous and Next
You can get the previous and next S2CellId from any given Key:
- Convert from Lat/Lng to Key (Face and Hilbert Curve Quadtree)
- Get the Previous or Next Key
- Convert the Key to an Id (uint64 string)
var key = S2; // '4/032212303102210'var id = S2; // '9749618446378729472' var nextKey = S2;var nextId = S2; var prevKey = S2;var prevId = S2; var backTenKeys = S2; // See itconsole; // '4/032212303102203'console; // '4/032212303102210'console; // '4/032212303102211'console;
convert Cell Id to Hilbert Curve Quad Tree
Convert from base 10 (decimal) S2 Cell Id
to base 4 quadkey
(aka hilbert curve quadtree id)
Example '4/032212303102210' becomes '9749618446378729472'
'use strict'; var quadkey = '4/032212303102210'var parts = quadkey;var face = parts0; // 4var position = parts1; // '032212303102210';var level = '032212303102210'length; // 15 var cellId = S2; console;
Convert from hilbert quadtree id to s2 cell id:
Example '9749618446378729472' becomes '4/032212303102210'
'use strict'; var cellId = '9749618446378729472'; var hilbertQuadkey = S2; console;
Convert Key and Id to LatLng
var latlng = S2; var latlng = S2;