Skip to content

Commit b87bcbc

Browse files
committed
Implement d3.geo.centroid via d3.geo.stream.
1 parent 7697f20 commit b87bcbc

6 files changed

Lines changed: 134 additions & 388 deletions

File tree

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ d3.geo.js: \
183183
src/geo/stream.js \
184184
src/geo/spherical.js \
185185
src/geo/cartesian.js \
186-
src/geo/type.js \
187186
src/geo/clip.js \
188187
src/geo/cut.js \
189188
src/geo/resample.js \

d3.js

Lines changed: 58 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -5427,84 +5427,6 @@
54275427
d[1] /= l;
54285428
d[2] /= l;
54295429
}
5430-
function d3_geo_type(types) {
5431-
for (var t in d3_geo_typeDefaults) if (!(t in types)) types[t] = d3_geo_typeDefaults[t];
5432-
return function(o) {
5433-
return o && (d3_geo_typeObjects.hasOwnProperty(o.type) ? types[o.type](o) : types.geometry(o));
5434-
};
5435-
}
5436-
var d3_geo_typeDefaults = {
5437-
Feature: function(o) {
5438-
var g = this.geometry(o.geometry);
5439-
return g && (o = Object.create(o), o.geometry = g, o);
5440-
},
5441-
FeatureCollection: function(o) {
5442-
var a, f, features = o.features, i = -1, n = features.length;
5443-
while (++i < n) if (f = this.Feature(features[i])) a ? a.push(f) : a = [ f ];
5444-
return a && (o = Object.create(o), o.features = a, o);
5445-
},
5446-
GeometryCollection: function(o) {
5447-
var a, g, geometries = o.geometries, i = -1, n = geometries.length;
5448-
while (++i < n) if (g = this.geometry(geometries[i])) a ? a.push(g) : a = [ g ];
5449-
return a && (o = Object.create(o), o.geometries = a, o);
5450-
},
5451-
LineString: function(o) {
5452-
var c = this.line(o.coordinates);
5453-
return c && (o = Object.create(o), o.coordinates = c, o);
5454-
},
5455-
MultiLineString: function(o) {
5456-
var a, c, coordinates = o.coordinates, i = -1, n = coordinates.length;
5457-
while (++i < n) if (c = this.line(coordinates[i])) a ? a.push(c) : a = [ c ];
5458-
return a && (o = Object.create(o), o.coordinates = a, o);
5459-
},
5460-
MultiPoint: function(o) {
5461-
var a, c, coordinates = o.coordinates, i = -1, n = coordinates.length;
5462-
while (++i < n) if (c = this.point(coordinates[i])) a ? a.push(c) : a = [ c ];
5463-
return a && (o = Object.create(o), o.coordinates = a, o);
5464-
},
5465-
MultiPolygon: function(o) {
5466-
var a, c, coordinates = o.coordinates, i = -1, n = coordinates.length;
5467-
while (++i < n) if (c = this.polygon(coordinates[i])) a ? a.push(c) : a = [ c ];
5468-
return a && (o = Object.create(o), o.coordinates = a, o);
5469-
},
5470-
Point: function(o) {
5471-
var c = this.point(o.coordinates);
5472-
return c && (o = Object.create(o), o.coordinates = c, o);
5473-
},
5474-
Polygon: function(o) {
5475-
var c = this.polygon(o.coordinates);
5476-
return c && (o = Object.create(o), o.coordinates = c, o);
5477-
},
5478-
Sphere: d3_noop,
5479-
geometry: function(o) {
5480-
return d3_geo_typeGeometries.hasOwnProperty(o.type) ? this[o.type](o) : null;
5481-
},
5482-
point: d3_noop,
5483-
line: function(coordinates) {
5484-
var a, c, i = -1, n = coordinates.length;
5485-
while (++i < n) if (c = this.point(coordinates[i])) a ? a.push(c) : a = [ c ];
5486-
return a;
5487-
},
5488-
polygon: function(coordinates) {
5489-
var a, c, i = -1, n = coordinates.length;
5490-
while (++i < n) if (c = this.line(coordinates[i])) a ? a.push(c) : a = [ c ];
5491-
return a;
5492-
}
5493-
};
5494-
var d3_geo_typeGeometries = {
5495-
LineString: 1,
5496-
MultiLineString: 1,
5497-
MultiPoint: 1,
5498-
MultiPolygon: 1,
5499-
Point: 1,
5500-
Polygon: 1,
5501-
Sphere: 1
5502-
};
5503-
var d3_geo_typeObjects = {
5504-
Feature: 1,
5505-
FeatureCollection: 1,
5506-
GeometryCollection: 1
5507-
};
55085430
function d3_geo_clip(pointVisible, clipLine, interpolate) {
55095431
return function(listener) {
55105432
var line = clipLine(listener), polygon = [], ring;
@@ -5966,79 +5888,66 @@
59665888
return [ [ x0, y0 ], [ x1, y1 ] ];
59675889
};
59685890
}
5969-
var d3_geo_centroidX, d3_geo_centroidY, d3_geo_centroidZ;
5970-
d3.geo.centroid = d3_geo_type({
5971-
FeatureCollection: d3_noop,
5972-
GeometryCollection: d3_noop,
5973-
Feature: function(feature) {
5974-
return this.geometry(feature.geometry);
5975-
},
5976-
Point: function(point) {
5977-
return point.coordinates;
5978-
},
5979-
MultiPoint: function(multiPoint) {
5980-
var coordinates = multiPoint.coordinates, n = coordinates.length, i = 0, point, λ, φ, cosφ, x = 0, y = 0, z = 0;
5981-
while (i < n) {
5982-
point = coordinates[i++];
5983-
λ = point[0] * d3_radians;
5984-
cosφ = Math.cos(φ = point[1] * d3_radians);
5985-
x += (cosφ * Math.cos(λ) - x) / i;
5986-
y += (cosφ * Math.sin(λ) - y) / i;
5987-
z += (Math.sin(φ) - z) / i;
5988-
}
5989-
return n ? [ Math.atan2(y, x) * d3_degrees, Math.asin(Math.max(-1, Math.min(1, z))) * d3_degrees ] : null;
5990-
},
5991-
LineString: function(line) {
5992-
var centroid = d3_geo_centroidLine(line.coordinates), cx, cy, cz;
5993-
return centroid && centroid[3] ? [ Math.atan2(cy = centroid[1], cx = centroid[0]) * d3_degrees, Math.asin(Math.max(-1, Math.min(1, (cz = centroid[2]) / Math.sqrt(cx * cx + cy * cy + cz * cz)))) * d3_degrees ] : null;
5994-
},
5995-
MultiLineString: function(multiLine) {
5996-
var coordinates = multiLine.coordinates, n = coordinates.length, i = -1, cx = 0, cy = 0, cz = 0, weight = 0;
5997-
while (++i < n) {
5998-
centroid = d3_geo_centroidLine(coordinates[i]);
5999-
if (!centroid) continue;
6000-
cx += centroid[0];
6001-
cy += centroid[1];
6002-
cz += centroid[2];
6003-
weight += centroid[3];
6004-
}
6005-
return weight ? [ Math.atan2(cy, cx) * d3_degrees, Math.asin(Math.max(-1, Math.min(1, cz / Math.sqrt(cx * cx + cy * cy + cz * cz)))) * d3_degrees ] : null;
6006-
},
6007-
Polygon: function(polygon) {
6008-
return this.MultiLineString(polygon);
5891+
d3.geo.centroid = function(object) {
5892+
d3_geo_centroidW = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
5893+
d3.geo.stream(object, d3_geo_centroid);
5894+
return d3_geo_centroidW ? [ Math.atan2(d3_geo_centroidY, d3_geo_centroidX) * d3_degrees, Math.asin(Math.max(-1, Math.min(1, d3_geo_centroidZ / Math.sqrt(d3_geo_centroidX * d3_geo_centroidX + d3_geo_centroidY * d3_geo_centroidY + d3_geo_centroidZ * d3_geo_centroidZ)))) * d3_degrees ] : null;
5895+
};
5896+
var d3_geo_centroidW, d3_geo_centroidX, d3_geo_centroidY, d3_geo_centroidZ;
5897+
var d3_geo_centroid = {
5898+
sphere: d3_noop,
5899+
point: d3_geo_centroidPoint,
5900+
lineStart: d3_geo_centroidLineStart,
5901+
lineEnd: d3_geo_centroidLineEnd,
5902+
polygonStart: function() {
5903+
d3_geo_centroid.lineStart = d3_geo_centroidRingStart;
60095904
},
6010-
MultiPolygon: function(multiPolygon) {
6011-
var coordinates = multiPolygon.coordinates, n = coordinates.length, i = -1, cx = 0, cy = 0, cz = 0, weight = 0, polygon;
6012-
while (++i < n) {
6013-
polygon = coordinates[i];
6014-
for (var j = 0, m = polygon.length; j < m; ++j) {
6015-
centroid = d3_geo_centroidLine(polygon[j]);
6016-
if (!centroid) continue;
6017-
cx += centroid[0];
6018-
cy += centroid[1];
6019-
cz += centroid[2];
6020-
weight += centroid[3];
6021-
}
6022-
}
6023-
return weight ? [ Math.atan2(cy, cx) * d3_degrees, Math.asin(Math.max(-1, Math.min(1, cz / Math.sqrt(cx * cx + cy * cy + cz * cz)))) * d3_degrees ] : null;
5905+
polygonEnd: function() {
5906+
d3_geo_centroid.lineStart = d3_geo_centroidLineStart;
5907+
}
5908+
};
5909+
function d3_geo_centroidPoint(λ, φ) {
5910+
++d3_geo_centroidW;
5911+
λ *= d3_radians;
5912+
var cosφ = Math.cos(φ *= d3_radians);
5913+
d3_geo_centroidX += (cosφ * Math.cos(λ) - d3_geo_centroidX) / d3_geo_centroidW;
5914+
d3_geo_centroidY += (cosφ * Math.sin(λ) - d3_geo_centroidY) / d3_geo_centroidW;
5915+
d3_geo_centroidZ += (Math.sin(φ) - d3_geo_centroidZ) / d3_geo_centroidW;
5916+
}
5917+
function d3_geo_centroidRingStart() {
5918+
var λ00, φ00;
5919+
d3_geo_centroidLineStart();
5920+
var linePoint = d3_geo_centroid.point;
5921+
d3_geo_centroid.point = function(λ, φ) {
5922+
linePoint(λ00 = λ, φ00 = φ);
5923+
};
5924+
d3_geo_centroid.lineEnd = function() {
5925+
d3_geo_centroid.point(λ00, φ00);
5926+
d3_geo_centroidLineEnd();
5927+
};
5928+
}
5929+
function d3_geo_centroidLineStart() {
5930+
var x0, y0, z0;
5931+
d3_geo_centroid.point = function(λ, φ) {
5932+
λ *= d3_radians;
5933+
var cosφ = Math.cos(φ *= d3_radians);
5934+
x0 = cosφ * Math.cos(λ);
5935+
y0 = cosφ * Math.sin(λ);
5936+
z0 = Math.sin(φ);
5937+
d3_geo_centroid.point = nextPoint;
5938+
};
5939+
d3_geo_centroid.lineEnd = d3_geo_centroidLineEnd;
5940+
function nextPoint(λ, φ) {
5941+
λ *= d3_radians;
5942+
var cosφ = Math.cos(φ *= d3_radians), x = cosφ * Math.cos(λ), y = cosφ * Math.sin(λ), z = Math.sin(φ), w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
5943+
d3_geo_centroidW += w;
5944+
d3_geo_centroidX += w * (x0 + (x0 = x));
5945+
d3_geo_centroidY += w * (y0 + (y0 = y));
5946+
d3_geo_centroidZ += w * (z0 + (z0 = z));
60245947
}
6025-
});
6026-
function d3_geo_centroidLine(coordinates) {
6027-
if (!(n = coordinates.length)) return null;
6028-
var point = coordinates[0], i = 0, n, λ = point[0] * d3_radians, φ, cosφ = Math.cos(φ = point[1] * d3_radians), x0 = cosφ * Math.cos(λ), y0 = cosφ * Math.sin(λ), z0 = Math.sin(φ), cx = 0, cy = 0, cz = 0, x, y, z, w, weight = 0;
6029-
while (++i < n) {
6030-
point = coordinates[i];
6031-
λ = point[0] * d3_radians;
6032-
cosφ = Math.cos(φ = point[1] * d3_radians);
6033-
x = cosφ * Math.cos(λ);
6034-
y = cosφ * Math.sin(λ);
6035-
z = Math.sin(φ);
6036-
weight += w = Math.atan2(Math.sqrt((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
6037-
cx += w * (x0 + (x0 = x));
6038-
cy += w * (y0 + (y0 = y));
6039-
cz += w * (z0 + (z0 = z));
6040-
}
6041-
return [ cx, cy, cz, weight ];
5948+
}
5949+
function d3_geo_centroidLineEnd() {
5950+
d3_geo_centroid.point = d3_geo_centroidPoint;
60425951
}
60435952
d3.geo.circle = function() {
60445953
var origin = [ 0, 0 ], angle, precision = 6, rotate, interpolate;

d3.min.js

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

0 commit comments

Comments
 (0)