我一直在按照本教程学习如何使用Quadtree数据结构在我一直在创建的d3地图上进行绘制。可以在这里找到该教程:https://www.phase2technology.com/blog/using-d3-quadtrees-power-interactive-map-bonnier-corporation
从本质上讲,我一直在关注本教程,它对您有很大的帮助。我的数据与本教程中使用的数据格式不完全匹配,尝试进行排序时遇到了一些麻烦。
let clusterPoints = [];
let clusterRange = 45;
// for this data structure,
// we need to return an Array of Arrays! (important!)
let latlongpoints = data.map(function(x) {
let point = [
x.LATITUDE,
x.LONGITUDE
];
return point;
});
console.log(latlongpoints); // should be array of arrays
let qTree = d3.quadtree()
.addAll(latlongpoints); // adding points to quadtree
console.log(qTree);
console.log('before for loop');
for (let a = 0; a <= width; a += clusterRange) {
for (let b = 0; b <= height; b += clusterRange) {
let searched = search(qTree, a, b, a + clusterRange, b + clusterRange);
console.log(searched); // only (3) is working, what gives?
let centerPoint = searched.reduce(function(prev, current) {
return [prev[0] + current[0], prev[1] + current[1]];
}, [0, 0]);
centerPoint[0] = centerPoint[0] / searched.length;
centerPoint[1] = centerPoint[1] / searched.length;
centerPoint.push(searched);
if (centerPoint[0] && centerPoint[1]) {
clusterPoints.push(centerPoint);
}
}
}
latlongpoints
返回一个包含lat和long的数组,像这样。
// Find the nodes within the specified rectangle.
function search(quadtree, x0, y0, x3, y3) {
let validData = [];
quadtree.visit(function(node, x1, y1, x2, y2) {
var p = node.data;
if (p) {
p.selected = (p[0] >= x0) && (p[0] < x3) && (p[1] >= y0) && (p[1] < y3);
if (p.selected) {
validData.push(p);
}
}
return x1 >= x3 || y1 >= y3 || x2 < x0 || y2 < y0;
});
return validData;
}
我的for循环中searched
变量的控制台输出结果是仅(3)返回了任何数据,而我不确定100%为什么。这是数据转储。
也许有人可以向我正确的方向推动我为什么获得此输出。如果您查看这篇文章,我基本上会遵循其中的所有步骤。
谢谢!