This commit is contained in:
Mike Bostock 2012-12-12 13:56:41 -08:00
Родитель 8f5fe826c7
Коммит 2e88ce7f03
3 изменённых файлов: 328 добавлений и 72 удалений

129
d3.js поставляемый
Просмотреть файл

@ -5397,76 +5397,71 @@
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
listener.polygonEnd();
}
function d3_geo_streamBuffer() {
this._buffer = [];
this._point = d3_geo_pathCircle(4.5);
}
d3_geo_streamBuffer.prototype = {
point: d3_geo_streamBufferPoint,
lineStart: function() {
this.point = d3_geo_streamBufferPointLineStart;
},
lineEnd: d3_geo_streamBufferLineEnd,
polygonStart: function() {
this.lineEnd = d3_geo_streamBufferLineEndPolygon;
},
polygonEnd: function() {
this.lineEnd = d3_geo_streamBufferLineEnd;
},
toString: function() {
var s = this._buffer.join("");
this._buffer = [];
return s;
function d3_geo_streamBuffer(buffer, pointRadius) {
var stream = {
point: point,
lineStart: function() {
stream.point = pointLineStart;
},
lineEnd: lineEnd,
polygonStart: function() {
stream.lineEnd = lineEndPolygon;
},
polygonEnd: function() {
stream.lineEnd = lineEnd;
}
};
function point(x, y) {
buffer.push("M", x, ",", y, pointRadius);
}
};
function d3_geo_streamBufferPoint(x, y) {
this._buffer.push("M", x, ",", y, this._point);
}
function d3_geo_streamBufferPointLineStart(x, y) {
this._buffer.push("M", x, ",", y);
this.point = d3_geo_streamBufferPointLine;
}
function d3_geo_streamBufferPointLine(x, y) {
this._buffer.push("L", x, ",", y);
}
function d3_geo_streamBufferLineEnd() {
this.point = d3_geo_streamBufferPoint;
}
function d3_geo_streamBufferLineEndPolygon() {
this._buffer.push("Z");
}
function d3_geo_streamContext() {
this._pointRadius = 4.5;
}
d3_geo_streamContext.prototype = {
point: d3_geo_streamContextPoint,
lineStart: function() {
this.point = d3_geo_streamContextPointLineStart;
},
lineEnd: d3_geo_streamContextLineEnd,
polygonStart: function() {
this.lineEnd = d3_geo_streamContextLineEndPolygon;
},
polygonEnd: function() {
this.lineEnd = d3_geo_streamContextLineEnd;
function pointLineStart(x, y) {
buffer.push("M", x, ",", y);
stream.point = pointLine;
}
};
function d3_geo_streamContextPoint(x, y) {
this._context.moveTo(x, y);
this._context.arc(x, y, this._pointRadius, 0, 2 * π);
function pointLine(x, y) {
buffer.push("L", x, ",", y);
}
function lineEnd() {
stream.point = point;
}
function lineEndPolygon() {
buffer.push("Z");
}
pointRadius = d3_geo_pathCircle(4.5);
return stream;
}
function d3_geo_streamContextPointLineStart(x, y) {
this._context.moveTo(x, y);
this.point = d3_geo_streamContextPointLine;
}
function d3_geo_streamContextPointLine(x, y) {
this._context.lineTo(x, y);
}
function d3_geo_streamContextLineEnd() {
this.point = d3_geo_streamContextPoint;
}
function d3_geo_streamContextLineEndPolygon() {
this._context.closePath();
function d3_geo_streamContext(context, pointRadius) {
var stream = {
point: point,
lineStart: function() {
stream.point = pointLineStart;
},
lineEnd: lineEnd,
polygonStart: function() {
stream.lineEnd = lineEndPolygon;
},
polygonEnd: function() {
stream.lineEnd = lineEnd;
}
};
function point(x, y) {
context.moveTo(x, y);
context.arc(x, y, pointRadius, 0, 2 * π);
}
function pointLineStart(x, y) {
context.moveTo(x, y);
stream.point = pointLine;
}
function pointLine(x, y) {
context.lineTo(x, y);
}
function lineEnd() {
stream.point = point;
}
function lineEndPolygon() {
context.closePath();
}
return stream;
}
function d3_geo_spherical(cartesian) {
return [ Math.atan2(cartesian[1], cartesian[0]), Math.asin(Math.max(-1, Math.min(1, cartesian[2]))) ];

10
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

261
test/geo/stream-test.js Normal file
Просмотреть файл

@ -0,0 +1,261 @@
require("../env");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("d3.geo.stream");
suite.addBatch({
"stream": {
topic: function() {
return d3.geo.stream;
},
"does not allow null input": function(stream) {
try {
stream(null);
assert.fail("expected error");
} catch (expected) {}
},
"ignores unknown types": function(stream) {
stream({type: "Unknown"}, {});
stream({type: "Feature", geometry: {type: "Unknown"}}, {});
stream({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Unknown"}}]}, {});
stream({type: "GeometryCollection", geometries: [{type: "Unknown"}]}, {});
},
"returns void": function(stream) {
assert.isUndefined(stream({type: "Point", coordinates: [1, 2]}, {point: function() { return true; }}));
},
"allows empty multi-geometries": function(stream) {
stream({type: "MultiPoint", coordinates: []}, {});
stream({type: "MultiLineString", coordinates: []}, {});
stream({type: "MultiPolygon", coordinates: []}, {});
},
"Sphere ↦ sphere": function(stream) {
var calls = 0;
stream({type: "Sphere"}, {
sphere: function() {
++calls;
assert.equal(arguments.length, 0);
}
});
assert.equal(calls, 1);
},
"Point ↦ point": function(stream) {
var calls = 0;
stream({type: "Point", coordinates: [1, 2]}, {
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
assert.equal(x, 1);
assert.equal(y, 2);
}
});
assert.equal(calls, 1);
},
"MultiPoint ↦ point*": function(stream) {
var calls = 0;
stream({type: "MultiPoint", coordinates: [[1, 2], [3, 4]]}, {
point: function(x, y) {
assert.equal(arguments.length, 2);
if (++calls === 1) {
assert.equal(x, 1);
assert.equal(y, 2);
} else {
assert.equal(x, 3);
assert.equal(y, 4);
}
}
});
assert.equal(calls, 2);
},
"LineString ↦ lineStart, point{2,}, lineEnd": function(stream) {
var calls = 0;
stream({type: "LineString", coordinates: [[1, 2], [3, 4]]}, {
lineStart: function() {
assert.equal(++calls, 1);
assert.equal(arguments.length, 0);
},
point: function(x, y) {
assert.equal(arguments.length, 2);
if (++calls === 2) {
assert.equal(x, 1);
assert.equal(y, 2);
} else if (calls === 3) {
assert.equal(x, 3);
assert.equal(y, 4);
} else {
assert.fail("too many points");
}
},
lineEnd: function() {
assert.equal(++calls, 4);
assert.equal(arguments.length, 0);
}
});
assert.equal(calls, 4);
},
"MultiLineString ↦ (lineStart, point{2,}, lineEnd)*": function(stream) {
var calls = 0;
stream({type: "MultiLineString", coordinates: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]}, {
lineStart: function() {
++calls;
assert.isTrue(calls === 1 || calls === 5);
assert.equal(arguments.length, 0);
},
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
if (calls === 2) {
assert.equal(x, 1);
assert.equal(y, 2);
} else if (calls === 3) {
assert.equal(x, 3);
assert.equal(y, 4);
} else if (calls === 6) {
assert.equal(x, 5);
assert.equal(y, 6);
} else if (calls === 7) {
assert.equal(x, 7);
assert.equal(y, 8);
} else {
assert.fail("too many points");
}
},
lineEnd: function() {
++calls;
assert.isTrue(calls === 4 || calls === 8);
assert.equal(arguments.length, 0);
}
});
assert.equal(calls, 8);
},
"Polygon ↦ polygonStart, lineStart, point{3,}, lineEnd, polygonEnd": function(stream) {
var calls = 0;
stream({type: "Polygon", coordinates: [[[1, 2], [3, 4], [1, 2]], [[5, 6], [7, 8], [5, 6]]]}, {
polygonStart: function() {
++calls;
assert.isTrue(calls === 1);
assert.equal(arguments.length, 0);
},
lineStart: function() {
++calls;
assert.isTrue(calls === 2 || calls === 7);
assert.equal(arguments.length, 0);
},
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
if (calls === 3 || calls === 5) {
assert.equal(x, 1);
assert.equal(y, 2);
} else if (calls === 4) {
assert.equal(x, 3);
assert.equal(y, 4);
} else if (calls === 8 || calls === 10) {
assert.equal(x, 5);
assert.equal(y, 6);
} else if (calls === 9) {
assert.equal(x, 7);
assert.equal(y, 8);
} else {
assert.fail("too many points");
}
},
lineEnd: function() {
++calls;
assert.isTrue(calls === 6 || calls === 11);
assert.equal(arguments.length, 0);
},
polygonEnd: function() {
++calls;
assert.isTrue(calls === 12);
assert.equal(arguments.length, 0);
}
});
assert.equal(calls, 12);
},
"MultiPolygon ↦ (polygonStart, lineStart, point{3,}, lineEnd, polygonEnd)*": function(stream) {
var calls = 0;
stream({type: "MultiPolygon", coordinates: [[[[1, 2], [3, 4], [1, 2]]], [[[5, 6], [7, 8], [5, 6]]]]}, {
polygonStart: function() {
++calls;
assert.isTrue(calls === 1 || calls === 8);
assert.equal(arguments.length, 0);
},
lineStart: function() {
++calls;
assert.isTrue(calls === 2 || calls === 9);
assert.equal(arguments.length, 0);
},
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
if (calls === 3 || calls === 5) {
assert.equal(x, 1);
assert.equal(y, 2);
} else if (calls === 4) {
assert.equal(x, 3);
assert.equal(y, 4);
} else if (calls === 10 || calls === 12) {
assert.equal(x, 5);
assert.equal(y, 6);
} else if (calls === 11) {
assert.equal(x, 7);
assert.equal(y, 8);
} else {
assert.fail("too many points");
}
},
lineEnd: function() {
++calls;
assert.isTrue(calls === 6 || calls === 13);
assert.equal(arguments.length, 0);
},
polygonEnd: function() {
++calls;
assert.isTrue(calls === 7 || calls === 14);
assert.equal(arguments.length, 0);
}
});
assert.equal(calls, 14);
},
"Feature ↦ .*": function(stream) {
var calls = 0;
stream({type: "Feature", geometry: {type: "Point", coordinates: [1, 2]}}, {
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
assert.equal(x, 1);
assert.equal(y, 2);
}
});
assert.equal(calls, 1);
},
"FeatureCollection ↦ .*": function(stream) {
var calls = 0;
stream({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Point", coordinates: [1, 2]}}]}, {
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
assert.equal(x, 1);
assert.equal(y, 2);
}
});
assert.equal(calls, 1);
},
"GeometryCollection ↦ .*": function(stream) {
var calls = 0;
stream({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [1, 2]}]}, {
point: function(x, y) {
++calls;
assert.equal(arguments.length, 2);
assert.equal(x, 1);
assert.equal(y, 2);
}
});
assert.equal(calls, 1);
}
}
});
suite.export(module);