@@ -26,13 +26,14 @@ const OFFSET_ZOOM = 2;
26
26
const OFFSET_ID = 3 ;
27
27
const OFFSET_PARENT = 4 ;
28
28
const OFFSET_NUM = 5 ;
29
- const OFFSET_PROP = 6 ;
29
+ const OFFSET_WEIGHT = 6 ;
30
+ const OFFSET_PROP = 7 ;
30
31
31
32
export default class Supercluster {
32
33
constructor ( options ) {
33
34
this . options = Object . assign ( Object . create ( defaultOptions ) , options ) ;
34
35
this . trees = new Array ( this . options . maxZoom + 1 ) ;
35
- this . stride = this . options . reduce ? 7 : 6 ;
36
+ this . stride = OFFSET_PROP + ( this . options . reduce ? 1 : 0 ) ;
36
37
this . clusterProps = [ ] ;
37
38
}
38
39
@@ -62,7 +63,8 @@ export default class Supercluster {
62
63
Infinity , // the last zoom the point was processed at
63
64
i , // index of the source feature in the original input array
64
65
- 1 , // parent cluster id
65
- 1 // number of points in a cluster
66
+ 1 , // number of points in a cluster
67
+ p . properties . weight ?? 1 // cluster weight
66
68
) ;
67
69
if ( this . options . reduce ) data . push ( 0 ) ; // noop
68
70
}
@@ -295,17 +297,23 @@ export default class Supercluster {
295
297
const numPointsOrigin = data [ i + OFFSET_NUM ] ;
296
298
let numPoints = numPointsOrigin ;
297
299
300
+ const pointsWeightOrigin = data [ i + OFFSET_WEIGHT ] ;
301
+ let pointsWeight = pointsWeightOrigin ;
302
+
298
303
// count the number of points in a potential cluster
299
304
for ( const neighborId of neighborIds ) {
300
305
const k = neighborId * stride ;
301
306
// filter out neighbors that are already processed
302
- if ( data [ k + OFFSET_ZOOM ] > zoom ) numPoints += data [ k + OFFSET_NUM ] ;
307
+ if ( data [ k + OFFSET_ZOOM ] > zoom ) {
308
+ numPoints += data [ k + OFFSET_NUM ] ;
309
+ pointsWeight += data [ k + OFFSET_WEIGHT ] ;
310
+ }
303
311
}
304
312
305
313
// if there were neighbors to merge, and there are enough points to form a cluster
306
314
if ( numPoints > numPointsOrigin && numPoints >= minPoints ) {
307
- let wx = x * numPointsOrigin ;
308
- let wy = y * numPointsOrigin ;
315
+ let wx = x * pointsWeightOrigin ;
316
+ let wy = y * pointsWeightOrigin ;
309
317
310
318
let clusterProperties ;
311
319
let clusterPropIndex = - 1 ;
@@ -319,9 +327,9 @@ export default class Supercluster {
319
327
if ( data [ k + OFFSET_ZOOM ] <= zoom ) continue ;
320
328
data [ k + OFFSET_ZOOM ] = zoom ; // save the zoom (so it doesn't get processed twice)
321
329
322
- const numPoints2 = data [ k + OFFSET_NUM ] ;
323
- wx += data [ k ] * numPoints2 ; // accumulate coordinates for calculating weighted center
324
- wy += data [ k + 1 ] * numPoints2 ;
330
+ const pointsWeight2 = data [ k + OFFSET_WEIGHT ] ;
331
+ wx += data [ k ] * pointsWeight2 ; // accumulate coordinates for calculating weighted position
332
+ wy += data [ k + 1 ] * pointsWeight2 ;
325
333
326
334
data [ k + OFFSET_PARENT ] = id ;
327
335
@@ -336,7 +344,7 @@ export default class Supercluster {
336
344
}
337
345
338
346
data [ i + OFFSET_PARENT ] = id ;
339
- nextData . push ( wx / numPoints , wy / numPoints , Infinity , id , - 1 , numPoints ) ;
347
+ nextData . push ( wx / pointsWeight , wy / pointsWeight , Infinity , id , - 1 , numPoints , pointsWeight ) ;
340
348
if ( reduce ) nextData . push ( clusterPropIndex ) ;
341
349
342
350
} else { // left points as unclustered
0 commit comments