|
| 1 | +require("../env"); |
| 2 | + |
| 3 | +var vows = require("vows"), |
| 4 | + assert = require("assert"); |
| 5 | + |
| 6 | +var suite = vows.describe("d3.geo.centroid"); |
| 7 | + |
| 8 | +suite.addBatch({ |
| 9 | + "centroid": { |
| 10 | + topic: function() { |
| 11 | + return d3.geo.centroid; |
| 12 | + }, |
| 13 | + "Point": function(centroid) { |
| 14 | + assert.deepEqual(centroid({type: "Point", coordinates: [0, 0]}), [0, 0]); |
| 15 | + }, |
| 16 | + "MultiPoint": function(centroid) { |
| 17 | + assert.inDelta(centroid({type: "MultiPoint", coordinates: [[0, 0], [1, 2]]}), [0.499847, 0.999847], 1e-6); |
| 18 | + assert.deepEqual(centroid({type: "MultiPoint", coordinates: [[179, 0], [-179, 0]]}), [180, 0]); |
| 19 | + }, |
| 20 | + "LineString": function(centroid) { |
| 21 | + assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [1, 0]]}), [.5, 0], 1e-6); |
| 22 | + assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [0, 90]]}), [0, 45], 1e-6); |
| 23 | + assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [0, 45], [0, 90]]}), [0, 45], 1e-6); |
| 24 | + assert.inDelta(centroid({type: "LineString", coordinates: [[-1, -1], [1, 1]]}), [0, 0], 1e-6); |
| 25 | + assert.inDelta(centroid({type: "LineString", coordinates: [[-60, -1], [60, 1]]}), [0, 0], 1e-6); |
| 26 | + assert.inDelta(centroid({type: "LineString", coordinates: [[179, -1], [-179, 1]]}), [180, 0], 1e-6); |
| 27 | + assert.inDelta(centroid({type: "LineString", coordinates: [[-179, 0], [0, 0], [179, 0]]}), [0, 0], 1e-6); |
| 28 | + assert.inDelta(centroid({type: "LineString", coordinates: [[0, -90], [0, 90]]}), [0, 0], 1e-6); |
| 29 | + assert.inDelta(centroid({type: "LineString", coordinates: [[-180, -90], [0, 90]]}), [-90, 0], 1e-6); |
| 30 | + }, |
| 31 | + "MultiLineString": function(centroid) { |
| 32 | + assert.inDelta(centroid({type: "MultiLineString", coordinates: [[[0, 0], [0, 2]]]}), [0, 1], 1e-6); |
| 33 | + }, |
| 34 | + "Polygon": function(centroid) { |
| 35 | + assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -90], [0, 0], [0, 90], [1, 0], [0, -90]]]}), [.5, 0], 1e-6); |
| 36 | + assert.inDelta(centroid(d3.geo.circle().angle(5).origin([0, 45])()), [0, 45], 1e-6); |
| 37 | + assert.equal(centroid({type: "Polygon", coordinates: [d3.range(-180, 180 + 1 / 2, 1).map(function(x) { return [x, -60]; })]})[1], -90); |
| 38 | + assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -10], [0, 10], [10, 10], [10, -10], [0, -10]]]}), [5, 0], 1e-6); |
| 39 | + }, |
| 40 | + "MultiPolygon": function(centroid) { |
| 41 | + assert.inDelta(centroid({type: "MultiPolygon", coordinates: [[[[0, -90], [0, 0], [0, 90], [1, 0], [0, -90]]]]}), [.5, 0], 1e-6); |
| 42 | + }, |
| 43 | + "Sphere": function(centroid) { |
| 44 | + assert.isUndefined(centroid({type: "Sphere"})); |
| 45 | + }, |
| 46 | + "Feature": function(centroid) { |
| 47 | + assert.deepEqual(centroid({type: "Feature", geometry: {type: "Point", coordinates: [0, 0]}}), [0, 0]); |
| 48 | + }, |
| 49 | + "FeatureCollection": function(centroid) { |
| 50 | + assert.isUndefined(centroid({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Point", coordinates: [0, 0]}}]})); |
| 51 | + }, |
| 52 | + "GeometryCollection": function(centroid) { |
| 53 | + assert.isUndefined(centroid({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}]})); |
| 54 | + } |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +suite.export(module); |
0 commit comments