Debian Codegraph @

topojson/test/topology-test.js

raw
1 var tape = require("tape"),
2 topojson = require("../");
3
4 // The topology `objects` is a map of geometry objects by name, allowing
5 // multiple GeoJSON geometry objects to share the same topology. When you
6 // pass multiple input files to bin/topojson, the basename of the file is
7 // used as the key, but you're welcome to edit the file to change it.
8 tape("topology input objects are mapped to topology.objects", function(test) {
9 var topology = topojson.topology({
10 foo: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]},
11 bar: {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}
12 });
13 test.equal(topology.objects.foo.type, "LineString");
14 test.equal(topology.objects.bar.type, "Polygon");
15 test.end();
16 });
17
18 // TopoJSON doesn't use features because you can represent the same
19 // information more compactly just by using geometry objects.
20 tape("topology features are mapped to geometries", function(test) {
21 var topology = topojson.topology({
22 foo: {type: "Feature", geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}},
23 bar: {type: "Feature", geometry: {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}}
24 });
25 test.equal(topology.objects.foo.type, "LineString");
26 test.equal(topology.objects.bar.type, "Polygon");
27 test.end();
28 });
29 tape("topology feature collections are mapped to geometry collections", function(test) {
30 var topology = topojson.topology({
31 collection: {
32 type: "FeatureCollection",
33 features: [
34 {type: "Feature", geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}},
35 {type: "Feature", geometry: {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}}
36 ]
37 }
38 });
39 test.equal(topology.objects.collection.type, "GeometryCollection");
40 test.equal(topology.objects.collection.geometries.length, 2);
41 test.equal(topology.objects.collection.geometries[0].type, "LineString");
42 test.equal(topology.objects.collection.geometries[1].type, "Polygon");
43 test.end();
44 });
45 tape("topology nested geometry collections", function(test) {
46 var topology = topojson.topology({
47 collection: {
48 type: "GeometryCollection",
49 geometries: [
50 {type: "GeometryCollection", geometries: [
51 {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}
52 ]},
53 {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}
54 ]
55 }
56 });
57 test.equal(topology.objects.collection.geometries[0].geometries[0].arcs.length, 1);
58 test.end();
59 });
60 tape("topology null geometry objects are preserved in geometry collections", function(test) {
61 var topology = topojson.topology({
62 collection: {
63 type: "GeometryCollection",
64 geometries: [
65 null,
66 {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}
67 ]
68 }
69 });
70 test.equal(topology.objects.collection.type, "GeometryCollection");
71 test.equal(topology.objects.collection.geometries.length, 2);
72 test.equal(topology.objects.collection.geometries[0].type, null);
73 test.equal(topology.objects.collection.geometries[1].type, "Polygon");
74 test.end();
75 });
76 tape("topology features with null geometry objects are preserved in feature collections", function(test) {
77 var topology = topojson.topology({
78 collection: {
79 type: "FeatureCollection",
80 features: [
81 {type: "Feature", geometry: null},
82 {type: "Feature", geometry: {type: "Polygon", coordinates: [[[.5, .6], [.7, .8]]]}}
83 ]
84 }
85 });
86 test.equal(topology.objects.collection.type, "GeometryCollection");
87 test.equal(topology.objects.collection.geometries.length, 2);
88 test.equal(topology.objects.collection.geometries[0].type, null);
89 test.equal(topology.objects.collection.geometries[1].type, "Polygon");
90 test.end();
91 });
92 tape("topology top-level features with null geometry objects are preserved", function(test) {
93 var topology = topojson.topology({feature: {type: "Feature", geometry: null}});
94 test.deepEqual(topology.objects, {feature: {type: null}});
95 test.end();
96 });
97
98 // To know what a geometry object represents, specify an id. I prefer
99 // numeric identifiers, such as ISO 3166-1 numeric, but strings work too.
100 tape("topology converting a feature to a geometry preserves its id", function(test) {
101 var topology = topojson.topology({foo: {type: "Feature", id: 42, properties: {}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}});
102 test.equal(topology.objects.foo.type, "LineString");
103 test.equal(topology.objects.foo.id, 42);
104 test.end();
105 });
106
107 // I prefer to store properties in separate files (such as CSV), so that it
108 // can be represented more efficiently. Also, this lets you use the same
109 // TopoJSON file in multiple applications. Use the geometry object's id to
110 // join with external data, e.g., http://bl.ocks.org/4060606
111 tape("topology converting a feature to a geometry does not preserve its properties by default", function(test) {
112 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {name: "George"}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}});
113 test.equal(topology.objects.foo.properties, undefined);
114 test.end();
115 });
116
117 // But you can store properties on TopoJSON geometries, if you insist.
118 tape("topology a properties transform may be specified to preserve and rename properties", function(test) {
119 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {UNREASONABLY_LONG_NAME: "George"}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}}, {"property-transform": function(object) { return object.properties; }});
120 test.deepEqual(topology.objects.foo.properties, {UNREASONABLY_LONG_NAME: "George"});
121 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {UNREASONABLY_LONG_NAME: "George"}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}}, {"property-transform": function(object) { return {name: object.properties.UNREASONABLY_LONG_NAME}; }});
122 test.deepEqual(topology.objects.foo.properties, {name: "George"});
123 test.end();
124 });
125 tape("topology a properties transform can apply to geometry collections", function(test) {
126 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {UNREASONABLY_LONG_NAME: "George"}, geometry: {type: "GeometryCollection", geometries: [{type: "LineString", coordinates: [[.1, .2], [.3, .4]]}]}}}, {"property-transform": function(object) { return {name: object.properties.UNREASONABLY_LONG_NAME}; }});
127 test.deepEqual(topology.objects.foo.properties, {name: "George"});
128 test.end();
129 });
130
131 // Unlike GeoJSON's feature.properties, the properties object is optional.
132 tape("topology if no properties are specified, no properties are emitted", function(test) {
133 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {name: "George", demeanor: "curious"}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}}, {"property-transform": function(object) { return {name: object.properties.name}; }});
134 test.deepEqual(topology.objects.foo.properties, {name: "George"});
135 var topology = topojson.topology({foo: {type: "Feature", id: "Foo", properties: {demeanor: "curious"}, geometry: {type: "LineString", coordinates: [[.1, .2], [.3, .4]]}}}, {"property-transform": function(object) { return {}; }});
136 test.deepEqual(topology.objects.foo.properties);
137 test.end();
138 });
139
140 // It's not required by the specification that the transform exactly
141 // encompass the input geometry, but this is a good test that the reference
142 // implementation is working correctly.
143 tape("topology the returned transform exactly encompasses the input geometry", function(test) {
144 var topology = topojson.topology({foo: {type: "LineString", coordinates: [[1/8, 1/16], [1/2, 1/4]]}}, {quantization: 2});
145 test.deepEqual(topology.transform, {scale: [3/8, 3/16], translate: [1/8, 1/16]});
146 var topology = topojson.topology({foo: {type: "Polygon", coordinates: [[[1/8, 1/16], [1/2, 1/16], [1/2, 1/4], [1/8, 1/4], [1/8, 1/16]]]}}, {quantization: 2});
147 test.deepEqual(topology.transform, {scale: [3/8, 3/16], translate: [1/8, 1/16]});
148 test.end();
149 });
150
151 // TopoJSON uses integers with delta encoding to represent geometry
152 // efficiently. (Quantization is necessary for simplification anyway, so
153 // that we can identify which points are shared by contiguous geometry
154 // objects.) The delta encoding works particularly well because line strings
155 // are not random: most points are very close to their neighbors!
156 tape("topology arc coordinates are integers with delta encoding", function(test) {
157 // var topology = topojson.topology({foo: {type: "LineString", coordinates: [[1/8, 1/16], [1/2, 1/16], [1/8, 1/4], [1/2, 1/4]]}}, {quantization: 2});
158 // test.deepEqual(topology.arcs[0], [[0, 0], [1, 0], [-1, 1], [1, 0]]);
159 var topology = topojson.topology({foo: {type: "Polygon", coordinates: [[[1/8, 1/16], [1/2, 1/16], [1/2, 1/4], [1/8, 1/4], [1/8, 1/16]]]}}, {quantization: 2});
160 test.deepEqual(topology.arcs[0], [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]]);
161 test.end();
162 });
163
164 // TopoJSON uses integers with for points, also. However, there’s no delta-
165 // encoding, even for MultiPoints. And, unlike other geometry objects,
166 // points are still defined with coordinates rather than arcs.
167 tape("topology points coordinates are integers with delta encoding", function(test) {
168 var topology = topojson.topology({foo: {type: "Point", coordinates: [1/8, 1/16]}, bar: {type: "Point", coordinates: [1/2, 1/4]}}, {quantization: 2});
169 test.deepEqual(topology.arcs, []);
170 test.deepEqual(topology.objects.foo, {type: "Point", coordinates: [0, 0]});
171 test.deepEqual(topology.objects.bar, {type: "Point", coordinates: [1, 1]});
172 var topology = topojson.topology({foo: {type: "MultiPoint", coordinates: [[1/8, 1/16], [1/2, 1/4]]}}, {quantization: 2});
173 test.deepEqual(topology.arcs, []);
174 test.deepEqual(topology.objects.foo, {type: "MultiPoint", coordinates: [[0, 0], [1, 1]]});
175 test.end();
176 });
177
178 // Rounding is more accurate than flooring.
179 tape("topology quantization rounds to the closest integer coordinate to minimize error", function(test) {
180 var topology = topojson.topology({foo: {type: "LineString", coordinates: [[0.0, 0.0], [0.5, 0.5], [1.6, 1.6], [3.0, 3.0], [4.1, 4.1], [4.9, 4.9], [5.9, 5.9], [6.5, 6.5], [7.0, 7.0], [8.4, 8.4], [8.5, 8.5], [10, 10]]}}, {quantization: 11});
181 test.deepEqual(topojson.feature(topology, topology.objects.foo).geometry.coordinates, [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9], [10, 10]]);
182 test.deepEqual(topology.arcs, [[[0, 0], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]]);
183 test.deepEqual(topology.transform, {scale: [1, 1], translate: [0, 0]});
184 test.end();
185 });
186
187 // When rounding, we must be careful not to exceed [±180°, ±90°]!
188 tape("topology quantization precisely preserves minimum and maximum values", function(test) {
189 var topology = topojson.topology({foo: {type: "LineString", coordinates: [[-180, -90], [0, 0], [180, 90]]}}, {quantization: 3});
190 test.deepEqual(topojson.feature(topology, topology.objects.foo).geometry.coordinates, [[-180, -90], [0, 0], [180, 90]]);
191 test.deepEqual(topology.arcs, [[[0, 0], [1, 1], [1, 1]]]);
192 test.deepEqual(topology.transform, {scale: [180, 90], translate: [-180, -90]});
193 test.end();
194 });
195
196 // GeoJSON inputs are in floating point format, so some error may creep in
197 // that prevents you from using exact match to determine shared points. The
198 // default quantization, 1e4, allows for 10,000 differentiable points in
199 // both dimensions. If you're using TopoJSON to represent especially high-
200 // precision geometry, you might want to increase the precision; however,
201 // this necessarily increases the output size and the likelihood of seams
202 // between contiguous geometry after simplification. The quantization factor
203 // should be a power of ten for the most efficient representation, since
204 // JSON uses base-ten encoding for numbers.
205 tape("topology precision of quantization is configurable", function(test) {
206 var topology = topojson.topology({foo: {type: "LineString", coordinates: [[1/8, 1/16], [1/2, 1/16], [1/8, 1/4], [1/2, 1/4]]}}, {quantization: 3});
207 test.deepEqual(topology.arcs[0], [[0, 0], [2, 0], [-2, 2], [2, 0]]);
208 var topology = topojson.topology({foo: {type: "Polygon", coordinates: [[[1/8, 1/16], [1/2, 1/16], [1/2, 1/4], [1/8, 1/4], [1/8, 1/16]]]}}, {quantization: 5});
209 test.deepEqual(topology.arcs[0], [[0, 0], [4, 0], [0, 4], [-4, 0], [0, -4]]);
210 test.end();
211 });
212
213 // Quantization may introduce coincident points, so these are removed.
214 tape("topology coincident points are removed", function(test) {
215 var topology = topojson.topology({foo: {type: "LineString", coordinates: [[1/8, 1/16], [1/8, 1/16], [1/2, 1/4], [1/2, 1/4]]}}, {quantization: 2});
216 test.deepEqual(topology.arcs, [[[0, 0], [1, 1]]]);
217 var topology = topojson.topology({foo: {type: "Polygon", coordinates: [[[1/8, 1/16], [1/2, 1/16], [1/2, 1/16], [1/2, 1/4], [1/8, 1/4], [1/8, 1/4], [1/8, 1/16]]]}}, {quantization: 2});
218 test.deepEqual(topology.arcs[0], [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]]);
219 test.end();
220 });
221
222 // Quantization may introduce degenerate features which have collapsed onto a single point.
223 tape("topology collapsed lines are preserved", function(test) {
224 var topology = topojson.topology({
225 foo: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 2]]},
226 bar: {type: "LineString", coordinates: [[-80, -80], [0, 0], [80, 80]]}
227 }, {quantization: 3});
228 test.deepEqual(topology.objects.foo, {type: "LineString", arcs: [0]});
229 test.deepEqual(topology.arcs[0], [[1, 1], [0, 0]]);
230 test.end();
231 });
232 tape("topology collapsed lines in a MultiLineString are preserved", function(test) {
233 var topology = topojson.topology({foo: {type: "MultiLineString", coordinates: [[[1/8, 1/16], [1/2, 1/4]], [[1/8, 1/16], [1/8, 1/16]], [[1/2, 1/4], [1/8, 1/16]]]}}, {quantization: 2});
234 test.equal(topology.arcs.length, 2);
235 test.deepEqual(topology.arcs[1], [[0, 0], [0, 0]]);
236 test.deepEqual(topology.arcs[0], [[0, 0], [1, 1]]);
237 test.deepEqual(topology.objects.foo.arcs, [[0], [1], [~0]]);
238 test.end();
239 });
240 tape("topology collapsed polygons are preserved", function(test) {
241 var topology = topojson.topology({
242 foo: {type: "Polygon", coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]},
243 bar: {type: "Polygon", coordinates: [[[0, 0], [0, 1], [1, 1], [0, 0]]]},
244 baz: {type: "MultiPoint", coordinates: [[-80, -80], [0, 0], [80, 80]]}
245 }, {quantization: 3});
246 test.deepEqual(topology.objects.foo, {type: "Polygon", arcs: [[0]]});
247 test.deepEqual(topology.objects.bar, {type: "Polygon", arcs: [[0]]});
248 test.deepEqual(topology.arcs[0], [[1, 1], [0, 0], [0, 0], [0, 0]]);
249 test.end();
250 });
251 tape("topology collapsed polygons in a MultiPolygon are preserved", function(test) {
252 var topology = topojson.topology({foo: {type: "MultiPolygon", coordinates: [
253 [[[1/8, 1/16], [1/2, 1/16], [1/2, 1/4], [1/8, 1/4], [1/8, 1/16]]],
254 [[[1/8, 1/16], [1/8, 1/16], [1/8, 1/16], [1/8, 1/16]]],
255 [[[1/8, 1/16], [1/8, 1/4], [1/2, 1/4], [1/2, 1/16], [1/8, 1/16]]]
256 ]}}, {quantization: 2});
257 test.equal(topology.arcs.length > 0, true);
258 test.equal(topology.arcs[0].length >= 2, true);
259 test.equal(topology.objects.foo.arcs.length === 3, true);
260 test.end();
261 });
262 tape("topology collapsed geometries in a GeometryCollection are preserved", function(test) {
263 var topology = topojson.topology({collection: {type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "MultiPolygon", coordinates: []}}]}}, {quantization: 2});
264 test.equal(topology.arcs.length, 0);
265 test.deepEqual(topology.objects.collection, {type: "GeometryCollection", geometries: [{type: null}]});
266 test.end();
267 });
268
269 // If one of the top-level objects in the input is empty, however, it is
270 // still preserved in the output.
271 tape("topology empty geometries are not removed", function(test) {
272 var topology = topojson.topology({foo: {type: "MultiPolygon", coordinates: []}}, {quantization: 2});
273 test.equal(topology.arcs.length, 0);
274 test.deepEqual(topology.objects.foo, {type: null});
275 test.end();
276 });
277 tape("topology empty polygons are not removed", function(test) {
278 var topology = topojson.topology({
279 foo: {type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "MultiPolygon", coordinates: [[]]}}]},
280 bar: {type: "Polygon", coordinates: []}
281 });
282 test.equal(topology.arcs.length, 0);
283 test.deepEqual(topology.objects.foo, {type: "GeometryCollection", geometries: [{type: null}]});
284 test.deepEqual(topology.objects.bar, {type: null});
285 test.end();
286 });
287
288 //
289 // A-----B
290 //
291 tape("topology the lines AB and AB share the same arc", function(test) {
292 var topology = topojson.topology({
293 ab: {type: "LineString", coordinates: [[0, 0], [0, 1]]},
294 ba: {type: "LineString", coordinates: [[0, 0], [0, 1]]}
295 });
296 test.deepEqual(topology.objects.ab, {type: "LineString", arcs: [0]});
297 test.deepEqual(topology.objects.ba, {type: "LineString", arcs: [0]});
298 test.end();
299 });
300
301 //
302 // A-----B
303 //
304 tape("topology the lines AB and BA share the same arc", function(test) {
305 var topology = topojson.topology({
306 ab: {type: "LineString", coordinates: [[0, 0], [0, 1]]},
307 ba: {type: "LineString", coordinates: [[0, 1], [0, 0]]}
308 });
309 test.deepEqual(topology.objects.ab, {type: "LineString", arcs: [0]});
310 test.deepEqual(topology.objects.ba, {type: "LineString", arcs: [~0]});
311 test.end();
312 });
313
314 //
315 // A
316 // \
317 // \
318 // \
319 // \
320 // \
321 // B-----C-----D
322 //
323 tape("topology the lines ACD and BCD share three arcs", function(test) {
324 var topology = topojson.topology({
325 acd: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1]]},
326 bcd: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1]]}
327 });
328 test.deepEqual(topology.objects.acd, {type: "LineString", arcs: [0, 1]});
329 test.deepEqual(topology.objects.bcd, {type: "LineString", arcs: [2, 1]});
330 test.end();
331 });
332
333 //
334 // A
335 // \
336 // \
337 // \
338 // \
339 // \
340 // B-----C-----D
341 //
342 tape("topology the lines ACD and DCB share three arcs", function(test) {
343 var topology = topojson.topology({
344 acd: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1]]},
345 dcb: {type: "LineString", coordinates: [[2, 1], [1, 1], [0, 1]]}
346 }, {quantization: 3});
347 test.deepEqual(topology.arcs, [
348 [[0, 0], [1, 2]], // AC
349 [[1, 2], [1, 0]], // CD
350 [[1, 2], [-1, 0]], // CB
351 ]);
352 test.deepEqual(topology.objects.acd, {type: "LineString", arcs: [0, 1]});
353 test.deepEqual(topology.objects.dcb, {type: "LineString", arcs: [~1, 2]});
354 test.end();
355 });
356
357 //
358 // A
359 // \
360 // \
361 // \
362 // \
363 // \
364 // B-----C-----D-----F
365 //
366 tape("topology the lines ACDF and BCDF share three arcs", function(test) {
367 var topology = topojson.topology({
368 acdf: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 1]]},
369 bcdf: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 1]]}
370 }, {quantization: 4});
371 test.deepEqual(topology.arcs, [
372 [[0, 0], [1, 3]], // AC
373 [[1, 3], [1, 0], [1, 0]], // CDF
374 [[0, 3], [1, 0]] // BC
375 ]);
376 test.deepEqual(topology.objects.acdf, {type: "LineString", arcs: [0, 1]});
377 test.deepEqual(topology.objects.bcdf, {type: "LineString", arcs: [2, 1]});
378 test.end();
379 });
380
381 //
382 // E
383 // /
384 // /
385 // /
386 // /
387 // /
388 // B-----C-----D-----F
389 //
390 tape("topology the lines BCDE and BCDF share three arcs", function(test) {
391 var topology = topojson.topology({
392 bcde: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 0]]},
393 bcdf: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 1]]}
394 }, {quantization: 4});
395 test.deepEqual(topology.arcs, [
396 [[0, 3], [1, 0], [1, 0]], // BCD
397 [[2, 3], [1, -3]], // DE
398 [[2, 3], [1, 0]] // DF
399 ]);
400 test.deepEqual(topology.objects.bcde, {type: "LineString", arcs: [0, 1]});
401 test.deepEqual(topology.objects.bcdf, {type: "LineString", arcs: [0, 2]});
402 test.end();
403 });
404
405 //
406 // A E
407 // \ /
408 // \ /
409 // \ /
410 // \ /
411 // \ /
412 // C-----D
413 //
414 tape("topology the lines ACDE and CD share three arcs", function(test) {
415 var topology = topojson.topology({
416 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
417 cd: {type: "LineString", coordinates: [[1, 1], [2, 1]]}
418 }, {quantization: 4});
419 test.deepEqual(topology.arcs, [
420 [[0, 0], [1, 3]], // AC
421 [[1, 3], [1, 0]], // CD
422 [[2, 3], [1, -3]] // DE
423 ]);
424 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1, 2]});
425 test.deepEqual(topology.objects.cd, {type: "LineString", arcs: [1]});
426 test.end();
427 });
428
429 //
430 // A E
431 // \ /
432 // \ /
433 // \ /
434 // \ /
435 // \ /
436 // B-----C-----D
437 //
438 tape("topology the lines ACDE and BCD share four arcs", function(test) {
439 var topology = topojson.topology({
440 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
441 bcd: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1]]}
442 }, {quantization: 4});
443 test.deepEqual(topology.arcs, [
444 [[0, 0], [1, 3]], // AC
445 [[1, 3], [1, 0]], // CD
446 [[2, 3], [1, -3]], // DE
447 [[0, 3], [1, 0]] // BC
448 ]);
449 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1, 2]});
450 test.deepEqual(topology.objects.bcd, {type: "LineString", arcs: [3, 1]});
451 test.end();
452 });
453
454 //
455 // A E
456 // \ /
457 // \ /
458 // \ /
459 // \ /
460 // \ /
461 // C-----D-----F
462 //
463 tape("topology the lines ACDE and CDF share four arcs", function(test) {
464 var topology = topojson.topology({
465 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
466 cdf: {type: "LineString", coordinates: [[1, 1], [2, 1], [3, 1]]}
467 }, {quantization: 4});
468 test.deepEqual(topology.arcs, [
469 [[0, 0], [1, 3]], // AC
470 [[1, 3], [1, 0]], // CD
471 [[2, 3], [1, -3]], // DE
472 [[2, 3], [1, 0]] // CF
473 ]);
474 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1, 2]});
475 test.deepEqual(topology.objects.cdf, {type: "LineString", arcs: [1, 3]});
476 test.end();
477 });
478
479 //
480 // A E
481 // \ /
482 // \ /
483 // \ /
484 // \ /
485 // \ /
486 // B-----C-----D-----F
487 //
488 tape("topology the lines ACDE and BCDF share five arcs", function(test) {
489 var topology = topojson.topology({
490 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
491 bcdf: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 1]]}
492 }, {quantization: 4});
493 test.deepEqual(topology.arcs, [
494 [[0, 0], [1, 3]], // AC
495 [[1, 3], [1, 0]], // CD
496 [[2, 3], [1, -3]], // DE
497 [[0, 3], [1, 0]], // BC
498 [[2, 3], [1, 0]] // DF
499 ]);
500 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1, 2]});
501 test.deepEqual(topology.objects.bcdf, {type: "LineString", arcs: [3, 1, 4]});
502 test.end();
503 });
504
505 //
506 // A E
507 // \ /
508 // \ /
509 // \ /
510 // \ /
511 // \ /
512 // C-----D-----F
513 //
514 tape("topology the lines ACDE, EDCA and ACDF share three arcs", function(test) {
515 var topology = topojson.topology({
516 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
517 edca: {type: "LineString", coordinates: [[3, 0], [2, 1], [1, 1], [0, 0]]},
518 acdf: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 1]]}
519 }, {quantization: 4});
520 test.deepEqual(topology.arcs, [
521 [[0, 0], [1, 3], [1, 0]], // ACD
522 [[2, 3], [1, -3]], // DE
523 [[2, 3], [1, 0]] // DF
524 ]);
525 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1]});
526 test.deepEqual(topology.objects.acdf, {type: "LineString", arcs: [0, 2]});
527 test.deepEqual(topology.objects.edca, {type: "LineString", arcs: [~1, ~0]});
528 test.end();
529 });
530
531 //
532 // A E
533 // \ /
534 // \ /
535 // \ /
536 // \ /
537 // \ /
538 // C-----D-----F
539 //
540 tape("topology the lines ACDE, ACDF and EDCA share three arcs", function(test) {
541 var topology = topojson.topology({
542 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
543 acdf: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 1]]},
544 edca: {type: "LineString", coordinates: [[3, 0], [2, 1], [1, 1], [0, 0]]}
545 }, {quantization: 4});
546 test.deepEqual(topology.arcs, [
547 [[0, 0], [1, 3], [1, 0]], // ACD
548 [[2, 3], [1, -3]], // DE
549 [[2, 3], [1, 0]] // DF
550 ]);
551 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1]});
552 test.deepEqual(topology.objects.acdf, {type: "LineString", arcs: [0, 2]});
553 test.deepEqual(topology.objects.edca, {type: "LineString", arcs: [~1, ~0]});
554 test.end();
555 });
556
557 //
558 // A E
559 // \ /
560 // \ /
561 // \ /
562 // \ /
563 // \ /
564 // B-----C-----D-----F
565 //
566 tape("topology the lines ACDE, ACDF, BCDE and BCDF and their reversals share five arcs", function(test) {
567 var topology = topojson.topology({
568 acde: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 0]]},
569 acdf: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1], [3, 1]]},
570 bcde: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 0]]},
571 bcdf: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 1], [3, 1]]},
572 edca: {type: "LineString", coordinates: [[3, 0], [2, 1], [1, 1], [0, 0]]},
573 fdca: {type: "LineString", coordinates: [[3, 1], [2, 1], [1, 1], [0, 0]]},
574 edcb: {type: "LineString", coordinates: [[3, 0], [2, 1], [1, 1], [0, 1]]},
575 fdcb: {type: "LineString", coordinates: [[3, 1], [2, 1], [1, 1], [0, 1]]}
576 }, {quantization: 4});
577 test.deepEqual(topology.arcs, [
578 [[0, 0], [1, 3]], // AC
579 [[1, 3], [1, 0]], // CD
580 [[2, 3], [1, -3]], // DE
581 [[2, 3], [1, 0]], // DF
582 [[0, 3], [1, 0]] // BC
583 ]);
584 test.deepEqual(topology.objects.acde, {type: "LineString", arcs: [0, 1, 2]});
585 test.deepEqual(topology.objects.acdf, {type: "LineString", arcs: [0, 1, 3]});
586 test.deepEqual(topology.objects.bcde, {type: "LineString", arcs: [4, 1, 2]});
587 test.deepEqual(topology.objects.bcdf, {type: "LineString", arcs: [4, 1, 3]});
588 test.deepEqual(topology.objects.edca, {type: "LineString", arcs: [~2, ~1, ~0]});
589 test.deepEqual(topology.objects.fdca, {type: "LineString", arcs: [~3, ~1, ~0]});
590 test.deepEqual(topology.objects.edcb, {type: "LineString", arcs: [~2, ~1, ~4]});
591 test.deepEqual(topology.objects.fdcb, {type: "LineString", arcs: [~3, ~1, ~4]});
592 test.end();
593 });
594
595 //
596 // A-----B-----E
597 // | | |
598 // | | |
599 // | | |
600 // | | |
601 // | | |
602 // D-----C-----F
603 //
604 tape("topology the polygons ABCDA and BEFCB share three arcs", function(test) {
605 var topology = topojson.topology({
606 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
607 befcb: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]}
608 }, {quantization: 3});
609 test.deepEqual(topology.arcs, [
610 [[1, 0], [0, 2]], // BC
611 [[1, 2], [-1, 0], [0, -2], [1, 0]], // CDAB
612 [[1, 0], [1, 0], [0, 2], [-1, 0]] // BEFC
613 ]);
614 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0, 1]]});
615 test.deepEqual(topology.objects.befcb, {type: "Polygon", arcs: [[2, ~0]]});
616 test.end();
617 });
618
619 //
620 // A-----B
621 // |\ |
622 // | \ |
623 // | \ |
624 // | \ |
625 // | \|
626 // D-----C
627 //
628 tape("topology the polygons ABCDA and ABCA share three arcs", function(test) {
629 var topology = topojson.topology({
630 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
631 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 0]]]}
632 }, {quantization: 2});
633 test.deepEqual(topology.arcs, [
634 [[0, 0], [1, 0], [0, 1]], // ABC
635 [[1, 1], [-1, 0], [0, -1]], // CDA
636 [[1, 1], [-1, -1]] // CA
637 ]);
638 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0, 1]]});
639 test.deepEqual(topology.objects.abca, {type: "Polygon", arcs: [[0, 2]]});
640 test.end();
641 });
642
643 //
644 // C
645 // / \
646 // / \
647 // / \
648 // / \
649 // / \
650 // A-----B-----------D-----E
651 //
652 tape("topology the lines ABCDE and ABDE share two arcs", function(test) {
653 var topology = topojson.topology({
654 abcde: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]]},
655 abde: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]}
656 }, {quantization: 5});
657 test.deepEqual(topology.arcs, [
658 [[0, 0], [1, 0]], // AB
659 [[1, 0], [1, 0], [1, 0]], // BCD
660 [[3, 0], [1, 0]], // DE
661 [[1, 0], [2, 0]] // BD
662 ]);
663 test.deepEqual(topology.objects.abcde, {type: "LineString", arcs: [0, 1, 2]});
664 test.deepEqual(topology.objects.abde, {type: "LineString", arcs: [0, 3, 2]});
665 test.end();
666 });
667
668 //
669 // A-----B
670 // |\ |\
671 // | \ | \
672 // | \ | \
673 // | \ | \
674 // | \| \
675 // D-----C-----F
676 //
677 tape("topology the polygons ABCA, ACDA and BFCB share five arcs", function(test) {
678 var topology = topojson.topology({
679 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 0]]]},
680 acda: {type: "Polygon", coordinates: [[[0, 0], [1, 1], [0, 1], [0, 0]]]},
681 bfcb: {type: "Polygon", coordinates: [[[1, 0], [2, 1], [1, 1], [1, 0]]]}
682 }, {quantization: 3});
683 test.deepEqual(topology.arcs, [
684 [[0, 0], [1, 0]], // AB
685 [[1, 0], [0, 2]], // BC
686 [[1, 2], [-1, -2]], // CA
687 [[1, 2], [-1, 0], [0, -2]], // CDA
688 [[1, 0], [1, 2], [-1, 0]] // BFC
689 ]);
690 test.deepEqual(topology.objects.abca, {type: "Polygon", arcs: [[0, 1, 2]]});
691 test.deepEqual(topology.objects.acda, {type: "Polygon", arcs: [[~2, 3]]});
692 test.deepEqual(topology.objects.bfcb, {type: "Polygon", arcs: [[4, ~1]]});
693 test.end();
694 });
695
696 //
697 // A-----B-----E
698 // \ | |\
699 // \ | | \
700 // \ | | \
701 // \ | | \
702 // \| | \
703 // C-----F-----G
704 //
705 tape("topology the polygons ABCA, BEFCB and EGFE share six arcs", function(test) {
706 var topology = topojson.topology({
707 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 0]]]},
708 befcb: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]},
709 egfe: {type: "Polygon", coordinates: [[[2, 0], [3, 1], [2, 1], [2, 0]]]}
710 }, {quantization: 4});
711 test.deepEqual(topology.arcs, [
712 [[1, 0], [0, 3]], // BC
713 [[1, 3], [-1, -3], [1, 0]], // CAB
714 [[1, 0], [1, 0]], // BE
715 [[2, 0], [0, 3]], // EF
716 [[2, 3], [-1, 0]], // FC
717 [[2, 0], [1, 3], [-1, 0]] // EGF
718 ]);
719 test.deepEqual(topology.objects.abca, {type: "Polygon", arcs: [[0, 1]]});
720 test.deepEqual(topology.objects.befcb, {type: "Polygon", arcs: [[2, 3, 4, ~0]]});
721 test.deepEqual(topology.objects.egfe, {type: "Polygon", arcs: [[5, ~3]]});
722 test.end();
723 });
724
725 //
726 // A-----B-----E
727 // | | |
728 // | | |
729 // D-----C |
730 // | |
731 // | |
732 // G-----------F
733 //
734 tape("topology the polygons ABCDA, ABEFGDA and BEFGDCB share three arcs", function(test) {
735 var topology = topojson.topology({
736 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
737 abefgda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [2, 2], [0, 2], [0, 1], [0, 0]]]},
738 befgdcb: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 2], [0, 2], [0, 1], [1, 1], [1, 0]]]}
739 }, {quantization: 3});
740 test.deepEqual(topology.arcs, [
741 [[1, 0], [0, 1], [-1, 0]], // BCD
742 [[0, 1], [0, -1], [1, 0]], // DAB
743 [[1, 0], [1, 0], [0, 2], [-2, 0], [0, -1]] // BEFGD
744 ]);
745 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0, 1]]});
746 test.deepEqual(topology.objects.abefgda, {type: "Polygon", arcs: [[2, 1]]});
747 test.deepEqual(topology.objects.befgdcb, {type: "Polygon", arcs: [[2, ~0]]});
748 test.end();
749 });
750
751 //
752 // A-----B
753 // | |
754 // | |
755 // D-----C
756 //
757 tape("topology the polygons ABCDA and BCDAB share one arc", function(test) {
758 var topology = topojson.topology({
759 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
760 bcdab: {type: "Polygon", coordinates: [[[1, 0], [1, 1], [0, 1], [0, 0], [1, 0]]]}
761 }, {quantization: 2});
762 test.deepEqual(topology.arcs, [
763 [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]]
764 ]);
765 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0]]});
766 test.deepEqual(topology.objects.bcdab, {type: "Polygon", arcs: [[0]]});
767 test.end();
768 });
769
770 //
771 // A-----------------B
772 // | |
773 // | |
774 // | E-----F |
775 // | | | |
776 // | | | |
777 // | H-----G |
778 // | |
779 // | |
780 // D-----------------C
781 //
782 tape("topology the polygons ABCDA-EHGFE and EFGHE share two arcs", function(test) {
783 var topology = topojson.topology({
784 abcda: {type: "Polygon", coordinates: [[[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]], [[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]]},
785 efghe: {type: "Polygon", coordinates: [[[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]]]}
786 }, {quantization: 4});
787 test.deepEqual(topology.arcs, [
788 [[0, 0], [3, 0], [0, 3], [-3, 0], [0, -3]], // ABCDA
789 [[1, 1], [0, 1], [1, 0], [0, -1], [-1, 0]] // EHGFE
790 ]);
791 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0], [1]]});
792 test.deepEqual(topology.objects.efghe, {type: "Polygon", arcs: [[~1]]});
793 test.end();
794 });
795
796 //
797 // A-----------------B
798 // | |
799 // | |
800 // | E-----F |
801 // | | | |
802 // | | | |
803 // | H-----G |
804 // | |
805 // | |
806 // D-----------------C
807 //
808 tape("topology the polygons ABCDA-EHGFE and FGHEF share two arcs", function(test) {
809 var topology = topojson.topology({
810 abcda: {type: "Polygon", coordinates: [[[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]], [[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]]},
811 fghef: {type: "Polygon", coordinates: [[[2, 1], [2, 2], [1, 2], [1, 1], [2, 1]]]}
812 }, {quantization: 4});
813 test.deepEqual(topology.arcs, [
814 [[0, 0], [3, 0], [0, 3], [-3, 0], [0, -3]], // ABCDA
815 [[1, 1], [0, 1], [1, 0], [0, -1], [-1, 0]] // EHGFE
816 ]);
817 test.deepEqual(topology.objects.abcda, {type: "Polygon", arcs: [[0], [1]]});
818 test.deepEqual(topology.objects.fghef, {type: "Polygon", arcs: [[~1]]});
819 test.end();
820 });
821
822 //
823 // C-----D
824 // \ /
825 // \ /
826 // A-----B-----E
827 //
828 tape("topology the polygon BCDB and the line string ABE share three arcs", function(test) {
829 var topology = topojson.topology({
830 abe: {type: "LineString", coordinates: [[0, 1], [2, 1], [4, 1]]},
831 bcdb: {type: "Polygon", coordinates: [[[2, 1], [1, 0], [3, 0], [2, 1]]]}
832 }, {quantization: 5});
833 test.deepEqual(topology.arcs, [
834 [[0, 4], [2, 0]], // AB
835 [[2, 4], [2, 0]], // BE
836 [[2, 4], [-1, -4], [2, 0], [-1, 4]] // BCDB
837 ]);
838 test.deepEqual(topology.objects.abe, {type: "LineString", arcs: [0, 1]});
839 test.deepEqual(topology.objects.bcdb, {type: "Polygon", arcs: [[2]]});
840 test.end();
841 });
842
843 //
844 // A-----B-----C-----D-----E
845 // | |
846 // | |
847 // J-----I-----H-----G-----F
848 tape("topology a polygon surrounding the South pole with a cut along the antimeridian", function(test) {
849 var topology = topojson.topology({
850 polygon: {type: "Polygon", coordinates: [[
851 [-180, -80], [-90, -80], [0, -80], [90, -80], [180, -80],
852 [180, -90], [90, -90], [0, -90], [-90, -90], [-180, -90],
853 [-180, -80]
854 ]]}}, {quantization: 4});
855 test.deepEqual(topology.arcs, [
856 [[0, 3], [1, 0], [1, 0], [-2, 0]]
857 ]);
858 test.deepEqual(topology.objects.polygon, {type: "Polygon", arcs: [[0]]});
859 test.end();
860 });
861
862 //
863 // B-----C-----D-----E-----F
864 // | |
865 // | |
866 // A G
867 // | |
868 // | |
869 // L-----K-----J-----I-----H
870 tape("topology a large polygon surrounding the South pole with a cut along the antimeridian", function(test) {
871 var topology = topojson.topology({
872 polygon: {type: "Polygon", coordinates: [[
873 [-180, -85], [-180, -80], [-90, -80], [0, -80], [90, -80], [180, -80],
874 [180, -85], [180, -90], [90, -90], [0, -90], [-90, -90], [-180, -90],
875 [-180, -85]
876 ]]}}, {quantization: 5});
877 test.deepEqual(topology.arcs, [
878 [[0, 4], [1, 0], [1, 0], [1, 0], [-3, 0]]
879 ]);
880 test.deepEqual(topology.objects.polygon, {type: "Polygon", arcs: [[0]]});
881 test.end();
882 });
883
884 //
885 // A-----B-----C-----D
886 // | |
887 // N E
888 // \ /
889 // M F
890 // / \
891 // L G
892 // | |
893 // K-----J-----I-----H
894 tape("topology a large polygon with a hole across the antimeridian and cut along the antimeridian", function(test) {
895 var topology = topojson.topology({
896 polygon: {type: "Polygon", coordinates: [[
897 [-180, -60], [-180, -30], [-150, 0], [-180, 30], [-180, 60], [-60, 60], [60, 60],
898 [180, 60], [180, 30], [150, 0], [180, -30], [180, -60], [60, -60], [-60, -60], [-180, -60]
899 ]]}}, {quantization: 8});
900 test.deepEqual(topology.arcs, [
901 [[0, 7], [2, 0], [3, 0], [-5, 0]],
902 [[0, 0], [5, 0], [-3, 0], [-2, 0]],
903 [[0, 5], [6, -1], [-6, -2], [1, 2], [-1, 1]]
904 ]);
905 test.deepEqual(topology.objects.polygon, {type: "Polygon", arcs: [[0], [1], [2]]});
906 test.end();
907 });