Debian Codegraph @

topojson/test/topology/index-test.js

raw
1 var tape = require("tape"),
2 index = require("../../lib/topojson/topology/index");
3
4 tape("topology exact duplicate lines ABC & ABC share the arc ABC", function(test) {
5 var topology = index({
6 foo: {
7 type: "LineString",
8 coordinates: [[0, 0], [1, 0], [2, 0]]
9 },
10 bar: {
11 type: "LineString",
12 coordinates: [[0, 0], [1, 0], [2, 0]]
13 }
14 });
15 test.deepEqual(topology, {
16 type: "Topology",
17 arcs: [
18 [[0, 0], [1, 0], [2, 0]]
19 ],
20 objects: {
21 foo: {
22 type: "LineString",
23 arcs: [0]
24 },
25 bar: {
26 type: "LineString",
27 arcs: [0]
28 }
29 }
30 });
31 test.end();
32 });
33
34 tape("topology reversed duplicate lines ABC & CBA share the arc ABC", function(test) {
35 var topology = index({
36 foo: {
37 type: "LineString",
38 coordinates: [[0, 0], [1, 0], [2, 0]]
39 },
40 bar: {
41 type: "LineString",
42 coordinates: [[2, 0], [1, 0], [0, 0]]
43 }
44 });
45 test.deepEqual(topology, {
46 type: "Topology",
47 arcs: [
48 [[0, 0], [1, 0], [2, 0]]
49 ],
50 objects: {
51 foo: {
52 type: "LineString",
53 arcs: [0]
54 },
55 bar: {
56 type: "LineString",
57 arcs: [~0]
58 }
59 }
60 });
61 test.end();
62 });
63
64 tape("topology when an old arc ABC extends a new arc AB, they share the arc AB", function(test) {
65 var topology = index({
66 foo: {
67 type: "LineString",
68 coordinates: [[0, 0], [1, 0], [2, 0]]
69 },
70 bar: {
71 type: "LineString",
72 coordinates: [[0, 0], [1, 0]]
73 }
74 });
75 test.deepEqual(topology, {
76 type: "Topology",
77 arcs: [
78 [[0, 0], [1, 0]],
79 [[1, 0], [2, 0]]
80 ],
81 objects: {
82 foo: {
83 type: "LineString",
84 arcs: [0, 1]
85 },
86 bar: {
87 type: "LineString",
88 arcs: [0]
89 }
90 }
91 });
92 test.end();
93 });
94
95 tape("topology when a reversed old arc CBA extends a new arc AB, they share the arc BA", function(test) {
96 var topology = index({
97 foo: {
98 type: "LineString",
99 coordinates: [[2, 0], [1, 0], [0, 0]]
100 },
101 bar: {
102 type: "LineString",
103 coordinates: [[0, 0], [1, 0]]
104 }
105 });
106 test.deepEqual(topology, {
107 type: "Topology",
108 arcs: [
109 [[2, 0], [1, 0]],
110 [[1, 0], [0, 0]]
111 ],
112 objects: {
113 foo: {
114 type: "LineString",
115 arcs: [0, 1]
116 },
117 bar: {
118 type: "LineString",
119 arcs: [~1]
120 }
121 }
122 });
123 test.end();
124 });
125
126 tape("topology when a new arc ADE shares its start with an old arc ABC, they don’t share arcs", function(test) {
127 var topology = index({
128 foo: {
129 type: "LineString",
130 coordinates: [[0, 0], [1, 0], [2, 0]]
131 },
132 bar: {
133 type: "LineString",
134 coordinates: [[0, 0], [1, 1], [2, 1]]
135 }
136 });
137 test.deepEqual(topology, {
138 type: "Topology",
139 arcs: [
140 [[0, 0], [1, 0], [2, 0]],
141 [[0, 0], [1, 1], [2, 1]]
142 ],
143 objects: {
144 foo: {
145 type: "LineString",
146 arcs: [0]
147 },
148 bar: {
149 type: "LineString",
150 arcs: [1]
151 }
152 }
153 });
154 test.end();
155 });
156
157 tape("topology when a new arc DEC shares its start with an old arc ABC, they don’t share arcs", function(test) {
158 var topology = index({
159 foo: {
160 type: "LineString",
161 coordinates: [[0, 0], [1, 0], [2, 0]]
162 },
163 bar: {
164 type: "LineString",
165 coordinates: [[0, 1], [1, 1], [2, 0]]
166 }
167 });
168 test.deepEqual(topology, {
169 type: "Topology",
170 arcs: [
171 [[0, 0], [1, 0], [2, 0]],
172 [[0, 1], [1, 1], [2, 0]]
173 ],
174 objects: {
175 foo: {
176 type: "LineString",
177 arcs: [0]
178 },
179 bar: {
180 type: "LineString",
181 arcs: [1]
182 }
183 }
184 });
185 test.end();
186 });
187
188 tape("topology when a new arc ABC extends an old arc AB, they share the arc AB", function(test) {
189 var topology = index({
190 foo: {
191 type: "LineString",
192 coordinates: [[0, 0], [1, 0]]
193 },
194 bar: {
195 type: "LineString",
196 coordinates: [[0, 0], [1, 0], [2, 0]]
197 }
198 });
199 test.deepEqual(topology, {
200 type: "Topology",
201 arcs: [
202 [[0, 0], [1, 0]],
203 [[1, 0], [2, 0]]
204 ],
205 objects: {
206 foo: {
207 type: "LineString",
208 arcs: [0]
209 },
210 bar: {
211 type: "LineString",
212 arcs: [0, 1]
213 }
214 }
215 });
216 test.end();
217 });
218
219 tape("topology when a new arc ABC extends a reversed old arc BA, they share the arc BA", function(test) {
220 var topology = index({
221 foo: {
222 type: "LineString",
223 coordinates: [[1, 0], [0, 0]]
224 },
225 bar: {
226 type: "LineString",
227 coordinates: [[0, 0], [1, 0], [2, 0]]
228 }
229 });
230 test.deepEqual(topology, {
231 type: "Topology",
232 arcs: [
233 [[1, 0], [0, 0]],
234 [[1, 0], [2, 0]]
235 ],
236 objects: {
237 foo: {
238 type: "LineString",
239 arcs: [0]
240 },
241 bar: {
242 type: "LineString",
243 arcs: [~0, 1]
244 }
245 }
246 });
247 test.end();
248 });
249
250 tape("topology when a new arc starts BC in the middle of an old arc ABC, they share the arc BC", function(test) {
251 var topology = index({
252 foo: {
253 type: "LineString",
254 coordinates: [[0, 0], [1, 0], [2, 0]]
255 },
256 bar: {
257 type: "LineString",
258 coordinates: [[1, 0], [2, 0]]
259 }
260 });
261 test.deepEqual(topology, {
262 type: "Topology",
263 arcs: [
264 [[0, 0], [1, 0]],
265 [[1, 0], [2, 0]]
266 ],
267 objects: {
268 foo: {
269 type: "LineString",
270 arcs: [0, 1]
271 },
272 bar: {
273 type: "LineString",
274 arcs: [1]
275 }
276 }
277 });
278 test.end();
279 });
280
281 tape("topology when a new arc BC starts in the middle of a reversed old arc CBA, they share the arc CB", function(test) {
282 var topology = index({
283 foo: {
284 type: "LineString",
285 coordinates: [[2, 0], [1, 0], [0, 0]]
286 },
287 bar: {
288 type: "LineString",
289 coordinates: [[1, 0], [2, 0]]
290 }
291 });
292 test.deepEqual(topology, {
293 type: "Topology",
294 arcs: [
295 [[2, 0], [1, 0]],
296 [[1, 0], [0, 0]]
297 ],
298 objects: {
299 foo: {
300 type: "LineString",
301 arcs: [0, 1]
302 },
303 bar: {
304 type: "LineString",
305 arcs: [~0]
306 }
307 }
308 });
309 test.end();
310 });
311
312 tape("topology when a new arc ABD deviates from an old arc ABC, they share the arc AB", function(test) {
313 var topology = index({
314 foo: {
315 type: "LineString",
316 coordinates: [[0, 0], [1, 0], [2, 0]]
317 },
318 bar: {
319 type: "LineString",
320 coordinates: [[0, 0], [1, 0], [3, 0]]
321 }
322 });
323 test.deepEqual(topology, {
324 type: "Topology",
325 arcs: [
326 [[0, 0], [1, 0]],
327 [[1, 0], [2, 0]],
328 [[1, 0], [3, 0]]
329 ],
330 objects: {
331 foo: {
332 type: "LineString",
333 arcs: [0, 1]
334 },
335 bar: {
336 type: "LineString",
337 arcs: [0, 2]
338 }
339 }
340 });
341 test.end();
342 });
343
344 tape("topology when a new arc ABD deviates from a reversed old arc CBA, they share the arc BA", function(test) {
345 var topology = index({
346 foo: {
347 type: "LineString",
348 coordinates: [[2, 0], [1, 0], [0, 0]]
349 },
350 bar: {
351 type: "LineString",
352 coordinates: [[0, 0], [1, 0], [3, 0]]
353 }
354 });
355 test.deepEqual(topology, {
356 type: "Topology",
357 arcs: [
358 [[2, 0], [1, 0]],
359 [[1, 0], [0, 0]],
360 [[1, 0], [3, 0]]
361 ],
362 objects: {
363 foo: {
364 type: "LineString",
365 arcs: [0, 1]
366 },
367 bar: {
368 type: "LineString",
369 arcs: [~1, 2]
370 }
371 }
372 });
373 test.end();
374 });
375
376 tape("topology when a new arc DBC merges into an old arc ABC, they share the arc BC", function(test) {
377 var topology = index({
378 foo: {
379 type: "LineString",
380 coordinates: [[0, 0], [1, 0], [2, 0]]
381 },
382 bar: {
383 type: "LineString",
384 coordinates: [[3, 0], [1, 0], [2, 0]]
385 }
386 });
387 test.deepEqual(topology, {
388 type: "Topology",
389 arcs: [
390 [[0, 0], [1, 0]],
391 [[1, 0], [2, 0]],
392 [[3, 0], [1, 0]]
393 ],
394 objects: {
395 foo: {
396 type: "LineString",
397 arcs: [0, 1]
398 },
399 bar: {
400 type: "LineString",
401 arcs: [2, 1]
402 }
403 }
404 });
405 test.end();
406 });
407
408 tape("topology when a new arc DBC merges into a reversed old arc CBA, they share the arc CB", function(test) {
409 var topology = index({
410 foo: {
411 type: "LineString",
412 coordinates: [[2, 0], [1, 0], [0, 0]]
413 },
414 bar: {
415 type: "LineString",
416 coordinates: [[3, 0], [1, 0], [2, 0]]
417 }
418 });
419 test.deepEqual(topology, {
420 type: "Topology",
421 arcs: [
422 [[2, 0], [1, 0]],
423 [[1, 0], [0, 0]],
424 [[3, 0], [1, 0]]
425 ],
426 objects: {
427 foo: {
428 type: "LineString",
429 arcs: [0, 1]
430 },
431 bar: {
432 type: "LineString",
433 arcs: [2, ~0]
434 }
435 }
436 });
437 test.end();
438 });
439
440 tape("topology when a new arc DBE shares a single midpoint with an old arc ABC, they share the point B, but no arcs", function(test) {
441 var topology = index({
442 foo: {
443 type: "LineString",
444 coordinates: [[0, 0], [1, 0], [2, 0]]
445 },
446 bar: {
447 type: "LineString",
448 coordinates: [[0, 1], [1, 0], [2, 1]]
449 }
450 });
451 test.deepEqual(topology, {
452 type: "Topology",
453 arcs: [
454 [[0, 0], [1, 0]],
455 [[1, 0], [2, 0]],
456 [[0, 1], [1, 0]],
457 [[1, 0], [2, 1]]
458 ],
459 objects: {
460 foo: {
461 type: "LineString",
462 arcs: [0, 1]
463 },
464 bar: {
465 type: "LineString",
466 arcs: [2, 3]
467 }
468 }
469 });
470 test.end();
471 });
472
473 tape("topology when a new arc ABDE skips a point with an old arc ABCDE, they share arcs AB and DE", function(test) {
474 var topology = index({
475 foo: {
476 type: "LineString",
477 coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]]
478 },
479 bar: {
480 type: "LineString",
481 coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]
482 }
483 });
484 test.deepEqual(topology, {
485 type: "Topology",
486 arcs: [
487 [[0, 0], [1, 0]],
488 [[1, 0], [2, 0], [3, 0]],
489 [[3, 0], [4, 0]],
490 [[1, 0], [3, 0]]
491 ],
492 objects: {
493 foo: {
494 type: "LineString",
495 arcs: [0, 1, 2]
496 },
497 bar: {
498 type: "LineString",
499 arcs: [0, 3, 2]
500 }
501 }
502 });
503 test.end();
504 });
505
506 tape("topology when a new arc ABDE skips a point with a reversed old arc EDCBA, they share arcs BA and ED", function(test) {
507 var topology = index({
508 foo: {
509 type: "LineString",
510 coordinates: [[4, 0], [3, 0], [2, 0], [1, 0], [0, 0]]
511 },
512 bar: {
513 type: "LineString",
514 coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]
515 }
516 });
517 test.deepEqual(topology, {
518 type: "Topology",
519 arcs: [
520 [[4, 0], [3, 0]],
521 [[3, 0], [2, 0], [1, 0]],
522 [[1, 0], [0, 0]],
523 [[1, 0], [3, 0]]
524 ],
525 objects: {
526 foo: {
527 type: "LineString",
528 arcs: [0, 1, 2]
529 },
530 bar: {
531 type: "LineString",
532 arcs: [~2, 3, ~0]
533 }
534 }
535 });
536 test.end();
537 });
538
539 tape("topology when an arc ABCDBE self-intersects, it is still one arc", function(test) {
540 var topology = index({
541 foo: {
542 type: "LineString",
543 coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]
544 }
545 });
546 test.deepEqual(topology, {
547 type: "Topology",
548 arcs: [
549 [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]
550 ],
551 objects: {
552 foo: {
553 type: "LineString",
554 arcs: [0]
555 }
556 }
557 });
558 test.end();
559 });
560
561 tape("topology when an old arc ABCDBE self-intersects and shares a point B, the old arc has multiple cuts", function(test) {
562 var topology = index({
563 foo: {
564 type: "LineString",
565 coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]
566 },
567 bar: {
568 type: "LineString",
569 coordinates: [[0, 1], [1, 0], [2, 1]]
570 }
571 });
572 test.deepEqual(topology, {
573 type: "Topology",
574 arcs: [
575 [[0, 0], [1, 0]],
576 [[1, 0], [2, 0], [3, 0], [1, 0]],
577 [[1, 0], [4, 0]],
578 [[0, 1], [1, 0]],
579 [[1, 0], [2, 1]]
580 ],
581 objects: {
582 foo: {
583 type: "LineString",
584 arcs: [0, 1, 2]
585 },
586 bar: {
587 type: "LineString",
588 arcs: [3, 4]
589 }
590 }
591 });
592 test.end();
593 });
594
595 tape("topology when an arc ABCA is closed, it has one arc", function(test) {
596 var topology = index({
597 foo: {
598 type: "LineString",
599 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
600 }
601 });
602 test.deepEqual(topology, {
603 type: "Topology",
604 arcs: [
605 [[0, 0], [1, 0], [0, 1], [0, 0]]
606 ],
607 objects: {
608 foo: {
609 type: "LineString",
610 arcs: [0]
611 }
612 }
613 });
614 test.end();
615 });
616
617 tape("topology exact duplicate closed lines ABCA & ABCA share the arc ABCA", function(test) {
618 var topology = index({
619 foo: {
620 type: "LineString",
621 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
622 },
623 bar: {
624 type: "LineString",
625 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
626 }
627 });
628 test.deepEqual(topology, {
629 type: "Topology",
630 arcs: [
631 [[0, 0], [1, 0], [0, 1], [0, 0]]
632 ],
633 objects: {
634 foo: {
635 type: "LineString",
636 arcs: [0]
637 },
638 bar: {
639 type: "LineString",
640 arcs: [0]
641 }
642 }
643 });
644 test.end();
645 });
646
647 tape("topology reversed duplicate closed lines ABCA & ACBA share the arc ABCA", function(test) {
648 var topology = index({
649 foo: {
650 type: "LineString",
651 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
652 },
653 bar: {
654 type: "LineString",
655 coordinates: [[0, 0], [0, 1], [1, 0], [0, 0]]
656 }
657 });
658 test.deepEqual(topology, {
659 type: "Topology",
660 arcs: [
661 [[0, 0], [1, 0], [0, 1], [0, 0]]
662 ],
663 objects: {
664 foo: {
665 type: "LineString",
666 arcs: [0]
667 },
668 bar: {
669 type: "LineString",
670 arcs: [~0]
671 }
672 }
673 });
674 test.end();
675 });
676
677 tape("topology coincident closed polygons ABCA & BCAB share the arc BCAB", function(test) {
678 var topology = index({
679 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
680 bcab: {type: "Polygon", coordinates: [[[1, 0], [0, 1], [0, 0], [1, 0]]]}
681 });
682 test.deepEqual(topology, {
683 type: "Topology",
684 arcs: [
685 [[0, 0], [1, 0], [0, 1], [0, 0]]
686 ],
687 objects: {
688 abca: {type: "Polygon", arcs: [[0]]},
689 bcab: {type: "Polygon", arcs: [[0]]}
690 }
691 });
692 test.end();
693 });
694
695 tape("topology coincident reversed closed polygons ABCA & BACB share the arc BCAB", function(test) {
696 var topology = index({
697 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
698 bacb: {type: "Polygon", coordinates: [[[1, 0], [0, 0], [0, 1], [1, 0]]]}
699 });
700 test.deepEqual(topology, {
701 type: "Topology",
702 arcs: [
703 [[0, 0], [1, 0], [0, 1], [0, 0]]
704 ],
705 objects: {
706 abca: {type: "Polygon", arcs: [[0]]},
707 bacb: {type: "Polygon", arcs: [[~0]]}
708 }
709 });
710 test.end();
711 });
712
713 tape("topology coincident closed polygons ABCA & DBED share the point B", function(test) {
714 var topology = index({
715 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
716 dbed: {type: "Polygon", coordinates: [[[2, 1], [1, 0], [2, 2], [2, 1]]]}
717 });
718 test.deepEqual(topology, {
719 type: "Topology",
720 arcs: [
721 [[1, 0], [0, 1], [0, 0], [1, 0]],
722 [[1, 0], [2, 2], [2, 1], [1, 0]]
723 ],
724 objects: {
725 abca: {
726 type: "Polygon",
727 arcs: [[0]]
728 },
729 dbed: {
730 type: "Polygon",
731 arcs: [[1]]
732 }
733 }
734 });
735 test.end();
736 });