• This repository has been archived on 25/Apr/2020
  • Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    JavaScript
  • Created about 12 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Find the k nearest neighbors for points on a sphere.

sphere-knn

THIS LIBRARY IS NO LONGER MAINTAINED. PLEASE FORK IF YOU NEED TO USE IT.

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.

More Repositories

1

skycons

JavaScript
1,249
star
2

delaunay-fast

Fast Delaunay Triangulation in JavaScript.
JavaScript
778
star
3

string-hash

Fast string hashing function for Node.JS.
JavaScript
309
star
4

forecast-ruby

Ruby gem for retrieving data from the Forecast Weather API.
Ruby
240
star
5

tz-lookup-oss

JavaScript Library for Timezone Lookup by Location
JavaScript
208
star
6

translations

translate Dark Sky API summaries into any language
JavaScript
139
star
7

binary-search

Tiny binary search library for Node.JS.
JavaScript
85
star
8

pngparse

100%-JavaScript PNG Parser for Node.JS
JavaScript
78
star
9

darksky-ruby

Ruby gem for retrieving data from the Dark Sky API
Ruby
48
star
10

astro

JavaScript
30
star
11

delaunay

Delaunay triangulation in arbitrary dimensions.
JavaScript
23
star
12

inhabited

quickly determine if a lat/lon may be inhabited or not
JavaScript
18
star
13

node-sarra

consume data from Environment and Climate Change Canada
JavaScript
13
star
14

metaregexp

A simple way to keep long regexes organized.
JavaScript
9
star
15

lowres-rgeo

low resolution reverse geocoder for Node.JS
JavaScript
9
star
16

elevation

Finds the elevation of a lat/lon with low-to-moderate resolution
JavaScript
7
star
17

pbj

tiny, stupid bitmap image format
JavaScript
6
star
18

cache

An in-memory, promise-backed cache
JavaScript
5
star
19

bounding

calculate bounding areas of points
JavaScript
4
star
20

zqueue

A little redis-backed queue dingus.
JavaScript
4
star
21

cache-helpers

Caching helper functions for Node.JS.
JavaScript
4
star
22

regression

A tiny Javascript toolkit for regression analysis.
JavaScript
4
star
23

shape

spherical geometry library
JavaScript
1
star