Permalink
Browse files

d3.geo.centroid: fix handling of mixed geometries.

  • Loading branch information...
1 parent 74582d8 commit e7ea256dc15b5488c5503d28fcdff0d880e0fbf1 @jasondavies jasondavies committed Dec 27, 2012
Showing with 43 additions and 16 deletions.
  1. +10 −6 d3.js
  2. +2 −2 d3.min.js
  3. +10 −6 src/geo/centroid.js
  4. +21 −2 test/geo/centroid-test.js
View
16 d3.js
@@ -5597,12 +5597,20 @@
};
var d3_geo_centroidDimension, d3_geo_centroidW, d3_geo_centroidX, d3_geo_centroidY, d3_geo_centroidZ;
var d3_geo_centroid = {
- sphere: d3_noop,
+ sphere: function() {
+ if (d3_geo_centroidDimension < 2) {
+ d3_geo_centroidDimension = 2;
+ d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
+ }
+ },
point: d3_geo_centroidPoint,
lineStart: d3_geo_centroidLineStart,
lineEnd: d3_geo_centroidLineEnd,
polygonStart: function() {
- d3_geo_centroidDimension = 2;
+ if (d3_geo_centroidDimension < 2) {
+ d3_geo_centroidDimension = 2;
+ d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
+ }
d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
},
polygonEnd: function() {
@@ -5620,10 +5628,6 @@
}
function d3_geo_centroidRingStart() {
var λ00, φ00;
- if (d3_geo_centroidDimension < 2) {
- d3_geo_centroidDimension = 2;
- d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
- }
d3_geo_centroidDimension = 1;
d3_geo_centroidLineStart();
d3_geo_centroidDimension = 2;
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -18,12 +18,20 @@ var d3_geo_centroidDimension,
d3_geo_centroidZ;
var d3_geo_centroid = {
- sphere: d3_noop,
+ sphere: function() {
+ if (d3_geo_centroidDimension < 2) {
+ d3_geo_centroidDimension = 2;
+ d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
+ }
+ },
point: d3_geo_centroidPoint,
lineStart: d3_geo_centroidLineStart,
lineEnd: d3_geo_centroidLineEnd,
polygonStart: function() {
- d3_geo_centroidDimension = 2;
+ if (d3_geo_centroidDimension < 2) {
+ d3_geo_centroidDimension = 2;
+ d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
+ }
d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
},
polygonEnd: function() {
@@ -45,10 +53,6 @@ function d3_geo_centroidPoint(λ, φ) {
function d3_geo_centroidRingStart() {
var λ00, φ00; // first point
- if (d3_geo_centroidDimension < 2) {
- d3_geo_centroidDimension = 2;
- d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
- }
d3_geo_centroidDimension = 1;
d3_geo_centroidLineStart();
d3_geo_centroidDimension = 2;
View
@@ -65,18 +65,37 @@ suite.addBatch({
]}), [179.5, 0], 1e-6);
},
"GeometryCollection": {
- "Point and LineString": function(centroid) {
+ "LineString, Point": function(centroid) {
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
{type: "Point", coordinates: [0, 0]}
]}), [179.5, 0], 1e-6);
},
- "Point, LineString and Polygon": function(centroid) {
+ "Polygon, LineString, Point": function(centroid) {
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
{type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]},
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
{type: "Point", coordinates: [0, 0]}
]}), [-179.5, 0.5], 1e-6);
+ },
+ "Point, LineString, Polygon": function(centroid) {
+ assert.inDelta(centroid({type: "GeometryCollection", geometries: [
+ {type: "Point", coordinates: [0, 0]},
+ {type: "LineString", coordinates: [[179, 0], [180, 0]]},
+ {type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]}
+ ]}), [-179.5, 0.5], 1e-6);
+ },
+ "Sphere, Point": function(centroid) {
+ assert.isUndefined(centroid({type: "GeometryCollection", geometries: [
+ {type: "Sphere"},
+ {type: "Point", coordinates: [0, 0]}
+ ]}));
+ },
+ "Point, Sphere": function(centroid) {
+ assert.isUndefined(centroid({type: "GeometryCollection", geometries: [
+ {type: "Point", coordinates: [0, 0]},
+ {type: "Sphere"}
+ ]}));
}
}
}

0 comments on commit e7ea256

Please sign in to comment.