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
npm install @turf/turf
// Initialize GeojsonFlatbush with features.const index = 2// Add feature collection to index.const collection = turf;index;// Perform the indexing.index;// Query with polygon.const polygon = turf;const found = index;// Find k-nearest neighbors and return IDs instead featuresconst ids = index;// Do something with found feature.foundfeatures;// Reconstruct the index from a raw array bufferconst newindex = GeojsonFlatbush;
For a full listing of all available API options see the documentation page. The following methods are available:
const index = 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 to indexindex // Perform indexingindex // Load feature collection or arrayindex // Find k-nearest neighbors from pointindex // Find features within bounding boxGeojsonFlatbush // Create new index from raw data
GeojsonFlatbushis really just a wrapper to
Flatbushbut just more GeoJSON friendly.
GeojsonFlatbush, just like its underlying spatial index,
Flatbush, does not store the original data when using
load(). It only stores a bounding box and its ID (i.e., the order in which the data was added).
The query methods (
search) will return a
FeatureCollectionif 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
loadsince 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
FeatureCollectionfor loading and querying. Unless you want to keep track of the ID order.
To build and test the library locally:
npm installnpm test
Copyright (c) 2019 Daniel Pulido mailto:firstname.lastname@example.org
Source code is released under the ISC License.