d3/test/geo/area-test.js

173 строки
7.1 KiB
JavaScript
Исходник Обычный вид История

2012-12-08 18:48:16 +04:00
var vows = require("vows"),
2013-03-14 10:21:23 +04:00
load = require("../load"),
assert = require("../env-assert");
2012-12-08 18:48:16 +04:00
var suite = vows.describe("d3.geo.area");
var π = Math.PI;
suite.addBatch({
"area": {
2013-03-14 10:21:23 +04:00
topic: load("geo/area", "geo/circle", "geo/graticule", "arrays/range"),
"Point": function(d3) {
assert.equal(d3.geo.area({type: "Point", coordinates: [0, 0]}), 0);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"MultiPoint": function(d3) {
assert.equal(d3.geo.area({type: "MultiPoint", coordinates: [[0, 1], [2, 3]]}), 0);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"LineString": function(d3) {
assert.equal(d3.geo.area({type: "LineString", coordinates: [[0, 1], [2, 3]]}), 0);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"MultiLineString": function(d3) {
assert.equal(d3.geo.area({type: "MultiLineString", coordinates: [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]}), 0);
2012-12-08 18:48:16 +04:00
},
"Polygon": {
2013-03-14 10:21:23 +04:00
"tiny": function(d3) {
assert.inDelta(d3.geo.area({type: "Polygon", coordinates: [[
2013-02-01 17:20:05 +04:00
[-64.66070178517852, 18.33986913231323],
[-64.66079715091509, 18.33994007490749],
[-64.66074946804680, 18.33994007490749],
[-64.66070178517852, 18.33986913231323]
]]}), 4.890516e-13, 1e-13);
},
2013-03-14 10:21:23 +04:00
"semilune": function(d3) {
assert.inDelta(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [0, 90], [90, 0], [0, 0]]]}), π / 2, 1e-6);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"lune": function(d3) {
assert.equal(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [0, 90], [90, 0], [0, -90], [0, 0]]]}), π);
2012-12-08 18:48:16 +04:00
},
"hemispheres": {
2013-03-14 10:21:23 +04:00
"North": function(d3) {
assert.inDelta(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [-90, 0], [180, 0], [90, 0], [0, 0]]]}), 2 * π, 1e-6);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"South": function(d3) {
assert.inDelta(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [90, 0], [180, 0], [-90, 0], [0, 0]]]}), 2 * π, 1e-6);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"East": function(d3) {
assert.equal(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [0, 90], [180, 0], [0, -90], [0, 0]]]}), 2 * π);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"West": function(d3) {
assert.equal(d3.geo.area({type: "Polygon", coordinates: [[[0, 0], [0, -90], [180, 0], [0, 90], [0, 0]]]}), 2 * π);
2012-12-08 18:48:16 +04:00
}
},
"graticule outline": {
2013-03-14 10:21:23 +04:00
"sphere": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.graticule().extent([[-180, -90], [180, 90]]).outline()), 4 * π, 1e-5);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"hemisphere": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.graticule().extent([[-180, 0], [180, 90]]).outline()), 2 * π, 1e-5);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"semilune": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.graticule().extent([[0, 0], [90, 90]]).outline()), π / 2, 1e-5);
2012-12-08 18:48:16 +04:00
}
2012-12-10 00:32:01 +04:00
},
"circles": {
2013-03-14 10:21:23 +04:00
"hemisphere": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(90)()), 2 * π, 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"60°": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(60).precision(.1)()), π, 1e-5);
2012-12-10 15:26:37 +04:00
},
2013-03-14 10:21:23 +04:00
"60° North": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(60).precision(.1).origin([0, 90])()), π, 1e-5);
2012-12-10 15:26:37 +04:00
},
2013-03-14 10:21:23 +04:00
"45°": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(45).precision(.1)()), π * (2 - Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"45° North": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(45).precision(.1).origin([0, 90])()), π * (2 - Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"45° South": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(45).precision(.1).origin([0, -90])()), π * (2 - Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"135°": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(135).precision(.1)()), π * (2 + Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"135° North": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(135).precision(.1).origin([0, 90])()), π * (2 + Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"135° South": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(135).precision(.1).origin([0, -90])()), π * (2 + Math.SQRT2), 1e-5);
2012-12-10 00:32:01 +04:00
},
2013-03-14 10:21:23 +04:00
"tiny": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(1e-6).precision(.1)()), 0, 1e-6);
},
2013-03-14 10:21:23 +04:00
"huge": function(d3) {
assert.inDelta(d3.geo.area(d3.geo.circle().angle(180 - 1e-6).precision(.1)()), 4 * π, 1e-6);
2012-12-10 15:26:37 +04:00
},
2013-03-14 10:21:23 +04:00
"60° with 45° hole": function(d3) {
2012-12-10 15:26:37 +04:00
var circle = d3.geo.circle().precision(.1);
2013-03-14 10:21:23 +04:00
assert.inDelta(d3.geo.area({
2012-12-10 15:26:37 +04:00
type: "Polygon",
coordinates: [
circle.angle(60)().coordinates[0],
circle.angle(45)().coordinates[0].reverse()
]
}), π * (Math.SQRT2 - 1), 1e-5);
},
2013-03-14 10:21:23 +04:00
"45° holes at [0°, 0°] and [0°, 90°]": function(d3) {
2012-12-10 15:26:37 +04:00
var circle = d3.geo.circle().precision(.1).angle(45);
2013-03-14 10:21:23 +04:00
assert.inDelta(d3.geo.area({
2012-12-10 15:26:37 +04:00
type: "Polygon",
coordinates: [
circle.origin([0, 0])().coordinates[0].reverse(),
circle.origin([0, 90])().coordinates[0].reverse()
]
}), π * 2 * Math.SQRT2, 1e-5);
},
2013-03-14 10:21:23 +04:00
"45° holes at [0°, 90°] and [0°, 0°]": function(d3) {
2012-12-10 15:26:37 +04:00
var circle = d3.geo.circle().precision(.1).angle(45);
2013-03-14 10:21:23 +04:00
assert.inDelta(d3.geo.area({
2012-12-10 15:26:37 +04:00
type: "Polygon",
coordinates: [
circle.origin([0, 90])().coordinates[0].reverse(),
circle.origin([0, 0])().coordinates[0].reverse()
]
}), π * 2 * Math.SQRT2, 1e-5);
}
},
"stripes": {
2013-03-14 10:21:23 +04:00
"45°, -45°": function(d3) {
assert.inDelta(d3.geo.area(stripes(d3, 45, -45)), π * 2 * Math.SQRT2, 1e-5);
2012-12-10 15:26:37 +04:00
},
2013-03-14 10:21:23 +04:00
"-45°, 45°": function(d3) {
assert.inDelta(d3.geo.area(stripes(d3, -45, 45)), π * 2 * (2 - Math.SQRT2), 1e-5);
2012-12-10 15:26:37 +04:00
},
2013-03-14 10:21:23 +04:00
"45°, 30°": function(d3) {
assert.inDelta(d3.geo.area(stripes(d3, 45, 30)), π * (Math.SQRT2 - 1), 1e-5);
2012-12-10 00:32:01 +04:00
}
2012-12-08 18:48:16 +04:00
}
},
"MultiPolygon": {
2013-03-14 10:21:23 +04:00
"two hemispheres": function(d3) {
assert.equal(d3.geo.area({type: "MultiPolygon", coordinates: [
2012-12-08 18:48:16 +04:00
[[[0, 0], [-90, 0], [180, 0], [90, 0], [0, 0]]],
[[[0, 0], [90, 0], [180, 0], [-90, 0], [0, 0]]]
]}), 4 * π);
}
},
2013-03-14 10:21:23 +04:00
"Sphere": function(d3) {
assert.equal(d3.geo.area({type: "Sphere"}), 4 * π);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"GeometryCollection": function(d3) {
assert.equal(d3.geo.area({type: "GeometryCollection", geometries: [{type: "Sphere"}]}), 4 * π);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"FeatureCollection": function(d3) {
assert.equal(d3.geo.area({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Sphere"}}]}), 4 * π);
2012-12-08 18:48:16 +04:00
},
2013-03-14 10:21:23 +04:00
"Feature": function(d3) {
assert.equal(d3.geo.area({type: "Feature", geometry: {type: "Sphere"}}), 4 * π);
2012-12-08 18:48:16 +04:00
}
}
});
suite.export(module);
2012-12-10 15:26:37 +04:00
2013-03-14 10:21:23 +04:00
function stripes(d3, a, b) {
2012-12-10 15:26:37 +04:00
return {type: "Polygon", coordinates: [a, b].map(function(d, i) {
var stripe = d3.range(-180, 180, .1).map(function(x) { return [x, d]; });
stripe.push(stripe[0]);
return i ? stripe.reverse() : stripe;
})};
}