forked from acanimal/AnimatedCluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedCluster.js
384 lines (348 loc) · 15.5 KB
/
AnimatedCluster.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
/* Copyright (c) 2013 by Antonio Santiago <asantiagop_at_gmail_dot_com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of OpenLayers Contributors.
*/
/**
* @requires OpenLayers/Strategy/Cluster.js
*/
/**
* Class: OpenLayers.Strategy.AnimatedCluster
* Cluster strategy for vector layers with animations.
*
* Inherits from:
* - <OpenLayers.Strategy.Cluster>
*/
OpenLayers.Strategy.AnimatedCluster = OpenLayers.Class(OpenLayers.Strategy.Cluster, {
/**
* APIProperty: animationMethod
* {<OpenLayers.Easing>(Function)} Easing equation used for the animation
* Defaultly set to OpenLayers.Easing.Expo.easeOut
*/
animationMethod: OpenLayers.Easing.Expo.easeOut,
/**
* APIProperty: animationDuration
* {Integer} The number of steps to be passed to the OpenLayers.Tween.start()
* method when the clusters are animated.
* Default is 20.
*/
animationDuration: 20,
/**
* Property: animationTween
* {OpenLayers.Tween} Animated panning tween object.
*/
animationTween: null,
/**
* Property: previousResolution
* {Float} The previous resolution of the map.
*/
previousResolution: null,
/**
* Property: previousClusters
* {Array(<OpenLayers.Feature.Vector>)} Clusters of features at previous
* resolution.
*/
previousClusters: null,
/**
* Property: animating
* {Boolean} Indicates if we are in the process of clusters animation.
*/
animating: false,
/**
* Property: zoomIn
* {Boolean} Indicates if we are zooming in or zooming out.
*/
zoomIn: true,
/**
* Constructor: OpenLayers.Strategy.AnimatedCluster
* Create a new animation clustering strategy.
*
* Parameters:
* options - {Object} Optional object whose properties will be set on the
* instance.
*/
initialize: function(options) {
OpenLayers.Strategy.Cluster.prototype.initialize.apply(this, arguments);
if(options.animationMethod) {
this.animationMethod = options.animationMethod;
}
},
/**
* Method: destroy
* Free resources.
*/
destroy: function() {
if(this.animationTween) {
this.animationTween.stop();
this.animationTween = null;
}
},
/**
* 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.
*/
cluster: function(event) {
var resolution = this.layer.map.getResolution();
var isPan = (event && event.type=="moveend" && !event.zoomChanged);
// Each time clusters are animated we need to call layer.redraw to show
// position changes. This produces layer will be redrawn and a call to
// cluster is made.
// Because this, ff we are animating clusters and zoom didn't changed, simply return.
if(this.animating && (resolution == this.resolution)) {
return;
}
if((!event || event.zoomChanged || isPan) && this.features) {
if(resolution != this.resolution || !this.clustersExist() || isPan) {
if(resolution != this.resolution) {
this.zoomIn = (!this.resolution || (resolution <= this.resolution));
}
// Store previous data if we are changing zoom level
this.previousResolution = this.resolution;
this.previousClusters = this.clusters;
this.resolution = resolution;
var clusters = [];
var feature, clustered, cluster;
for(var i=0; i<this.features.length; ++i) {
feature = this.features[i];
// Check if the feature's geometry is on the map's viewport,
// if so then manages it, otherwise ignore.
if(this.layer && this.layer.map) {
var screenBounds = this.layer.map.getExtent();
var featureBounds = feature.geometry.getBounds();
if(!screenBounds.intersectsBounds(featureBounds)) {
continue;
}
}
if(feature.geometry) {
// Cluster for the current resolution
clustered = false;
for(var j=clusters.length-1; j>=0; --j) {
cluster = clusters[j];
if(this.shouldCluster(cluster, feature)) {
this.addToCluster(cluster, feature);
clustered = true;
break;
}
}
if(!clustered) {
clusters.push(this.createCluster(this.features[i]));
}
}
}
// Apply threshold for cluster at current resolution
if(clusters.length > 0) {
if(this.threshold > 1) {
var clone = clusters.slice();
clusters = [];
var candidate;
for(var i=0, len=clone.length; i<len; ++i) {
candidate = clone[i];
if(candidate.attributes.count < this.threshold) {
Array.prototype.push.apply(clusters, candidate.cluster);
} else {
clusters.push(candidate);
}
}
}
}
this.clusters = clusters;
this.clustering = true;
// Add clusters features to the layer
this.layer.removeAllFeatures();
// A legitimate feature addition could occur during this
// addFeatures call. For clustering to behave well, features
// should be removed from a layer before requesting a new batch.
if(this.zoomIn || !this.previousClusters) {
this.layer.addFeatures(this.clusters);
} else {
this.layer.addFeatures(this.previousClusters);
}
this.clustering = false;
// Get the initial and final position of each cluster required
// make the animation
if(this.clusters.length > 0 && this.previousClusters) {
// Before clustering stop any animation
if(this.animationTween) {
this.animationTween.stop();
}
var clustersA, clustersB;
if(this.zoomIn) {
clustersA = this.clusters;
clustersB = this.previousClusters;
} else {
clustersA = this.previousClusters;
clustersB = this.clusters;
}
for(var i=0; i< clustersA.length; i++) {
var ca = clustersA[i];
var caFeatures = ca.cluster || [ca]; // either a cluster of features or a single feature
var cb = this.findFeaturesInClusters(caFeatures, clustersB);
if(cb) {
ca._geometry = {};
if(this.zoomIn) {
ca._geometry.origx = cb.geometry.x;
ca._geometry.origy = cb.geometry.y;
ca._geometry.destx = ca.geometry.x;
ca._geometry.desty = ca.geometry.y;
ca.geometry.x = ca._geometry.origx;
ca.geometry.y = ca._geometry.origy;
} else {
ca._geometry.origx = ca.geometry.x;
ca._geometry.origy = ca.geometry.y;
ca._geometry.destx = cb.geometry.x;
ca._geometry.desty = cb.geometry.y;
}
}
}
// If we are panning then don't animate the cluster
if(isPan && !this.animating){
// Make sure that layer gets redrawn, even if it is just a pan.
this.layer.redraw();
return;
}
// Make animation
if(!this.animationTween) {
this.animationTween = new OpenLayers.Tween(this.animationMethod);
}
this.animating = true;
this.animationTween.start({
x: 0.0,
y: 0.0
}, {
x: 1.0,
y: 1.0
}, this.animationDuration, {
callbacks: {
eachStep: OpenLayers.Function.bind(this.animate, this),
done: OpenLayers.Function.bind(function(delta){
this.animate(delta);
// Remove the temporal attributes
var clusters = this.zoomIn ? this.clusters : this.previousClusters;
for(var i=0; i< clusters.length; i++) {
// if is this really a cluster and not a feature
if (clusters[i].attributes.count) {
if (clusters[i].cluster._geometry) {
delete clusters[i].cluster._geometry;
} else if (clusters[i]._geometry) {
delete clusters[i]._geometry;
}
}
}
// If zooming out then remove the previous cluster
// and the current one
if(!this.zoomIn) {
this.clustering = true;
this.layer.removeFeatures(this.previousClusters);
this.layer.addFeatures(this.clusters);
this.clustering = false;
}
this.animating = false;
}, this)
}
});
}
}
}
},
/**
* Method: findFeaturesInClusters
* Given a set of features and an array of clusters returns the cluster
* where the features are located.
*
* Parameters:
* features - {Array} An array of <OpenLayers.Feature.Vector>.
* clusters - A cluster as an array of <OpenLayers.Feature.Vector>.
*
* Returns:
* {<OpenLayers.Feature.Vector>} The cluster where the first feature of
* the feature array is found.
*/
findFeaturesInClusters: function(features, clusters) {
for(var i=0; i<features.length; i++) {
var feature = features[i];
for(var j=0; j<clusters.length; j++) {
var cluster = clusters[j];
// if cluster is really cluster not a feature
if (cluster.attributes.count) {
var clusterFeatures = clusters[j].cluster;
for(var k=0; k<clusterFeatures.length; k++) {
if(feature.id == clusterFeatures[k].id) {
return cluster;
}
}
}
}
}
return null;
},
/**
* APIMethod: animate
* Animates the clusters changing its position.
*
* Parameters:
* delta - {Object} Object with x-y values with the new increments to
* be applied.
*/
animate: function(delta) {
var clusters = this.zoomIn ? this.clusters : this.previousClusters;
for(var i=0; i<clusters.length; i++) {
if(!clusters[i]._geometry) continue;
var dx = (clusters[i]._geometry.destx - clusters[i]._geometry.origx) * delta.x;
var dy = (clusters[i]._geometry.desty - clusters[i]._geometry.origy) * delta.y;
clusters[i].geometry.x = clusters[i]._geometry.origx + dx;
clusters[i].geometry.y = clusters[i]._geometry.origy + dy;
}
this.layer.redraw();
},
/**
* Method: shouldCluster
* Determine whether to include a feature in a given cluster.
*
* Parameters:
* cluster - {<OpenLayers.Feature.Vector>} A cluster.
* feature - {<OpenLayers.Feature.Vector>} A feature.
* previousResolution - {Boolean} Indicates if the check must be made with
* the current or previous resolution value.
*
* Returns:
* {Boolean} The feature should be included in the cluster.
*/
shouldCluster: function(cluster, feature, previousResolution) {
var res = previousResolution ? this.previousResolution : this.resolution;
var cc = cluster.geometry.getBounds().getCenterLonLat();
var fc = feature.geometry.getBounds().getCenterLonLat();
var distance = (
Math.sqrt(
Math.pow((cc.lon - fc.lon), 2) + Math.pow((cc.lat - fc.lat), 2)
) / res
);
return (distance <= this.distance);
},
CLASS_NAME: "OpenLayers.Strategy.AnimatedCluster"
});