GithubHelp home page GithubHelp logo

sleggi / s2-geometry.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hunterjm/s2-geometry.js

0.0 0.0 0.0 52 KB

Porting Google's S2 Geometry Library to Javascript

License: ISC License

JavaScript 100.00%

s2-geometry.js's Introduction

s2-geometry (JavaScript/ES5.1)

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

Face 2


The North Pole
(and Canada / Europe)
Face 0
Orientation A

Face 0


Africa
Face 1
Orientation D

Face 1


Asia
Face 3
Orientation D

Face 3


Nothing
(and Australia)
Face 4
Orientation A

Face 4


The Americas
(and Provo, UT)
Face 5
Orientation D

Face 5


Antarctica

Where is this being used?

Simple Examples

'use strict';

var S2 = require('s2-geometry').S2;

var lat = 40.2574448;
var lng = -111.7089464;
var level = 15;



//
// Convert from Lat / Lng
//
var key = S2.latLngToKey(lat, lng, level);
// '4/032212303102210'



//
// Convert between Hilbert Curve Quadtree Key and S2 Cell Id
//
var id = S2.keyToId(key);
// '9749618446378729472'

var key = S2.idToKey(id);
// '9749618446378729472'



//
// Neighbors
//
var neighbors = S2.latLngToNeighborKeys(lat, lng, level);
// [ keyLeft, keyDown, keyRight, keyUp ]



//
// Previous, Next, and Step
//
var nextKey = S2.nextKey(key);
var prevKey = S2.prevKey(key);

var backTenKeys = S2.stepKey(key, -10);

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)
var key = S2.latLngToKey(40.2574448, -111.7089464, 15);   // '4/032212303102210'
var id = S2.keyToId(key);                                 // '9749618446378729472'

var nextKey = S2.nextKey(key);
var nextId = S2.keyToId(nextKey);

var prevKey = S2.prevKey(key);
var prevId = S2.keyToId(prevKey);

var backTenKeys = S2.stepKey(key, -10);

// See it
console.log(prevKey);                                 // '4/032212303102203'
console.log(key);                                     // '4/032212303102210'
console.log(nextKey);                                 // '4/032212303102211'
console.log(nextId);

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.split('/');
var face = parts[0];                  // 4
var position = parts[1];              // '032212303102210';
var level = '032212303102210'.length; // 15

var cellId = S2.facePosLevelToId(face, position, level);

console.log(cellId);

Convert from hilbert quadtree id to s2 cell id:

Example '9749618446378729472' becomes '4/032212303102210'

'use strict';

var cellId = '9749618446378729472';

var hilbertQuadkey = S2.idToKey(cellId);

console.log(hilbertQuadkey);

s2-geometry.js's People

Contributors

jonatkins avatar davidmurdoch avatar hunterjm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.