mourner / kdbush

A fast static index for 2D points
ISC License
629 stars 69 forks source link

v4.0 — Serializable KDBush #32

Closed mourner closed 1 year ago

mourner commented 4 years ago

Reimplements KDBush to store the whole index along with its properties in a single array buffer for easy transfer and serialization. Closes #31. Closes #25. Closes #23. cc @hydrosquall

Breaks the API to support this in the same way as Flatbush:

// old API
const index = new KDBush(points, p => p.x, p => p.y, 64, Uint32Array);

// new API
const index = new KDBush(points.length, 64, Uint32Array);
for (const {x, y} of points) index.add(x, y);
index.finish();

The new API is more verbose, but it allows creating an index without requiring to store coordinates in some intermediate point array, allowing more efficient memory use.

To do:

hydrosquall commented 4 years ago

Great to see this, thanks @mourner !

cerodell commented 4 years ago

Hello!

Is this new API avaible?

I am trying to build a static KDTree that I can save as a JSON. The static KDTree will then be loaded as a JSON into my site as a reference for indexing for user-defined points. This is possible? I am able to make the JSON but once I load it in I lose the .range extension. Any suggestion would be greatly appreciated :)

Cheers, Chris