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

sphere-knn

v1.4.0

Published

search for k nearest neighbors on a sphere

Downloads

5,293

Readme

sphere-knn

sphere-knn is a Node.JS module that provides fast nearest-neighbor lookups on a sphere. This is useful if, for example, you have a database of geographic points (latitude, longitude) and want to swiftly look up which of those points are near a given latitude, longitude pair. It came out of a need to do such lookups in the Dark Sky API, but all existing libraries either threw scary runtime flags, were too slow, broke at the International Date Line, or didn't have tests.

So anyway, this one is well-tested and works correctly regardless of where on the earth things are located. It's been in production use at Dark Sky since Oct 2012.

Usage

To install:

npm install sphere-knn

To use:

var sphereKnn = require("sphere-knn"),
    lookup    = sphereKnn([
      /* This array needs to be full of objects that have latitudes and
       * longitudes. Accepted property names are "lat", "latitude", "lon",
       * "lng", "long", "longitude". */
      {lat: ..., lon: ...},

      /* You can also use an array. */
      [my_lat, my_lon],

      ...
    ])

var points = lookup(someLatitude, someLongitude, maxResults, maxDistance)

The points array consists of objects that were in the array passed to sphereKnn(), ordered from nearest to furthest. The maxResults value is the maximum size of the returned array, and is mandatory. (Often, you'll just want it set to 1, but there are use-cases for more points.) The maxDistance value is the maximum distance away we should look up for in meters. This is handy if you want to find, say, any points within 200 kilometers of a given point. maxDistance is optional and defaults to Infinity. (Please note that since our backing earth model is a sphere, all distances given to this function are approximate. If it matters that much to you, add a few meters of padding and check the results with some very accurate (and complex) distance function.)

License

To the extend possible by law, The Dark Sky Company, LLC has waived all copyright and related or neighboring rights to this library.