Skip to content

Commit e7ea256

Browse files
committed
d3.geo.centroid: fix handling of mixed geometries.
1 parent 74582d8 commit e7ea256

4 files changed

Lines changed: 43 additions & 16 deletions

File tree

d3.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5597,12 +5597,20 @@
55975597
};
55985598
var d3_geo_centroidDimension, d3_geo_centroidW, d3_geo_centroidX, d3_geo_centroidY, d3_geo_centroidZ;
55995599
var d3_geo_centroid = {
5600-
sphere: d3_noop,
5600+
sphere: function() {
5601+
if (d3_geo_centroidDimension < 2) {
5602+
d3_geo_centroidDimension = 2;
5603+
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
5604+
}
5605+
},
56015606
point: d3_geo_centroidPoint,
56025607
lineStart: d3_geo_centroidLineStart,
56035608
lineEnd: d3_geo_centroidLineEnd,
56045609
polygonStart: function() {
5605-
d3_geo_centroidDimension = 2;
5610+
if (d3_geo_centroidDimension < 2) {
5611+
d3_geo_centroidDimension = 2;
5612+
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
5613+
}
56065614
d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
56075615
},
56085616
polygonEnd: function() {
@@ -5620,10 +5628,6 @@
56205628
}
56215629
function d3_geo_centroidRingStart() {
56225630
var λ00, φ00;
5623-
if (d3_geo_centroidDimension < 2) {
5624-
d3_geo_centroidDimension = 2;
5625-
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
5626-
}
56275631
d3_geo_centroidDimension = 1;
56285632
d3_geo_centroidLineStart();
56295633
d3_geo_centroidDimension = 2;

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geo/centroid.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ var d3_geo_centroidDimension,
1818
d3_geo_centroidZ;
1919

2020
var d3_geo_centroid = {
21-
sphere: d3_noop,
21+
sphere: function() {
22+
if (d3_geo_centroidDimension < 2) {
23+
d3_geo_centroidDimension = 2;
24+
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
25+
}
26+
},
2227
point: d3_geo_centroidPoint,
2328
lineStart: d3_geo_centroidLineStart,
2429
lineEnd: d3_geo_centroidLineEnd,
2530
polygonStart: function() {
26-
d3_geo_centroidDimension = 2;
31+
if (d3_geo_centroidDimension < 2) {
32+
d3_geo_centroidDimension = 2;
33+
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
34+
}
2735
d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
2836
},
2937
polygonEnd: function() {
@@ -45,10 +53,6 @@ function d3_geo_centroidPoint(λ, φ) {
4553
function d3_geo_centroidRingStart() {
4654
var λ00, φ00; // first point
4755

48-
if (d3_geo_centroidDimension < 2) {
49-
d3_geo_centroidDimension = 2;
50-
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
51-
}
5256
d3_geo_centroidDimension = 1;
5357
d3_geo_centroidLineStart();
5458
d3_geo_centroidDimension = 2;

test/geo/centroid-test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,37 @@ suite.addBatch({
6565
]}), [179.5, 0], 1e-6);
6666
},
6767
"GeometryCollection": {
68-
"Point and LineString": function(centroid) {
68+
"LineString, Point": function(centroid) {
6969
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
7070
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
7171
{type: "Point", coordinates: [0, 0]}
7272
]}), [179.5, 0], 1e-6);
7373
},
74-
"Point, LineString and Polygon": function(centroid) {
74+
"Polygon, LineString, Point": function(centroid) {
7575
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
7676
{type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]},
7777
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
7878
{type: "Point", coordinates: [0, 0]}
7979
]}), [-179.5, 0.5], 1e-6);
80+
},
81+
"Point, LineString, Polygon": function(centroid) {
82+
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
83+
{type: "Point", coordinates: [0, 0]},
84+
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
85+
{type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]}
86+
]}), [-179.5, 0.5], 1e-6);
87+
},
88+
"Sphere, Point": function(centroid) {
89+
assert.isUndefined(centroid({type: "GeometryCollection", geometries: [
90+
{type: "Sphere"},
91+
{type: "Point", coordinates: [0, 0]}
92+
]}));
93+
},
94+
"Point, Sphere": function(centroid) {
95+
assert.isUndefined(centroid({type: "GeometryCollection", geometries: [
96+
{type: "Point", coordinates: [0, 0]},
97+
{type: "Sphere"}
98+
]}));
8099
}
81100
}
82101
}

0 commit comments

Comments
 (0)