forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentroid-test.js
More file actions
187 lines (160 loc) · 10.4 KB
/
Copy pathcentroid-test.js
File metadata and controls
187 lines (160 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var vows = require("vows"),
_ = require("../../"),
load = require("../load"),
assert = require("../assert");
var suite = vows.describe("d3.geo.centroid");
suite.addBatch({
"centroid": {
topic: load("geo/centroid").expression("d3.geo.centroid"),
"the centroid of a point is itself": function(centroid) {
assert.inDelta(centroid({type: "Point", coordinates: [0, 0]}), [0, 0], 1e-6);
assert.inDelta(centroid({type: "Point", coordinates: [1, 1]}), [1, 1], 1e-6);
assert.inDelta(centroid({type: "Point", coordinates: [2, 3]}), [2, 3], 1e-6);
assert.inDelta(centroid({type: "Point", coordinates: [-4, -5]}), [-4, -5], 1e-6);
},
"the centroid of a set of points is the (spherical) average of its constituent members": function(centroid) {
assert.inDelta(centroid({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}, {type: "Point", coordinates: [1, 2]}]}), [0.499847, 1.000038], 1e-6);
assert.inDelta(centroid({type: "MultiPoint", coordinates: [[0, 0], [1, 2]]}), [0.499847, 1.000038], 1e-6);
assert.inDelta(centroid({type: "MultiPoint", coordinates: [[179, 0], [-179, 0]]}), [180, 0], 1e-6);
},
"the centroid of a set of points and their antipodes is ambiguous": function(centroid) {
assert.ok(centroid({type: "MultiPoint", coordinates: [[0, 0], [180, 0]]}).every(isNaN));
assert.ok(centroid({type: "MultiPoint", coordinates: [[0, 0], [90, 0], [180, 0], [-90, 0]]}).every(isNaN));
assert.ok(centroid({type: "MultiPoint", coordinates: [[0, 0], [0, 90], [180, 0], [0, -90]]}).every(isNaN));
},
"the centroid of the empty set of points is ambiguous": function(centroid) {
assert.ok(centroid({type: "MultiPoint", coordinates: []}).every(isNaN));
},
"the centroid of a line string is the (spherical) average of its constituent great arc segments": function(centroid) {
assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [1, 0]]}), [.5, 0], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [0, 90]]}), [0, 45], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[0, 0], [0, 45], [0, 90]]}), [0, 45], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[-1, -1], [1, 1]]}), [0, 0], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[-60, -1], [60, 1]]}), [0, 0], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[179, -1], [-179, 1]]}), [180, 0], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[-179, 0], [0, 0], [179, 0]]}), [0, 0], 1e-6);
assert.inDelta(centroid({type: "LineString", coordinates: [[-180, -90], [0, 0], [0, 90]]}), [0, 0], 1e-6);
},
"the centroid of a great arc from a point to its antipode is ambiguous": function(centroid) {
assert.ok(centroid({type: "LineString", coordinates: [[180, 0], [0, 0]]}).every(isNaN));
assert.ok(centroid({type: "MultiLineString", coordinates: [[[0, -90], [0, 90]]]}).every(isNaN));
},
"the centroid of a set of line strings is the (spherical) average of its constituent great arc segments": function(centroid) {
assert.inDelta(centroid({type: "MultiLineString", coordinates: [[[0, 0], [0, 2]]]}), [0, 1], 1e-6);
},
"a line of zero length is treated as points": function(centroid) {
assert.inDelta(centroid({type: "LineString", coordinates: [[1, 1], [1, 1]]}), [1, 1], 1e-6);
assert.inDelta(centroid({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}, {type: "LineString", coordinates: [[1, 2], [1, 2]]}]}), [0.666534, 1.333408], 1e-6);
},
"an empty polygon with non-zero extent is treated as a line": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[1, 1], [2, 1], [3, 1], [2, 1], [1, 1]]]}), [2, 1.000076], 1e-6);
assert.inDelta(centroid({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}, {type: "Polygon", coordinates: [[[1, 2], [1, 2], [1, 2], [1, 2]]]}]}), [0.799907, 1.600077], 1e-6);
},
"an empty polygon with zero extent is treated as a point": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[1, 1], [1, 1], [1, 1], [1, 1]]]}), [1, 1], 1e-6);
assert.inDelta(centroid({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}, {type: "Polygon", coordinates: [[[1, 2], [1, 2], [1, 2], [1, 2]]]}]}), [0.799907, 1.600077], 1e-6);
},
"the centroid of the equator is ambiguous": function(centroid) {
assert.ok(centroid({type: "LineString", coordinates: [[0, 0], [120, 0], [-120, 0], [0, 0]]}).every(isNaN));
},
"the centroid of a polygon is the (spherical) average of its surface": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -90], [0, 0], [0, 90], [1, 0], [0, -90]]]}), [.5, 0], 1e-6);
assert.inDelta(centroid({type: "Polygon", coordinates: [_.range(-180, 180 + 1 / 2, 1).map(function(x) { return [x, -60]; })]})[1], -90, 1e-6);
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -10], [0, 10], [10, 10], [10, -10], [0, -10]]]}), [5, 0], 1e-6);
},
"the centroid of a set of polygons is the (spherical) average of its surface": function(centroid) {
var circle = _.geo.circle();
assert.inDelta(centroid({
type: "MultiPolygon",
coordinates: [
circle.angle(45).origin([0, 0])().coordinates,
circle.angle(60).origin([180, 0])().coordinates
]
}), [180, 0], 1e-6);
},
"the centroid of a lune is the (spherical) average of its surface": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -90], [0, 0], [0, 90], [1, 0], [0, -90]]]}), [.5, 0], 1e-6);
},
"the centroid of a small circle is its origin": {
"5°": function(centroid) {
assert.inDelta(centroid(_.geo.circle().angle(5).origin([30, 45])()), [30, 45], 1e-6);
},
"135°": function(centroid) {
assert.inDelta(centroid(_.geo.circle().angle(135).origin([30, 45])()), [30, 45], 1e-6);
},
"South Pole": function(centroid) {
assert.equal(centroid({type: "Polygon", coordinates: [_.range(-180, 180 + 1 / 2, 1).map(function(x) { return [x, -60]; })]})[1], -90);
},
"equator": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -10], [0, 10], [10, 10], [10, -10], [0, -10]]]}), [5, 0], 1e-6);
},
"equator with coincident points": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -10], [0, 10], [0, 10], [10, 10], [10, -10], [0, -10]]]}), [5, 0], 1e-6);
},
"other": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[-180, 0], [-180, 10], [-179, 10], [-179, 0], [-180, 0]]]}), [-179.5, 4.987448], 1e-6);
},
"concentric rings": function(centroid) {
var circle = _.geo.circle().origin([0, 45]),
coordinates = circle.angle(60)().coordinates;
coordinates.push(circle.angle(45)().coordinates[0].reverse());
assert.inDelta(centroid({type: "Polygon", coordinates: coordinates}), [0, 45], 1e-6);
}
},
"the centroid of a spherical square on the equator": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[0, -10], [0, 10], [10, 10], [10, -10], [0, -10]]]}), [5, 0], 1e-6);
},
"the centroid of a spherical square touching the antimeridian": function(centroid) {
assert.inDelta(centroid({type: "Polygon", coordinates: [[[-180, 0], [-180, 10], [-179, 10], [-179, 0], [-180, 0]]]}), [-179.5, 4.987448], 1e-6);
},
"concentric rings": function(centroid) {
var circle = _.geo.circle().origin([0, 45]),
coordinates = circle.angle(60)().coordinates;
coordinates.push(circle.angle(45)().coordinates[0].reverse());
assert.inDelta(centroid({type: "Polygon", coordinates: coordinates}), [0, 45], 1e-6);
},
"the centroid of a sphere is ambiguous": function(centroid) {
assert.ok(centroid({type: "Sphere"}).every(isNaN));
},
"the centroid of a feature is the centroid of its constituent geometry": function(centroid) {
assert.inDelta(centroid({type: "Feature", geometry: {type: "LineString", coordinates: [[1, 1], [1, 1]]}}), [1, 1], 1e-6);
assert.inDelta(centroid({type: "Feature", geometry: {type: "Point", coordinates: [1, 1]}}), [1, 1], 1e-6);
assert.inDelta(centroid({type: "Feature", geometry: {type: "Polygon", coordinates: [[[0, -90], [0, 0], [0, 90], [1, 0], [0, -90]]]}}), [.5, 0], 1e-6);
},
"the centroid of a feature collection is the centroid of its constituent geometry": function(centroid) {
assert.inDelta(centroid({type: "FeatureCollection", features: [
{type: "Feature", geometry: {type: "LineString", coordinates: [[179, 0], [180, 0]]}},
{type: "Feature", geometry: {type: "Point", coordinates: [0, 0]}}
]}), [179.5, 0], 1e-6);
},
"the centroid of a non-empty line string and a point only considers the line string": function(centroid) {
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
{type: "Point", coordinates: [0, 0]}
]}), [179.5, 0], 1e-6);
},
"the centroid of a non-empty polygon, a non-empty line string and a point only considers the polygon": function(centroid) {
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
{type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]},
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
{type: "Point", coordinates: [0, 0]}
]}), [-179.5, 0.500006], 1e-6);
assert.inDelta(centroid({type: "GeometryCollection", geometries: [
{type: "Point", coordinates: [0, 0]},
{type: "LineString", coordinates: [[179, 0], [180, 0]]},
{type: "Polygon", coordinates: [[[-180, 0], [-180, 1], [-179, 1], [-179, 0], [-180, 0]]]}
]}), [-179.5, 0.500006], 1e-6);
},
"the centroid of the sphere and a point is the point": function(centroid) {
assert.deepEqual(centroid({type: "GeometryCollection", geometries: [
{type: "Sphere"},
{type: "Point", coordinates: [0, 0]}
]}), [0, 0]);
assert.deepEqual(centroid({type: "GeometryCollection", geometries: [
{type: "Point", coordinates: [0, 0]},
{type: "Sphere"}
]}), [0, 0]);
}
}
});
suite.export(module);