GeoJSON implementation of Flatbush - A really fast static spatial index.

Usage no npm install needed!

<script type="module">
  import geojsonFlatbush from '';


GeoJSON Flatbush

build coverage npm code Greenkeeper badge

GeoJSON implementation of Flatbush — A really fast static spatial index for 2D points and rectangles in JavaScript.



npm install --save geojson-flatbush


This is a simple example that populates the index with lines and queries the index with a polygon. Note, for this example, Turf must be installed npm install @turf/turf

// Initialize GeojsonFlatbush with features.
const index = new GeojsonFlatbush(2)

// Add feature collection to index.
const collection = turf.featureCollection([
  turf.lineString([[0, 0], [1, 1]]),
  turf.lineString([[0, 1], [1, 2]]),

// Perform the indexing.

// Query with polygon.
const polygon = turf.polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
const found =, collection);

// Find k-nearest neighbors and return IDs instead features
const ids = index.neighbors(point, 5);

// Do something with found feature.
found.features.forEach((feature) => {

// Reconstruct the index from a raw array buffer
const newindex = GeojsonFlatbush.from(;


For a full listing of all available API options see the documentation page. The following methods are available:

const index = new GeojsonFlatbush(numItems, [nodeSize], [ArrayType])
  // numItems: Number of items to index
  // nodeSize: Size of the tree node (16 by default)
  // ArrayType: the array type used for coordinates storage (Float64Array by default)

index.add(GeoJSON)                       // Add GeoJSON to index
index.finish()                           // Perform indexing
index.load(FeatureCollection)            // Load feature collection or array
index.neighbors(point, k)                // Find k-nearest neighbors from point, FeatureCollection) // Find features within bounding box
GeojsonFlatbush.from(         // Create new index from raw data


  • GeojsonFlatbush is really just a wrapper to Flatbush but just more GeoJSON friendly.

  • GeojsonFlatbush, just like its underlying spatial index, Flatbush, does not store the original data when using add() or load(). It only stores a bounding box and its ID (i.e., the order in which the data was added).

  • The query methods (neighbors, search) will return a FeatureCollection if a source collection is provided, but the order of the features in the source collection must match the order in which the original data was added.

  • It is probably not a good idea to combine using add and load since the order of the data will be needed to map the query output IDs to the original features. It is best to just add all your data to a single FeatureCollection for loading and querying. Unless you want to keep track of the ID order.


To build and test the library locally:

npm install
npm test


Copyright (c) 2019 Daniel Pulido

Source code is released under the ISC License.