-
Notifications
You must be signed in to change notification settings - Fork 4
/
CenteredCluster.js
585 lines (546 loc) · 20 KB
/
CenteredCluster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/* Copyright 2013-2018 by Xavier Mamano, http://github.com/jorix/OL-Ragbag
* Published under MIT license.
*
* `_groupFeatures` & `_groupClusters` functions and the skeleton of the class
* is based on OpenLayers.Strategy.Cluster class which is
* copyright (c) 2005-2013 by OpenLayers Contributors under the
* 2-clause BSD license http://openlayers.org/dev/license.txt
*/
/**
* @requires OpenLayers/Strategy.js
* @requires OpenLayers/Layer/Vector.js
*/
/**
* Class: OpenLayers.Strategy.CenteredCluster
* Strategy for vector feature clustering.
*
* *NOTE*: This code includes a patch for
* <OpenLayers.Layer.Vector.getDataExtent> function.
*
* Inherits from:
* - <OpenLayers.Strategy>
*/
OpenLayers.Strategy.CenteredCluster = OpenLayers.Class(OpenLayers.Strategy, {
/**
* APIProperty: centered
* {Boolean}
*/
centered: true,
/**
* APIProperty: enabled
* {Boolean}
*/
enabled: true,
/**
* APIProperty: zoomSettings
* {Boolean}
*/
zoomSettings: null,
/**
* APIProperty: candidateMatches
* {Function}
*/
candidateMatches: null,
/**
* APIProperty: distance
* {Integer} Pixel distance between features that should be considered a
* single cluster. Default is 20 pixels.
*/
distance: 20,
/**
* APIProperty: threshold
* {Integer} Optional threshold below which original features will be
* added to the layer instead of clusters. For example, a threshold
* of 3 would mean that any time there are 2 or fewer features in
* a cluster, those features will be added directly to the layer instead
* of a cluster representing those features. Default is null (which is
* equivalent to 1 - meaning that clusters may contain just one feature)
*/
threshold: null,
/**
* Property: features
* {Array(<OpenLayers.Feature.Vector>)} Cached features.
*/
features: null,
/**
* Property: clustering
* {Boolean} The strategy is currently clustering features.
*/
clustering: false,
/**
* Property: resolution
* {Float} The resolution (map units per pixel) of the current cluster set.
*/
resolution: null,
/**
* Property: defaultSettings
* {Object} Internal use only.
*/
defaultSettings: null,
/**
* Property: layerListeners
* {Object} layerListeners object will be registered with
* <OpenLayers.Events.on>, internal use only.
*/
layerListeners: null,
/**
* Constructor: OpenLayers.Strategy.CenteredCluster
* Create a new CenteredCluster strategy.
*
* Parameters:
* options - {Object} Optional object whose properties will be set on the
* instance.
*/
initialize: function(options) {
OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
/*
* Property: _candidateMatches
* {Function} Set by _createClusters method.
*/
var _candidateMatches = null;
/*
* Property: _resDistance2
* {Fload} Set by _createClusters method, and used in _withinDistance.
*/
var _resDistance2 = 0;
/*
* Fuction: _withinDistance
*
* Parameters:
* cluster -{Object}
*
* Returns:
* {Boolean} Is true if the point `x, y` is closer to `0, 0` than the
* set <distance>.
*/
var _withinDistance = function(x, y) {
return (x * x + y * y) <= _resDistance2;
};
/*
* Method: _addFeature
* Add the feature to the cluster
*
* Parameters:
* cluster -{Object}
* feature - {<OpenLayers.Feature.Vector>}
* fCenter - {OpenLayers.LonLat} Center of de feature.
*/
var _addFeature = function(cluster, feature, fCenter) {
if (_withinDistance(cluster.x - fCenter.lon,
cluster.y - fCenter.lat)) {
cluster.f.push(feature);
cluster.sx += fCenter.lon;
cluster.sy += fCenter.lat;
return true;
}
return false;
};
/*
* Method: _centerCluster
*
* Parameters:
* cluster -{Object}
*/
var _centerCluster = function(cluster) {
var len = cluster.f.length;
cluster.x = cluster.sx / len;
cluster.y = cluster.sy / len;
};
/*
* Method: _trimCluster
*
* Parameters:
* rejections - Array({<OpenLayers.Feature.Vector>})
* cluster -{Object} Calculated cluster.
*/
var _trimCluster = function(rejections, cluster) {
var wasRejected = false,
clusterArr = cluster.f;
do {
_centerCluster(cluster);
var rejected = false;
for (var ii = clusterArr.length - 1; ii >= 0; ii--) {
var feature = clusterArr[ii],
fCenter =
feature.geometry.getBounds().getCenterLonLat();
if (!_withinDistance(cluster.x - fCenter.lon,
cluster.y - fCenter.lat)) {
clusterArr.splice(ii, 1);
cluster.sx -= fCenter.lon;
cluster.sy -= fCenter.lat;
rejections.push(feature);
rejected = true;
}
}
wasRejected = wasRejected || rejected;
} while (rejected);
return wasRejected;
};
/*
* Method: _groupFeatures
*
* Parameters:
* clusters -Array({Object}) Initial and calculated clusters at the end.
* features -Array({<OpenLayers.Feature.Vector>}) To cluster features.
*/
var _groupFeatures = function(clusters, features) {
var feature, cluster, clustered, fCenter;
for (var i = 0, len = features.length; i < len; i++) {
feature = features[i];
feature.renderIntent = 'default';
if (feature.geometry) {
fCenter = feature.geometry.getBounds().getCenterLonLat();
clustered = false;
for (var ii = clusters.length - 1; ii >= 0; ii--) {
cluster = clusters[ii];
if (_candidateMatches(cluster.f, feature) &&
_addFeature(cluster, feature, fCenter)) {
clustered = true;
break;
}
}
if (!clustered) {
cluster = {
sx: fCenter.lon,
sy: fCenter.lat,
x: fCenter.lon,
y: fCenter.lat,
f: [feature]
};
clusters.push(cluster);
}
}
}
};
/*
* Method: _groupClusters
*
* Parameters:
* remainingStart - {Integer}
* clusters - Array({Object}) Calculated clusters at the end.
* candidates - Array({<OpenLayers.Feature.Vector>}) Initial calculated
* clusters.
*/
var _groupClusters = function(remainingStart, clusters, candidates) {
var candidate, cluster, clustered, ii, feature, fCenter;
for (var i = 0, len = candidates.length; i < len; i++) {
candidate = candidates[i];
// calculate the center of the cluster candidate.
_centerCluster(candidate);
clustered = false;
for (ii = clusters.length - 1; ii >= remainingStart; ii--) {
cluster = clusters[ii];
if (_withinDistance(cluster.x - candidate.x,
cluster.y - candidate.y) &&
_candidateMatches(cluster.f, candidate.f[0])) {
Array.prototype.push.apply(cluster.f, candidate.f);
cluster.sx += candidate.sx;
cluster.sy += candidate.sy;
clustered = true; // But we will review again.
clusters.splice(ii, 1);
candidates[i] = cluster;
i--;
break;
}
}
if (!clustered) {
for (ii = remainingStart - 1; ii >= 0; ii--) {
cluster = clusters[ii];
if (_withinDistance(cluster.x - candidate.x,
cluster.y - candidate.y) &&
_candidateMatches(cluster.f, candidate.f[0])) {
var cc = candidate.f;
for (var iii = cc.length - 1; iii >= 0; iii--) {
feature = cc[iii];
fCenter = feature.geometry.getBounds()
.getCenterLonLat();
if (_addFeature(cluster, feature, fCenter)) {
cc.splice(iii, 1);
if (candidate.f.length) {
candidate.sx -= fCenter.lon;
candidate.sy -= fCenter.lat;
_centerCluster(candidate);
} else {
clustered = true;
break;
}
}
}
if (clustered) {
break;
}
}
}
if (!clustered) {
clusters.push(candidate);
}
}
}
};
/*
* Method: _createClusters
*
* Parameters:
* resolution - {Fload}
*/
var _self = this;
var _createClusters = function(resolution) {
// Set distance
_resDistance2 = _self.distance * resolution;
_resDistance2 *= _resDistance2;
var candidateMatches = _self.candidateMatches;
if (candidateMatches) {
_candidateMatches = function(a, b) {
return candidateMatches.call(_self, a, b);
};
} else {
_candidateMatches = function() { return true; };
}
var finalClusters = [];
_groupFeatures(finalClusters, _self.features);
var i, len;
if (_self.centered) {
var remainingStart = 0,
remainingClusters;
for (i = 0; i < 3; i++) {
remainingClusters = finalClusters.slice(remainingStart);
finalClusters = finalClusters.slice(0, remainingStart);
_groupClusters(
remainingStart, finalClusters, remainingClusters
);
var rejected = [];
for (var ii = finalClusters.length - 1;
ii >= remainingStart; ii--) {
_trimCluster(rejected, finalClusters[ii]);
}
if (!rejected.length) {
break;
}
remainingStart = finalClusters.length;
_groupFeatures(finalClusters, rejected);
}
}
// We have calculated clusters on `remainingClusters`, publish it.
_self.clustering = true;
_self.layer.removeAllFeatures();
var clusters = [];
if (finalClusters.length > 0) {
for (i = 0, len = finalClusters.length; i < len; i++) {
var candidate = finalClusters[i],
cLen = candidate.f.length;
if (_self.threshold && cLen < _self.threshold) {
Array.prototype.push.apply(clusters, candidate.f);
} else {
var cluster = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(
candidate.x, candidate.y),
{count: cLen}
);
cluster.cluster = candidate.f;
clusters.push(cluster);
}
}
_self.layer.addFeatures(clusters);
}
_self.clustering = false;
};
/**
* Method: cluster
* Cluster features based on some threshold distance.
*
* Parameters:
* event - {Object} The event received when cluster is called as a
* result of a moveend event.
*/
var cluster = function(event) {
if (this.enabled) {
if (!this.features) {
this.features = this.layer.features.slice();
}
if ((!event || event.zoomChanged) && this.features.length) {
var resolution = this.layer.map.getResolution();
if (!event || resolution !== this.resolution) {
this.resolution = resolution;
_createClusters(resolution);
}
}
} else {
if (this.features) {
this.uncluster();
}
}
};
this.cluster = cluster;
// Layer listeners
this.layerListeners = {
'beforefeaturesadded': this.cacheFeatures,
'featuresremoved': this.refreshCache,
'afterfeaturemodified': this.refreshCache,
'moveend': this.onMoveend,
scope: this
};
// Store defaultSettings
this.defaultSettings = {
distance: this.distance,
threshold: this.threshold,
enabled: this.enabled,
centered: this.centered
};
},
/**
* Method: onMoveend
*/
onMoveend: function(event) {
if (event.zoomChanged && this.zoomSettings) {
var zoomSettings = this.zoomSettings,
zoomLevel = this.layer.map.getZoom();
OpenLayers.Util.extend(this, this.defaultSettings);
for (var i = 0, len = zoomSettings.length; i < len; i++) {
var item = zoomSettings[i];
if (zoomLevel >= item.zoomRange[0] &&
zoomLevel <= item.zoomRange[1]) {
OpenLayers.Util.extend(this, item.settings);
break;
}
}
}
this.cluster(event);
},
/**
* APIMethod: activate
* Activate the strategy. Register any listeners, do appropriate setup.
*
* Returns:
* {Boolean} The strategy was successfully activated.
*/
activate: function() {
var activated = OpenLayers.Strategy.prototype.activate.call(this);
if (activated) {
this.cluster();
this.layer.events.on(this.layerListeners);
}
return activated;
},
/**
* APIMethod: deactivate
* Deactivate the strategy. Unregister any listeners, do appropriate
* tear-down.
*
* Returns:
* {Boolean} The strategy was successfully deactivated.
*/
deactivate: function() {
var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
if (deactivated) {
if (this.features) {
this.uncluster();
}
this.layer.events.un(this.layerListeners);
}
return deactivated;
},
/**
* Method: cacheFeatures
* Cache features before they are added to the layer.
*
* Parameters:
* event - {Object} The event that this was listening for. This will come
* with a batch of features to be clustered.
*
* Returns:
* {Boolean} False to stop features from being added to the layer.
*/
cacheFeatures: function(event) {
if (this.clustering) { return; }
if (this.enabled) {
var layerFeatures = this.layer.features,
layerFeaLen = layerFeatures.length,
features;
if (layerFeaLen) {
features = event.features.slice();
for (var i = 0; i < layerFeaLen; i++) {
var feature = layerFeatures[i];
if (feature.cluster) {
Array.prototype.push.apply(features, feature.cluster);
} else {
features.push(feature);
}
}
} else {
features = event.features;
}
this.features = features;
this.cluster();
return false;
} else if (this.features) {
this.uncluster();
}
},
/**
* Method: refreshCache
* Refresh the cached features.
*/
refreshCache: function() {
this.cacheFeatures({features: []});
},
/**
* Method: uncluster
* Uncluster features. Internal use!
*
* Warning: methods that call this function should monitor that
* `this.features` has value.
*/
uncluster: function() {
var features = this.features.slice();
this.features = null;
this.clustering = true;
this.layer.removeAllFeatures();
this.layer.addFeatures(features);
this.clustering = false;
},
CLASS_NAME: 'OpenLayers.Strategy.CenteredCluster'
});
/**
* Class: OpenLayers.Layer.Vector
* Instances of OpenLayers.Layer.Vector are used to render vector data from
* a variety of sources. Create a new vector layer with the
* <OpenLayers.Layer.Vector> constructor.
*
* Inherits from:
* - <OpenLayers.Layer>
*/
/**
* APIMethod: getDataExtent
* Calculates the max extent which includes all of the features, *even if they
* are clustered*.
*
* Returns:
* {<OpenLayers.Bounds>} or null if the layer has no features with
* geometries.
*/
OpenLayers.Layer.Vector.prototype.getDataExtent = function() {
var _maxExtent = null,
features = this.features;
if (features && features.length > 0) {
var extendBounds = function(geometry) {
if (geometry) {
if (_maxExtent === null) {
_maxExtent = new OpenLayers.Bounds();
}
_maxExtent.extend(geometry.getBounds());
}
};
for (var i = 0, len = features.length; i < len; i++) {
var feature = features[i],
cluster = feature.cluster;
if (cluster) {
for (var ii = 0, iilen = cluster.length; ii < iilen; ii++) {
extendBounds(cluster[ii].geometry);
}
} else {
extendBounds(feature.geometry);
}
}
}
return _maxExtent;
};