Cluster.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Strategy.js
  7. */
  8. /**
  9. * Class: OpenLayers.Strategy.Cluster
  10. * Strategy for vector feature clustering.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Strategy>
  14. */
  15. OpenLayers.Strategy.Cluster = OpenLayers.Class(OpenLayers.Strategy, {
  16. /**
  17. * APIProperty: distance
  18. * {Integer} Pixel distance between features that should be considered a
  19. * single cluster. Default is 20 pixels.
  20. */
  21. distance: 20,
  22. /**
  23. * APIProperty: threshold
  24. * {Integer} Optional threshold below which original features will be
  25. * added to the layer instead of clusters. For example, a threshold
  26. * of 3 would mean that any time there are 2 or fewer features in
  27. * a cluster, those features will be added directly to the layer instead
  28. * of a cluster representing those features. Default is null (which is
  29. * equivalent to 1 - meaning that clusters may contain just one feature).
  30. */
  31. threshold: null,
  32. /**
  33. * Property: features
  34. * {Array(<OpenLayers.Feature.Vector>)} Cached features.
  35. */
  36. features: null,
  37. /**
  38. * Property: clusters
  39. * {Array(<OpenLayers.Feature.Vector>)} Calculated clusters.
  40. */
  41. clusters: null,
  42. /**
  43. * Property: clustering
  44. * {Boolean} The strategy is currently clustering features.
  45. */
  46. clustering: false,
  47. /**
  48. * Property: resolution
  49. * {Float} The resolution (map units per pixel) of the current cluster set.
  50. */
  51. resolution: null,
  52. /**
  53. * Constructor: OpenLayers.Strategy.Cluster
  54. * Create a new clustering strategy.
  55. *
  56. * Parameters:
  57. * options - {Object} Optional object whose properties will be set on the
  58. * instance.
  59. */
  60. /**
  61. * APIMethod: activate
  62. * Activate the strategy. Register any listeners, do appropriate setup.
  63. *
  64. * Returns:
  65. * {Boolean} The strategy was successfully activated.
  66. */
  67. activate: function() {
  68. var activated = OpenLayers.Strategy.prototype.activate.call(this);
  69. if(activated) {
  70. this.layer.events.on({
  71. "beforefeaturesadded": this.cacheFeatures,
  72. "moveend": this.cluster,
  73. scope: this
  74. });
  75. }
  76. return activated;
  77. },
  78. /**
  79. * APIMethod: deactivate
  80. * Deactivate the strategy. Unregister any listeners, do appropriate
  81. * tear-down.
  82. *
  83. * Returns:
  84. * {Boolean} The strategy was successfully deactivated.
  85. */
  86. deactivate: function() {
  87. var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
  88. if(deactivated) {
  89. this.clearCache();
  90. this.layer.events.un({
  91. "beforefeaturesadded": this.cacheFeatures,
  92. "moveend": this.cluster,
  93. scope: this
  94. });
  95. }
  96. return deactivated;
  97. },
  98. /**
  99. * Method: cacheFeatures
  100. * Cache features before they are added to the layer.
  101. *
  102. * Parameters:
  103. * event - {Object} The event that this was listening for. This will come
  104. * with a batch of features to be clustered.
  105. *
  106. * Returns:
  107. * {Boolean} False to stop features from being added to the layer.
  108. */
  109. cacheFeatures: function(event) {
  110. var propagate = true;
  111. if(!this.clustering) {
  112. this.clearCache();
  113. this.features = event.features;
  114. this.cluster();
  115. propagate = false;
  116. }
  117. return propagate;
  118. },
  119. /**
  120. * Method: clearCache
  121. * Clear out the cached features.
  122. */
  123. clearCache: function() {
  124. this.features = null;
  125. },
  126. /**
  127. * Method: cluster
  128. * Cluster features based on some threshold distance.
  129. *
  130. * Parameters:
  131. * event - {Object} The event received when cluster is called as a
  132. * result of a moveend event.
  133. */
  134. cluster: function(event) {
  135. if((!event || event.zoomChanged) && this.features) {
  136. var resolution = this.layer.map.getResolution();
  137. if(resolution != this.resolution || !this.clustersExist()) {
  138. this.resolution = resolution;
  139. var clusters = [];
  140. var feature, clustered, cluster;
  141. for(var i=0; i<this.features.length; ++i) {
  142. feature = this.features[i];
  143. if(feature.geometry) {
  144. clustered = false;
  145. for(var j=clusters.length-1; j>=0; --j) {
  146. cluster = clusters[j];
  147. if(this.shouldCluster(cluster, feature)) {
  148. this.addToCluster(cluster, feature);
  149. clustered = true;
  150. break;
  151. }
  152. }
  153. if(!clustered) {
  154. clusters.push(this.createCluster(this.features[i]));
  155. }
  156. }
  157. }
  158. this.layer.removeAllFeatures();
  159. if(clusters.length > 0) {
  160. if(this.threshold > 1) {
  161. var clone = clusters.slice();
  162. clusters = [];
  163. var candidate;
  164. for(var i=0, len=clone.length; i<len; ++i) {
  165. candidate = clone[i];
  166. if(candidate.attributes.count < this.threshold) {
  167. Array.prototype.push.apply(clusters, candidate.cluster);
  168. } else {
  169. clusters.push(candidate);
  170. }
  171. }
  172. }
  173. this.clustering = true;
  174. // A legitimate feature addition could occur during this
  175. // addFeatures call. For clustering to behave well, features
  176. // should be removed from a layer before requesting a new batch.
  177. this.layer.addFeatures(clusters);
  178. this.clustering = false;
  179. }
  180. this.clusters = clusters;
  181. }
  182. }
  183. },
  184. /**
  185. * Method: clustersExist
  186. * Determine whether calculated clusters are already on the layer.
  187. *
  188. * Returns:
  189. * {Boolean} The calculated clusters are already on the layer.
  190. */
  191. clustersExist: function() {
  192. var exist = false;
  193. if(this.clusters && this.clusters.length > 0 &&
  194. this.clusters.length == this.layer.features.length) {
  195. exist = true;
  196. for(var i=0; i<this.clusters.length; ++i) {
  197. if(this.clusters[i] != this.layer.features[i]) {
  198. exist = false;
  199. break;
  200. }
  201. }
  202. }
  203. return exist;
  204. },
  205. /**
  206. * Method: shouldCluster
  207. * Determine whether to include a feature in a given cluster.
  208. *
  209. * Parameters:
  210. * cluster - {<OpenLayers.Feature.Vector>} A cluster.
  211. * feature - {<OpenLayers.Feature.Vector>} A feature.
  212. *
  213. * Returns:
  214. * {Boolean} The feature should be included in the cluster.
  215. */
  216. shouldCluster: function(cluster, feature) {
  217. var cc = cluster.geometry.getBounds().getCenterLonLat();
  218. var fc = feature.geometry.getBounds().getCenterLonLat();
  219. var distance = (
  220. Math.sqrt(
  221. Math.pow((cc.lon - fc.lon), 2) + Math.pow((cc.lat - fc.lat), 2)
  222. ) / this.resolution
  223. );
  224. return (distance <= this.distance);
  225. },
  226. /**
  227. * Method: addToCluster
  228. * Add a feature to a cluster.
  229. *
  230. * Parameters:
  231. * cluster - {<OpenLayers.Feature.Vector>} A cluster.
  232. * feature - {<OpenLayers.Feature.Vector>} A feature.
  233. */
  234. addToCluster: function(cluster, feature) {
  235. cluster.cluster.push(feature);
  236. cluster.attributes.count += 1;
  237. },
  238. /**
  239. * Method: createCluster
  240. * Given a feature, create a cluster.
  241. *
  242. * Parameters:
  243. * feature - {<OpenLayers.Feature.Vector>}
  244. *
  245. * Returns:
  246. * {<OpenLayers.Feature.Vector>} A cluster.
  247. */
  248. createCluster: function(feature) {
  249. var center = feature.geometry.getBounds().getCenterLonLat();
  250. var cluster = new OpenLayers.Feature.Vector(
  251. new OpenLayers.Geometry.Point(center.lon, center.lat),
  252. {count: 1}
  253. );
  254. cluster.cluster = [feature];
  255. return cluster;
  256. },
  257. CLASS_NAME: "OpenLayers.Strategy.Cluster"
  258. });