Skip to content

Commit b3be95b

Browse files
authored
Return Best Distance (mapbox#61)
* Return best distance. Closes mapbox#2 * Move where -0 distance is precluded, based on feedback from @Fil.
1 parent 7e9b63e commit b3be95b

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

polylabel.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function polylabel(polygon, precision, debug) {
2525
var cellSize = Math.min(width, height);
2626
var h = cellSize / 2;
2727

28-
if (cellSize === 0) return [minX, minY];
28+
if (cellSize === 0) {
29+
var degeneratePoleOfInaccessibility = [minX, minY];
30+
degeneratePoleOfInaccessibility.distance = 0;
31+
return degeneratePoleOfInaccessibility;
32+
}
2933

3034
// a priority queue of cells in order of their "potential" (max distance to polygon)
3135
var cellQueue = new Queue(undefined, compareMax);
@@ -73,7 +77,9 @@ function polylabel(polygon, precision, debug) {
7377
console.log('best distance: ' + bestCell.d);
7478
}
7579

76-
return [bestCell.x, bestCell.y];
80+
var poleOfInaccessibility = [bestCell.x, bestCell.y];
81+
poleOfInaccessibility.distance = bestCell.d;
82+
return poleOfInaccessibility;
7783
}
7884

7985
function compareMax(a, b) {
@@ -107,7 +113,7 @@ function pointToPolygonDist(x, y, polygon) {
107113
}
108114
}
109115

110-
return (inside ? 1 : -1) * Math.sqrt(minDistSq);
116+
return minDistSq === 0 ? 0 : (inside ? 1 : -1) * Math.sqrt(minDistSq);
111117
}
112118

113119
// get polygon centroid

test/test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,38 @@ var water2 = require('./fixtures/water2.json');
88

99
test('finds pole of inaccessibility for water1 and precision 1', function (t) {
1010
var p = polylabel(water1, 1);
11-
t.same(p, [3865.85009765625, 2124.87841796875]);
11+
t.same(p, Object.assign([3865.85009765625, 2124.87841796875], {
12+
distance: 288.8493574779127
13+
}));
1214
t.end();
1315
});
1416

1517
test('finds pole of inaccessibility for water1 and precision 50', function (t) {
1618
var p = polylabel(water1, 50);
17-
t.same(p, [3854.296875, 2123.828125]);
19+
t.same(p, Object.assign([3854.296875, 2123.828125], {
20+
distance: 278.5795872381558
21+
}));
1822
t.end();
1923
});
2024

2125
test('finds pole of inaccessibility for water2 and default precision 1', function (t) {
2226
var p = polylabel(water2);
23-
t.same(p, [3263.5, 3263.5]);
27+
t.same(p, Object.assign([3263.5, 3263.5], {
28+
distance: 960.5
29+
}));
2430
t.end();
2531
});
2632

2733
test('works on degenerate polygons', function (t) {
2834
var p = polylabel([[[0, 0], [1, 0], [2, 0], [0, 0]]]);
29-
t.same(p, [0, 0]);
35+
t.same(p, Object.assign([0, 0], {
36+
distance: 0
37+
}));
3038

3139
p = polylabel([[[0, 0], [1, 0], [1, 1], [1, 0], [0, 0]]]);
32-
t.same(p, [0, 0]);
40+
t.same(p, Object.assign([0, 0], {
41+
distance: 0
42+
}));
3343

3444
t.end();
3545
});

0 commit comments

Comments
 (0)