-
Notifications
You must be signed in to change notification settings - Fork 298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor code #37
Merged
Merged
refactor code #37
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,26 +42,24 @@ SuperCluster.prototype = { | |
|
||
this.points = points; | ||
|
||
// generate a cluster object for each point | ||
// generate a cluster object for each point and index input points into a KD-tree | ||
var clusters = points.map(createPointCluster); | ||
this.trees[this.options.maxZoom + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); | ||
|
||
if (log) console.timeEnd(timerId); | ||
|
||
// cluster points on max zoom, then cluster the results on previous zoom, etc.; | ||
// results in a cluster hierarchy across zoom levels | ||
for (var z = this.options.maxZoom; z >= this.options.minZoom; z--) { | ||
var now = +Date.now(); | ||
|
||
// index input points into a KD-tree | ||
this.trees[z + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); | ||
|
||
clusters = this._cluster(clusters, z); // create a new set of clusters for the zoom | ||
// create a new set of clusters for the zoom and index input points into a KD-tree | ||
clusters = this._cluster(clusters, z); | ||
this.trees[z] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); | ||
|
||
if (log) console.log('z%d: %d clusters in %dms', z, clusters.length, +Date.now() - now); | ||
} | ||
|
||
// index top-level clusters | ||
this.trees[this.options.minZoom] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); | ||
|
||
if (log) console.timeEnd('total time'); | ||
|
||
return this; | ||
|
@@ -81,10 +79,10 @@ SuperCluster.prototype = { | |
getChildren: function (clusterId, clusterZoom) { | ||
var origin = this.trees[clusterZoom + 1].points[clusterId]; | ||
var r = this.options.radius / (this.options.extent * Math.pow(2, clusterZoom)); | ||
var points = this.trees[clusterZoom + 1].within(origin.x, origin.y, r); | ||
var ids = this.trees[clusterZoom + 1].within(origin.x, origin.y, r); | ||
var children = []; | ||
for (var i = 0; i < points.length; i++) { | ||
var c = this.trees[clusterZoom + 1].points[points[i]]; | ||
for (var i = 0; i < ids.length; i++) { | ||
var c = this.trees[clusterZoom + 1].points[ids[i]]; | ||
if (c.parentId === clusterId) { | ||
children.push(c.numPoints ? getClusterJSON(c) : this.points[c.id]); | ||
} | ||
|
@@ -218,26 +216,27 @@ SuperCluster.prototype = { | |
|
||
for (var j = 0; j < neighborIds.length; j++) { | ||
var b = tree.points[neighborIds[j]]; | ||
// filter out neighbors that are too far or already processed | ||
if (zoom < b.zoom) { | ||
var numPoints2 = b.numPoints || 1; | ||
b.zoom = zoom; // save the zoom (so it doesn't get processed twice) | ||
wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center | ||
wy += b.y * numPoints2; | ||
numPoints += numPoints2; | ||
b.parentId = i; | ||
|
||
if (this.options.reduce) { | ||
this._accumulate(clusterProperties, b); | ||
} | ||
// filter out neighbors that already processed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "that are already processed" |
||
if (b.zoom <= zoom) continue; | ||
b.zoom = zoom; // save the zoom (so it doesn't get processed twice) | ||
|
||
var numPoints2 = b.numPoints || 1; | ||
wx += b.x * numPoints2; // accumulate coordinates for calculating weighted center | ||
wy += b.y * numPoints2; | ||
|
||
numPoints += numPoints2; | ||
b.parentId = i; | ||
|
||
if (this.options.reduce) { | ||
this._accumulate(clusterProperties, b); | ||
} | ||
} | ||
|
||
if (numPoints === 1) { | ||
clusters.push(p); | ||
} else { | ||
p.parentId = i; | ||
clusters.push(createCluster(wx / numPoints, wy / numPoints, numPoints, i, clusterProperties)); | ||
clusters.push(createCluster(wx / numPoints, wy / numPoints, i, numPoints, clusterProperties)); | ||
} | ||
} | ||
|
||
|
@@ -253,15 +252,15 @@ SuperCluster.prototype = { | |
} | ||
}; | ||
|
||
function createCluster(x, y, numPoints, id, properties) { | ||
function createCluster(x, y, id, numPoints, properties) { | ||
return { | ||
x: x, // weighted cluster center | ||
y: y, | ||
zoom: Infinity, // the last zoom the cluster was processed at | ||
id: id, // index of the first child of the cluster in the zoom level tree | ||
properties: properties, | ||
parentId: -1, // parent cluster id | ||
numPoints: numPoints | ||
numPoints: numPoints, | ||
properties: properties | ||
}; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we index new clusters and not input points at this stage, we could say: