• Stars
    star
    618
  • Rank 72,605 (Top 2 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

A fast static index for 2D points

KDBush

A very fast static spatial index for 2D points based on a flat KD-tree. Compared to RBush:

  • Points only — no rectangles.
  • Static — you can't add/remove items after initial indexing.
  • Faster indexing and search, with lower memory footprint.
  • Index is stored as a single array buffer (so you can transfer it between threads or store it as a compact file).

If you need a static index for rectangles, not only points, see Flatbush. When indexing points, KDBush has the advantage of taking ~2x less memory than Flatbush.

Build Status Simply Awesome

Usage

// initialize KDBush for 1000 items
const index = new KDBush(1000);

// fill it with 1000 points
for (const {x, y} of items) {
    index.add(x, y);
}

// perform the indexing
index.finish();

// make a bounding box query
const foundIds = index.range(minX, minY, maxX, maxY);

// map ids to original items
const foundItems = foundIds.map(i => items[i]);

// make a radius query
const neighborIds = index.within(x, y, 5);

// instantly transfer the index from a worker to the main thread
postMessage(index.data, [index.data]);

// reconstruct the index from a raw array buffer
const index = KDBush.from(e.data);

Install

Install with NPM: npm install kdbush, then import as a module:

import KDBush from 'kdbush';

Or use as a module directly in the browser with jsDelivr:

<script type="module">
    import KDBush from 'https://cdn.jsdelivr.net/npm/kdbush/+esm';
</script>

Alternatively, there's a browser bundle with a KDBush global variable:

<script src="https://cdn.jsdelivr.net/npm/kdbush"></script>

API

new KDBush(numItems[, nodeSize, ArrayType])

Creates an index that will hold a given number of points (numItems). Additionally accepts:

  • nodeSize: Size of the KD-tree node, 64 by default. Higher means faster indexing but slower search, and vise versa.
  • ArrayType: Array type to use for storing coordinate values. Float64Array by default, but if your coordinates are integer values, Int32Array makes the index faster and smaller.

index.add(x, y)

Adds a given point to the index. Returns a zero-based, incremental number that represents the newly added point.

index.range(minX, minY, maxX, maxY)

Finds all items within the given bounding box and returns an array of indices that refer to the order the items were added (the values returned by index.add(x, y)).

index.within(x, y, radius)

Finds all items within a given radius from the query point and returns an array of indices.

KDBush.from(data)

Recreates a KDBush index from raw ArrayBuffer data (that's exposed as index.data on a previously indexed KDBush instance). Very useful for transferring or sharing indices between threads or storing them in a file.

Properties

  • data: array buffer that holds the index.
  • numItems: number of stored items.
  • nodeSize: number of items in a KD-tree node.
  • ArrayType: array type used for internal coordinates storage.
  • IndexArrayType: array type used for internal item indices storage.

More Repositories

1

suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
JavaScript
3,056
star
2

rbush

RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles
JavaScript
2,411
star
3

simplify-js

High-performance JavaScript polyline simplification library
JavaScript
2,274
star
4

bullshit.js

A bookmarklet for translating marketing speak into human-readable text. 💩
JavaScript
1,846
star
5

flatbush

A very fast static spatial index for 2D points and rectangles in JavaScript 🌱
JavaScript
1,404
star
6

simpleheat

A tiny JavaScript library for drawing heatmaps with Canvas
JavaScript
927
star
7

dead-simple-grid

Dead Simple Grid is a responsive CSS grid micro framework that is just that. Dead simple.
HTML
747
star
8

tinyqueue

The smallest and simplest priority queue in JavaScript.
JavaScript
428
star
9

projects

A list of awesome open source projects Volodymyr Agafonkin is involved in.
410
star
10

geokdbush

The fastest spatial index for geographic locations in JavaScript
JavaScript
334
star
11

robust-predicates

Fast robust predicates for computational geometry in JavaScript
JavaScript
296
star
12

road-orientation-map

A visualization of road orientations on an interactive map
JavaScript
295
star
13

rbush-knn

k-nearest neighbors search (KNN) for RBush
JavaScript
207
star
14

delaunator-rs

Fast 2D Delaunay triangulation in Rust. A port of Delaunator.
Rust
201
star
15

tinyjam

A radically simple, zero-configuration static site generator in JavaScript
JavaScript
153
star
16

flatqueue

A very fast and simple JavaScript priority queue
JavaScript
133
star
17

quickselect

A fast selection algorithm in JavaScript.
JavaScript
82
star
18

seidel

[DEPRECATED] A JS polygon triangulation library
JavaScript
82
star
19

icomesh

Fast JavaScript icosphere mesh generation library for WebGL visualizations
JavaScript
53
star
20

bbtree

Self-balancing Binary Search Trees in JavaScript
JavaScript
49
star
21

yeahjs

A tiny, modern, fast EJS templating library
JavaScript
45
star
22

geoflatbush

Geographic kNN extension for Flatbush
JavaScript
43
star
23

kdbush.hpp

A fast static spatial index for 2D points in C++11
C++
33
star
24

worker-data-load

A test that shows the benefit of loading large amounts of data directly in a worker instead of a page.
JavaScript
33
star
25

Leaflet.TouchHover

A plugin for adding hover-like interaction to Leaflet maps on mobile devices
JavaScript
27
star
26

eslint-config-mourner

A strict ESLint config for my JavaScript projects
JavaScript
19
star
27

suncalc-go

SunCalc written in Go
Go
18
star
28

yeahml

A tiny subset of YAML for JavaScript
JavaScript
8
star
29

serenity-tm

Serenity is a light, minimal syntax highlighting theme for Sublime Text and Textmate.
8
star
30

pbf-split

Splits a Node stream of protocol buffer messages
JavaScript
7
star
31

hello-lib

Simple boilerplate for my small JS libraries.
JavaScript
7
star
32

color-metrics

JavaScript
6
star
33

agafonkin.com

My little personal page
HTML
5
star
34

fanny

A simple and fast multilayer feedforward neural network implementation in JS, made for learning purposes.
JavaScript
5
star
35

hain

Hain triangulation algorithm in JS (work in progress)
C++
4
star
36

mourner

2
star
37

mourner.github.com

Nothing here yet.
1
star