Skip to content

Commit 4f618cc

Browse files
authored
Merge pull request #1383 from OpenGeoscience/speed-up-clustering
perf: Reduce some clustering computation
2 parents 9209a62 + dc28c89 commit 4f618cc

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/pointFeature.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ var feature = require('./feature');
5656
* @typedef {object} geo.pointFeature.clusteringSpec
5757
* @property {number} [radius=10] This is size in pixels that determines how
5858
* close points need to be to each other to be clustered.
59-
* @property {number} [maxZoom=18] Never cluster above this zoom level.
59+
* @property {number} [maxZoom=18] Never cluster above this zoom level. For a
60+
* point feature associated with a layer and a map, this will default to the
61+
* map's zoomRange().max value.
6062
*/
6163

6264
/**
@@ -138,6 +140,10 @@ var pointFeature = function (arg) {
138140

139141
// set clustering options to default if an options argument wasn't supplied
140142
var opts = m_clustering === true ? {radius: 10} : m_clustering;
143+
if (!opts.maxZoom && this.layer() && this.layer().map()) {
144+
opts = Object.assign({}, opts);
145+
opts.maxZoom = this.layer().map().zoomRange().max;
146+
}
141147

142148
// generate the cluster tree from the raw data
143149
var position = m_this.position();

src/util/clustering.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* hierarchically given an array of length scales (zoom levels).
44
*/
55

6-
var $ = require('jquery');
7-
86
/**
97
* This class manages a group of nearby points that are clustered as a
108
* single object for display purposes. The class constructor is private
@@ -106,10 +104,10 @@ ClusterTree.prototype.each = function (func) {
106104
* @returns {geo.geoPosition} The 2-d coordinates of the center.
107105
*/
108106
ClusterTree.prototype.coords = function () {
109-
var i, center = {x: 0, y: 0};
110107
if (this._coord) {
111108
return this._coord;
112109
}
110+
var i, center = {x: 0, y: 0};
113111
// first add up the points at the node
114112
for (i = 0; i < this._points.length; i += 1) {
115113
center.x += this._points[i].x;
@@ -122,10 +120,11 @@ ClusterTree.prototype.coords = function () {
122120
center.y += this._clusters[i].coords().y * this._clusters[i].count();
123121
}
124122

125-
return {
123+
this._coord = {
126124
x: center.x / this.count(),
127125
y: center.y / this.count()
128126
};
127+
return this._coord;
129128
};
130129

131130
/**
@@ -202,9 +201,6 @@ C.prototype.addPoint = function (point) {
202201
}
203202
}
204203

205-
if (!parent) {
206-
$.noop();
207-
}
208204
// create a new cluster with these two points
209205
newCluster = new ClusterTree(this, zoom, [closest, point]);
210206
this._clusters[zoom].addObject(newCluster, newCluster.coords());

0 commit comments

Comments
 (0)