Skip to content

Commit d7c2cc7

Browse files
committed
Checkpoint new projection-path interface.
1 parent af6f24a commit d7c2cc7

4 files changed

Lines changed: 291 additions & 340 deletions

File tree

d3.v2.js

Lines changed: 90 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,18 +2016,6 @@
20162016
function d3_geo_path_circle(radius) {
20172017
return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z";
20182018
}
2019-
function d3_geo_path_projectLine(projection) {
2020-
return projection.line || function(lineString) {
2021-
return [ lineString.map(projection) ];
2022-
};
2023-
}
2024-
function d3_geo_path_projectPolygon(projection) {
2025-
return projection.polygon || function(polygon) {
2026-
return [ polygon.map(function(ring) {
2027-
return ring.map(projection);
2028-
}) ];
2029-
};
2030-
}
20312019
function d3_geo_projection(project) {
20322020
return d3_geo_projectionMutator(function() {
20332021
return project;
@@ -2050,6 +2038,19 @@
20502038
return p;
20512039
}
20522040
var project, projectRotate, k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx = x, δy = y;
2041+
p.point = function(coordinates, context) {
2042+
var point = p(coordinates);
2043+
context.point(point[0], point[1]);
2044+
};
2045+
p.line = function(coordinates, context) {
2046+
var point = p(coordinates[0]), i = 0, n = coordinates.length;
2047+
context.moveTo(point[0], point[1]);
2048+
while (++i < n) context.lineTo((point = p(coordinates[i]))[0], point[1]);
2049+
};
2050+
p.ring = function(coordinates, context) {
2051+
p.line(coordinates, context);
2052+
context.closePath();
2053+
};
20532054
p.scale = function(_) {
20542055
if (!arguments.length) return k;
20552056
k = +_;
@@ -6303,139 +6304,93 @@
63036304
return d3_geo_projection(d3_geo_orthographic);
63046305
};
63056306
d3.geo.path = function() {
6306-
function path(d, i) {
6307+
function path(object) {
63076308
if (typeof pointRadius === "function") pointCircle = d3_geo_path_circle(pointRadius.apply(this, arguments));
6308-
pathType(d);
6309-
var result = buffer.length ? buffer.join("") : null;
6310-
buffer = [];
6311-
return result;
6312-
}
6313-
function multiLineString(lineStrings) {
6314-
for (var i = 0, n = lineStrings.length; i < n; ++i) {
6315-
var lineString = lineStrings[i];
6316-
buffer.push("M");
6317-
for (var j = 0, m = lineString.length; j < m; ++j) buffer.push(lineString[j], "L");
6318-
buffer.pop();
6319-
}
6320-
}
6321-
function multiPolygon(polygons) {
6322-
for (var i = 0, n = polygons.length; i < n; ++i) {
6323-
var polygon = polygons[i];
6324-
for (var j = 0, m = polygon.length; j < m; ++j) {
6325-
var ring = polygon[j], k = -1, p;
6326-
if ((p = ring.length - 1) > 0) {
6327-
buffer.push("M");
6328-
while (++k < p) buffer.push(ring[k], "L");
6329-
buffer[buffer.length - 1] = "Z";
6330-
}
6309+
var buffer = [];
6310+
pathObject(object, {
6311+
point: function(x, y) {
6312+
buffer.push("M", x, ",", y, pointCircle);
6313+
},
6314+
moveTo: function(x, y) {
6315+
buffer.push("M", x, ",", y);
6316+
},
6317+
lineTo: function(x, y) {
6318+
buffer.push("L", x, ",", y);
6319+
},
6320+
closePath: function() {
6321+
buffer.push("Z");
63316322
}
6332-
}
6323+
});
6324+
return buffer.length ? buffer.join("") : null;
63336325
}
6334-
function polygonArea(coordinates) {
6335-
var sum = area(coordinates[0]), i = 0, n = coordinates.length;
6336-
while (++i < n) sum -= area(coordinates[i]);
6337-
return sum;
6326+
function pathObject(object, context) {
6327+
var pathType = pathObjectByType.get(object.type);
6328+
if (pathType) pathType(object, context);
63386329
}
6339-
function polygonCentroid(coordinates) {
6340-
var polygon = d3.geom.polygon(coordinates[0].map(projection)), area = polygon.area(), centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1), x = centroid[0], y = centroid[1], z = area, i = 0, n = coordinates.length;
6341-
while (++i < n) {
6342-
polygon = d3.geom.polygon(coordinates[i].map(projection));
6343-
area = polygon.area();
6344-
centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1);
6345-
x -= centroid[0];
6346-
y -= centroid[1];
6347-
z -= area;
6348-
}
6349-
return [ x, y, 6 * z ];
6330+
function pathGeometry(geometry, context) {
6331+
var pathType = pathGeometryByType.get(geometry.type);
6332+
if (pathType) pathType(geometry, context);
63506333
}
6351-
function area(coordinates) {
6352-
return Math.abs(d3.geom.polygon(coordinates.map(projection)).area());
6334+
function pathFeature(feature, context) {
6335+
pathGeometry(feature.geometry, context);
63536336
}
6354-
var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection = d3.geo.albersUsa(), projectLine = d3_geo_path_projectLine(projection), projectPolygon = d3_geo_path_projectPolygon(projection), buffer = [];
6355-
var pathType = d3_geo_type({
6356-
FeatureCollection: function(o) {
6357-
var features = o.features, i = -1, n = features.length;
6358-
while (++i < n) buffer.push(pathType(features[i].geometry));
6359-
},
6360-
Feature: function(o) {
6361-
pathType(o.geometry);
6362-
},
6363-
Point: function(o) {
6364-
buffer.push("M", projection(o.coordinates), pointCircle);
6365-
},
6366-
MultiPoint: function(o) {
6367-
var coordinates = o.coordinates, i = -1, n = coordinates.length;
6368-
while (++i < n) buffer.push("M", projection(coordinates[i]), pointCircle);
6369-
},
6370-
LineString: function(o) {
6371-
multiLineString(projectLine(o.coordinates));
6372-
},
6373-
MultiLineString: function(o) {
6374-
var lineStrings = o.coordinates;
6375-
for (var i = 0, n = lineStrings.length; i < n; ++i) {
6376-
multiLineString(projectLine(lineStrings[i]));
6377-
}
6378-
},
6379-
Polygon: function(o) {
6380-
multiPolygon(projectPolygon(o.coordinates));
6381-
},
6382-
MultiPolygon: function(o) {
6383-
var polygons = o.coordinates;
6384-
for (var i = 0, n = polygons.length; i < n; ++i) {
6385-
multiPolygon(projectPolygon(polygons[i]));
6386-
}
6387-
},
6388-
GeometryCollection: function(o) {
6389-
var geometries = o.geometries, i = -1, n = geometries.length;
6390-
while (++i < n) buffer.push(pathType(geometries[i]));
6391-
}
6337+
function pathFeatureCollection(collection, context) {
6338+
var features = collection.features, i = -1, n = features.length;
6339+
while (++i < n) pathFeature(features[i], context);
6340+
}
6341+
function pathGeometryCollection(collection, context) {
6342+
var geometries = collection.geometries, i = -1, n = geometries.length;
6343+
while (++i < n) pathGeometry(geometries[i], context);
6344+
}
6345+
function pathLineString(lineString, context) {
6346+
projection.line(lineString.coordinates, context);
6347+
}
6348+
function pathMultiLineString(multiLineString, context) {
6349+
var coordinates = multiLineString.coordinates, i = -1, n = coordinates.length;
6350+
while (++i < n) projection.line(coordinates[i], context);
6351+
}
6352+
function pathMultiPoint(multiPoint, context) {
6353+
var coordinates = multiPoint.coordinates, i = -1, n = coordinates.length;
6354+
while (++i < n) projection.point(coordinates[i], context);
6355+
}
6356+
function pathMultiPolygon(multiPolygon, context) {
6357+
var coordinates = multiPolygon.coordinates, i = -1, n = coordinates.length;
6358+
while (++i < n) {
6359+
var subcoordinates = coordinates[i], j = -1, m = subcoordinates.length;
6360+
while (++j < m) projection.ring(subcoordinates[j], context);
6361+
}
6362+
}
6363+
function pathPoint(point, context) {
6364+
projection.point(point.coordinates, context);
6365+
}
6366+
function pathPolygon(polygon, context) {
6367+
var coordinates = polygon.coordinates, i = -1, n = coordinates.length;
6368+
while (++i < n) projection.ring(coordinates[i], context);
6369+
}
6370+
var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection = d3.geo.albersUsa();
6371+
var pathObjectByType = d3.map({
6372+
Feature: pathFeature,
6373+
FeatureCollection: pathFeatureCollection,
6374+
GeometryCollection: pathGeometryCollection,
6375+
LineString: pathLineString,
6376+
MultiLineString: pathMultiLineString,
6377+
MultiPoint: pathMultiPoint,
6378+
MultiPolygon: pathMultiPolygon,
6379+
Point: pathPoint,
6380+
Polygon: pathPolygon
63926381
});
6393-
var areaType = path.area = d3_geo_type({
6394-
FeatureCollection: function(o) {
6395-
var area = 0, features = o.features, i = -1, n = features.length;
6396-
while (++i < n) area += areaType(features[i]);
6397-
return area;
6398-
},
6399-
Feature: function(o) {
6400-
return areaType(o.geometry);
6401-
},
6402-
Polygon: function(o) {
6403-
return polygonArea(o.coordinates);
6404-
},
6405-
MultiPolygon: function(o) {
6406-
var sum = 0, coordinates = o.coordinates, i = -1, n = coordinates.length;
6407-
while (++i < n) sum += polygonArea(coordinates[i]);
6408-
return sum;
6409-
},
6410-
GeometryCollection: function(o) {
6411-
var sum = 0, geometries = o.geometries, i = -1, n = geometries.length;
6412-
while (++i < n) sum += areaType(geometries[i]);
6413-
return sum;
6414-
}
6415-
}, 0);
6416-
var centroidType = path.centroid = d3_geo_type({
6417-
Feature: function(o) {
6418-
return centroidType(o.geometry);
6419-
},
6420-
Polygon: function(o) {
6421-
var centroid = polygonCentroid(o.coordinates);
6422-
return [ centroid[0] / centroid[2], centroid[1] / centroid[2] ];
6423-
},
6424-
MultiPolygon: function(o) {
6425-
var area = 0, coordinates = o.coordinates, centroid, x = 0, y = 0, z = 0, i = -1, n = coordinates.length;
6426-
while (++i < n) {
6427-
centroid = polygonCentroid(coordinates[i]);
6428-
x += centroid[0];
6429-
y += centroid[1];
6430-
z += centroid[2];
6431-
}
6432-
return [ x / z, y / z ];
6433-
}
6382+
var pathGeometryByType = d3.map({
6383+
GeometryCollection: pathGeometryCollection,
6384+
LineString: pathLineString,
6385+
MultiLineString: pathMultiLineString,
6386+
MultiPoint: pathMultiPoint,
6387+
MultiPolygon: pathMultiPolygon,
6388+
Point: pathPoint,
6389+
Polygon: pathPolygon
64346390
});
64356391
path.projection = function(_) {
64366392
if (!arguments.length) return projection;
6437-
projectLine = d3_geo_path_projectLine(projection = _);
6438-
projectPolygon = d3_geo_path_projectPolygon(projection);
6393+
projection = _;
64396394
return path;
64406395
};
64416396
path.pointRadius = function(x) {

d3.v2.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)