npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s2-geometry

v1.2.10

Published

A pure JavaScript/ES5.1 port of Google/Niantic's S2 Geometry library (used by Ingress, Pokemon GO)

Downloads

76,827

Readme

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

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'


//
// Convert between Quadkey and Id
//
var latlng = S2.keyToLatLng(key);
var latlng = S2.idToLatLng(id);



//
// 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);

Convert Key and Id to LatLng

var latlng = S2.keyToLatLng('4/032212303102210');

var latlng = S2.idToLatLng('9749618446378729472');