supercluster
supercluster copied to clipboard
How to store the clustered values into file
I have one question after cluster the values I want to store it in file. after super cluster object initialize I am getting the following response
Supercluster {
options: { log: true, radius: 60, extent: 512, maxZoom: 16 },
trees:
[ KDBush {
nodeSize: 64,
points: [Array],
ids: [Uint16Array],
coords: [Float32Array] },
KDBush {
nodeSize: 64,
points: [Array],
ids: [Uint16Array],
coords: [Float32Array] },
points:
[ { type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] },
{ type: 'Feature', properties: [Object], geometry: [Object] } ] }
When I convert and write into a file. Format is changing
I am using Node js. Before writing into file i am JSON.stringify
After that stringify I am getting response like this
{
"options": {
"log": true,
"radius": 60,
"extent": 512,
"maxZoom": 16
},
"trees": [
{
"nodeSize": 64,
"points": [
{
"x": 0.24396358761574075,
"y": 0.38899943574185264,
"zoom": null,
"id": 1,
"parentId": -1,
"numPoints": 12,
"properties": null
},
{
"x": 0.7360923611111111,
"y": 0.4629351025691785,
"zoom": null,
"id": 33,
"parentId": -1,
"numPoints": 2,
"properties": null
}etc
supercluster name and KDBUSH also missed in that. Please suggest something
let's say r
is the result of your JSON.stringify(...)
, you can just assign all the properties and serialize trees
to KDBush
instances. The example uses a worker to index all the points and returns a serialized Supercluster
.
const r = await workerPool.exec('index', [features]);
if (r?.points && r?.trees) {
r.trees = r.trees.map((t) =>
Object.assign(
new KDBush(
[],
() => 0,
() => 0,
),
t,
),
);
this.clusterIndex = Object.assign(
new Supercluster({
...(r?.options ?? {}),
}),
{
points: r.points,
trees: r.trees,
},
);
this.logger.log(
`Successfully loaded ${features.length} features in ${
(new Date().valueOf() - start) / 1000
}s`,
);
}