All Projects → mourner → Flatbush

mourner / Flatbush

Licence: isc
A very fast static spatial index for 2D points and rectangles in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Flatbush

Data structure and algorithms library
A collection of classical algorithms and data-structures implementation in C++ for coding interview and competitive programming
Stars: ✭ 133 (-87.1%)
Mutual labels:  algorithm, data-structures, computational-geometry
Arabiccompetitiveprogramming
The repository contains the ENGLISH description files attached to the video series in my ARABIC algorithms channel.
Stars: ✭ 675 (-34.53%)
Mutual labels:  algorithm, data-structures
Dsa.js Data Structures Algorithms Javascript
🥞Data Structures and Algorithms explained and implemented in JavaScript + eBook
Stars: ✭ 6,251 (+506.3%)
Mutual labels:  algorithm, data-structures
Turf
A modular geospatial engine written in JavaScript
Stars: ✭ 6,659 (+545.88%)
Mutual labels:  algorithm, computational-geometry
Data Structure And Algorithms With Es6
Data Structures and Algorithms using ES6
Stars: ✭ 594 (-42.39%)
Mutual labels:  algorithm, data-structures
Algorithms
Data Structure Libraries and Algorithms implementation
Stars: ✭ 624 (-39.48%)
Mutual labels:  algorithm, data-structures
Light Tips
Some code tips about algorithms, php and more 🔥
Stars: ✭ 705 (-31.62%)
Mutual labels:  algorithm, data-structures
Sde Interview Questions
Most comprehensive list 📋 of tech interview questions 📘 of companies scraped from Geeksforgeeks, CareerCup and Glassdoor.
Stars: ✭ 5,406 (+424.35%)
Mutual labels:  algorithm, data-structures
Aabb Tree
A d-dimensional aabb-tree implementation for MATLAB.
Stars: ✭ 5 (-99.52%)
Mutual labels:  data-structures, computational-geometry
Polysnap
A work in progress polygon operations library with integer snap-rounding
Stars: ✭ 14 (-98.64%)
Mutual labels:  algorithm, computational-geometry
Lintcode
📜 Lintcode/Leetcode algorithm written by Java, Python and JavaScript.
Stars: ✭ 21 (-97.96%)
Mutual labels:  algorithm, data-structures
Kotlin Algorithm Club
Algorithms and data structures in Kotlin.
Stars: ✭ 576 (-44.13%)
Mutual labels:  algorithm, data-structures
Algorithms And Data Structures In Java
Algorithms and Data Structures in Java
Stars: ✭ 498 (-51.7%)
Mutual labels:  algorithm, data-structures
Get better at cp in 2 months
This contains the curriculum that I will follow to get better at Competitive Programming in 2 months.
Stars: ✭ 627 (-39.19%)
Mutual labels:  algorithm, data-structures
Algorithms and data structures
180+ Algorithm & Data Structure Problems using C++
Stars: ✭ 4,667 (+352.67%)
Mutual labels:  algorithm, data-structures
Tech Refrigerator
🍰 기술 냉장고입니다. 🛒 기술 면접 , 전공 시험 , 지식 함양 등 분명 도움될 거예요! 🤟
Stars: ✭ 699 (-32.2%)
Mutual labels:  algorithm, data-structures
Algorithms
Study cases for Algorithms and Data Structures.
Stars: ✭ 32 (-96.9%)
Mutual labels:  algorithm, data-structures
Kdbush
A fast static index for 2D points
Stars: ✭ 412 (-60.04%)
Mutual labels:  algorithm, computational-geometry
Algodeck
An Open-Source Collection of 200+ Algorithmic Flash Cards to Help you Preparing your Algorithm & Data Structure Interview 💯
Stars: ✭ 4,441 (+330.75%)
Mutual labels:  algorithm, data-structures
Algorithm
Algorithm is a library of tools that is used to create intelligent applications.
Stars: ✭ 787 (-23.67%)
Mutual labels:  algorithm, data-structures

Flatbush

A really fast static spatial index for 2D points and rectangles in JavaScript.

An efficient implementation of the packed Hilbert R-tree algorithm. Enables fast spatial queries on a very large number of objects (e.g. millions), which is very useful in maps, data visualizations and computational geometry algorithms.

Similar to RBush, with the following key differences:

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

Supports geographic locations with the geoflatbush extension.

Build Status minzipped size Simply Awesome

Usage

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

// fill it with 1000 rectangles
for (const p of items) {
    index.add(p.minX, p.minY, p.maxX, p.maxY);
}

// perform the indexing
index.finish();

// make a bounding box query
const found = index.search(minX, minY, maxX, maxY).map((i) => items[i]);

// make a k-nearest-neighbors query
const neighborIds = index.neighbors(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 = Flatbush.from(e.data);

Install

Install using NPM (npm install flatbush) or Yarn (yarn add flatbush), then:

// import as an ES module
import Flatbush from 'flatbush';

// or require as a CommonJS module
const Flatbush = require('flatbush');

Or use a browser build directly:

<script src="https://unpkg.com/[email protected]/flatbush.min.js"></script>

API

new Flatbush(numItems[, nodeSize, ArrayType])

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

  • nodeSize: size of the tree node (16 by default); experiment with different values for best performance (increasing this value makes indexing faster and queries slower, and vise versa).
  • ArrayType: the array type used for coordinates storage (Float64Array by default); other types may be faster in certain cases (e.g. Int32Array when your data is integer).

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

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

index.finish()

Performs indexing of the added rectangles. Their number must match the one provided when creating a Flatbush object.

index.search(minX, minY, maxX, maxY[, filterFn])

Returns an array of indices of items in a given bounding box. Item indices refer to the value returned by index.add().

const ids = index.search(10, 10, 20, 20);

If given a filterFn, calls it on every found item (passing an item index) and only includes it if the function returned a truthy value.

const ids = index.search(10, 10, 20, 20, (i) => items[i].foo === 'bar');

index.neighbors(x, y[, maxResults, maxDistance, filterFn])

Returns an array of item indices in order of distance from the given x, y (known as K nearest neighbors, or KNN). Item indices refer to the value returned by index.add().

const ids = index.neighbors(10, 10, 5); // returns 5 ids

maxResults and maxDistance are Infinity by default. Also accepts a filterFn similar to index.search.

Flatbush.from(data)

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

Properties

  • data: array buffer that holds the index.
  • minX, minY, maxX, maxY: bounding box of the data.
  • numItems: number of stored items.
  • nodeSize: number of items in a node tree.
  • ArrayType: array type used for internal coordinates storage.
  • IndexArrayType: array type used for internal item indices storage.

Performance

Running npm run bench with Node v10.11.0:

bench flatbush rbush
index 1,000,000 rectangles 263ms 1208ms
1000 searches 10% 594ms 1105ms
1000 searches 1% 68ms 213ms
1000 searches 0.01% 9ms 27ms
1000 searches of 100 neighbors 29ms 58ms
1 search of 1,000,000 neighbors 148ms 781ms
100,000 searches of 1 neighbor 870ms 1548ms
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].