Debian Codegraph @

topojson/test/neighbors-test.js

raw
1 var tape = require("tape"),
2 topojson = require("../");
3
4 tape("neighbors returns an empty array for empty input", function(test) {
5 var topology = topojson.topology({
6 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]},
7 cd: {type: "LineString", coordinates: [[0, 1], [1, 1]]}
8 }, {quantization: 2});
9 test.deepEqual(topojson.neighbors([]), []);
10 test.end();
11 });
12
13 //
14 // A-----B
15 //
16 // C-----D
17 //
18 tape("neighbors returns an empty array for objects with no neighbors", function(test) {
19 var topology = topojson.topology({
20 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]},
21 cd: {type: "LineString", coordinates: [[0, 1], [1, 1]]}
22 }, {quantization: 2});
23 test.deepEqual(topojson.neighbors([
24 topology.objects.ab,
25 topology.objects.cd
26 ]), [
27 [],
28 []
29 ]);
30 test.end();
31 });
32
33 //
34 // A-----B-----C
35 //
36 tape("neighbors geometries that only share isolated points are not considered neighbors", function(test) {
37 var topology = topojson.topology({
38 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]},
39 bc: {type: "LineString", coordinates: [[1, 0], [2, 0]]}
40 }, {quantization: 3});
41 test.deepEqual(topojson.neighbors([
42 topology.objects.ab,
43 topology.objects.bc
44 ]), [
45 [],
46 []
47 ]);
48 test.end();
49 });
50
51 //
52 // A-----B-----C-----D
53 //
54 tape("neighbors geometries that share arcs are considered neighbors", function(test) {
55 var topology = topojson.topology({
56 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
57 bcd: {type: "LineString", coordinates: [[1, 0], [2, 0], [3, 0]]}
58 }, {quantization: 4});
59 test.deepEqual(topojson.neighbors([
60 topology.objects.abc,
61 topology.objects.bcd
62 ]), [
63 [1],
64 [0]
65 ]);
66 test.end();
67 });
68
69 //
70 // A-----B-----C-----D
71 //
72 tape("neighbors geometries that share reversed arcs are considered neighbors", function(test) {
73 var topology = topojson.topology({
74 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
75 dcb: {type: "LineString", coordinates: [[3, 0], [2, 0], [1, 0]]}
76 }, {quantization: 4});
77 test.deepEqual(topojson.neighbors([
78 topology.objects.abc,
79 topology.objects.dcb
80 ]), [
81 [1],
82 [0]
83 ]);
84 test.end();
85 });
86
87 //
88 // A-----B-----C-----D-----E-----F
89 //
90 tape("neighbors neighbors are returned in sorted order by index", function(test) {
91 var topology = topojson.topology({
92 abcd: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0]]},
93 bcde: {type: "LineString", coordinates: [[1, 0], [2, 0], [3, 0], [4, 0]]},
94 cdef: {type: "LineString", coordinates: [[2, 0], [3, 0], [4, 0], [5, 0]]},
95 dbca: {type: "LineString", coordinates: [[3, 0], [2, 0], [1, 0], [0, 0]]},
96 edcb: {type: "LineString", coordinates: [[4, 0], [3, 0], [2, 0], [1, 0]]},
97 fedc: {type: "LineString", coordinates: [[5, 0], [4, 0], [3, 0], [2, 0]]}
98 }, {quantization: 6});
99 test.deepEqual(topojson.neighbors([
100 topology.objects.abcd,
101 topology.objects.bcde,
102 topology.objects.cdef,
103 topology.objects.dbca,
104 topology.objects.edcb,
105 topology.objects.fedc
106 ]), [
107 [1, 2, 3, 4, 5],
108 [0, 2, 3, 4, 5],
109 [0, 1, 3, 4, 5],
110 [0, 1, 2, 4, 5],
111 [0, 1, 2, 3, 5],
112 [0, 1, 2, 3, 4]
113 ]);
114 test.end();
115 });
116
117 //
118 // A-----B-----E G
119 // | | | |\
120 // | | | | \
121 // | | | | \
122 // | | | | \
123 // | | | | \
124 // D-----C-----F I-----H
125 //
126 tape("neighbors the polygons ABCDA and BEFCB are neighbors, but GHIG is not", function(test) {
127 var topology = topojson.topology({
128 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
129 befcb: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]},
130 ghig: {type: "Polygon", coordinates: [[[3, 0], [4, 1], [3, 1], [3, 0]]]}
131 }, {quantization: 5});
132 test.deepEqual(topojson.neighbors([
133 topology.objects.abcda,
134 topology.objects.befcb,
135 topology.objects.ghig
136 ]), [
137 [1],
138 [0],
139 []
140 ]);
141 test.end();
142 });
143
144 //
145 // A-----------B-----------C
146 // | | |
147 // | | |
148 // | D-----E-----F |
149 // | | | |
150 // | | | |
151 // | G-----H-----I |
152 // | | |
153 // | | |
154 // J-----------K-----------L
155 //
156 tape("neighbors the polygons ABEDGHKJA and BCLKHIFEB are neighbors, and not listed twice", function(test) {
157 var topology = topojson.topology({
158 abdeghkja: {type: "Polygon", coordinates: [[[0, 0], [2, 0], [2, 1], [1, 1], [1, 2], [2, 2], [2, 3], [0, 3], [0, 0]]]},
159 bclkhifeb: {type: "Polygon", coordinates: [[[2, 0], [4, 0], [4, 3], [2, 3], [2, 2], [3, 2], [3, 1], [2, 1], [2, 0]]]}
160 }, {quantization: 5});
161 test.deepEqual(topojson.neighbors([
162 topology.objects.abdeghkja,
163 topology.objects.bclkhifeb
164 ]), [
165 [1],
166 [0]
167 ]);
168 test.end();
169 });