Need private packages and team management tools?Check out npm Teams »

geojson-flatbush

0.3.0 • Public • Published

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.

GETTING STARTED

Install

npm install --save geojson-flatbush

Example

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]]),
]);
index.load(collection);
 
// Perform the indexing.
index.finish();
 
// Query with polygon.
const polygon = turf.polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
const found = index.search(polygon, 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) => {
  console.log(feature);
});
 
// Reconstruct the index from a raw array buffer
const newindex = GeojsonFlatbush.from(index.data);

USAGE

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
index.search(GeoJSON, FeatureCollection) // Find features within bounding box
GeojsonFlatbush.from(index.data)         // Create new index from raw data

TIPS

  • 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.

BUILD

To build and test the library locally:

npm install
npm test

LICENSE

Copyright (c) 2019 Daniel Pulido mailto:dpmcmlxxvi@gmail.com

Source code is released under the ISC License.

Install

npm i geojson-flatbush

DownloadsWeekly Downloads

1

Version

0.3.0

License

ISC

Unpacked Size

20.3 kB

Total Files

6

Last publish

Collaborators

  • avatar