From acea7483a819cfcec22ee64fb34a2109c6075e6e Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Mon, 19 Jun 2017 13:20:14 +1000 Subject: [PATCH 1/3] ignore features with null geometry --- index.js | 6 +++++- test/fixtures/places.json | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index db42162..4d5ccd1 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,7 @@ SuperCluster.prototype = { this.points = points; // generate a cluster object for each point and index input points into a KD-tree - var clusters = points.map(createPointCluster); + var clusters = points.map(createPointCluster).filter(function (i) { return i !== null; }); this.trees[this.options.maxZoom + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); if (log) console.timeEnd(timerId); @@ -265,6 +265,10 @@ function createCluster(x, y, id, numPoints, properties) { } function createPointCluster(p, id) { + if (!p.geometry) { + return null; + } + var coords = p.geometry.coordinates; return { x: lngX(coords[0]), // projected point coordinates diff --git a/test/fixtures/places.json b/test/fixtures/places.json index cd1aee1..a327d36 100644 --- a/test/fixtures/places.json +++ b/test/fixtures/places.json @@ -2916,6 +2916,13 @@ "type": "Point", "coordinates": [-64.74290930897877, 32.31726715702533] } + }, + { + "type": "Feature", + "properties": { + "name": "Null Island" + }, + "geometry": null } ] } From 6fc9f9858a58deb20a1e0f3ce53ba9a66f53b9ce Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Mon, 19 Jun 2017 20:44:52 +1000 Subject: [PATCH 2/3] use a simple loop to filter out null geometries --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4d5ccd1..435e240 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,13 @@ SuperCluster.prototype = { this.points = points; // generate a cluster object for each point and index input points into a KD-tree - var clusters = points.map(createPointCluster).filter(function (i) { return i !== null; }); + var clusters = []; + points.forEach(function (point, i) { + var pointCluster = createPointCluster(point, i); + if (pointCluster !== null) { + clusters.push(pointCluster); + } + }); this.trees[this.options.maxZoom + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); if (log) console.timeEnd(timerId); From 64e0c9673dc83262c23375eb65b9d63f50b1418b Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Mon, 19 Jun 2017 21:19:16 +1000 Subject: [PATCH 3/3] use for rather than forEach --- index.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 435e240..38746a9 100644 --- a/index.js +++ b/index.js @@ -44,12 +44,12 @@ SuperCluster.prototype = { // generate a cluster object for each point and index input points into a KD-tree var clusters = []; - points.forEach(function (point, i) { - var pointCluster = createPointCluster(point, i); - if (pointCluster !== null) { - clusters.push(pointCluster); + for (var i = 0; i < points.length; i++) { + if (!points[i].geometry) { + continue; } - }); + clusters.push(createPointCluster(points[i], i)); + } this.trees[this.options.maxZoom + 1] = kdbush(clusters, getX, getY, this.options.nodeSize, Float32Array); if (log) console.timeEnd(timerId); @@ -271,10 +271,6 @@ function createCluster(x, y, id, numPoints, properties) { } function createPointCluster(p, id) { - if (!p.geometry) { - return null; - } - var coords = p.geometry.coordinates; return { x: lngX(coords[0]), // projected point coordinates