Debian Codegraph @

topojson/test/topology/join-test.js

raw
1 var tape = require("tape"),
2 extract = require("../../lib/topojson/topology/extract"),
3 join = require("../../lib/topojson/topology/join");
4
5 tape("join the returned hashmap has true for junction points", function(test) {
6 var junctions = join(extract({
7 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
8 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]}
9 }));
10 test.equal(junctions.has([2, 0]), true);
11 test.equal(junctions.has([0, 0]), true);
12 test.end();
13 });
14
15 tape("join the returned hashmap has undefined for non-junction points", function(test) {
16 var junctions = join(extract({
17 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
18 ab: {type: "LineString", coordinates: [[0, 0], [2, 0]]}
19 }));
20 test.equal(junctions.has([1, 0]), false);
21 test.end();
22 });
23
24 tape("join exact duplicate lines ABC & ABC have junctions at their end points", function(test) {
25 var junctions = join(extract({
26 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
27 abc2: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
28 }));
29 testSetEqual(test, junctions.values(), [[0, 0], [2, 0]]);
30 test.end();
31 });
32
33 tape("join reversed duplicate lines ABC & CBA have junctions at their end points", function(test) {
34 var junctions = join(extract({
35 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
36 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]}
37 }));
38 testSetEqual(test, junctions.values(), [[0, 0], [2, 0]]);
39 test.end();
40 });
41
42 tape("join exact duplicate rings ABCA & ABCA have no junctions", function(test) {
43 var junctions = join(extract({
44 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
45 abca2: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]}
46 }));
47 testSetEqual(test, junctions.values(), []);
48 test.end();
49 });
50
51 tape("join reversed duplicate rings ACBA & ABCA have no junctions", function(test) {
52 var junctions = join(extract({
53 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
54 acba: {type: "Polygon", coordinates: [[[0, 0], [2, 0], [1, 0], [0, 0]]]}
55 }));
56 testSetEqual(test, junctions.values(), []);
57 test.end();
58 });
59
60 tape("join rotated duplicate rings BCAB & ABCA have no junctions", function(test) {
61 var junctions = join(extract({
62 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
63 bcab: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [0, 0], [1, 0]]]}
64 }));
65 testSetEqual(test, junctions.values(), []);
66 test.end();
67 });
68
69 tape("join ring ABCA & line ABCA have a junction at A", function(test) {
70 var junctions = join(extract({
71 abcaLine: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [0, 0]]},
72 abcaPolygon: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
73 }));
74 testSetEqual(test, junctions.values(), [[0, 0]]);
75 test.end();
76 });
77
78 tape("join ring BCAB & line ABCA have a junction at A", function(test) {
79 var junctions = join(extract({
80 abcaLine: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [0, 0]]},
81 bcabPolygon: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [0, 0], [1, 0]]]},
82 }));
83 testSetEqual(test, junctions.values(), [[0, 0]]);
84 test.end();
85 });
86
87 tape("join ring ABCA & line BCAB have a junction at B", function(test) {
88 var junctions = join(extract({
89 bcabLine: {type: "LineString", coordinates: [[1, 0], [2, 0], [0, 0], [1, 0]]},
90 abcaPolygon: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
91 }));
92 testSetEqual(test, junctions.values(), [[1, 0]]);
93 test.end();
94 });
95
96 tape("join when an old arc ABC extends a new arc AB, there is a junction at B", function(test) {
97 var junctions = join(extract({
98 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
99 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]}
100 }));
101 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
102 test.end();
103 });
104
105 tape("join when a reversed old arc CBA extends a new arc AB, there is a junction at B", function(test) {
106 var junctions = join(extract({
107 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
108 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]}
109 }));
110 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
111 test.end();
112 });
113
114 tape("join when a new arc ADE shares its start with an old arc ABC, there is a junction at A", function(test) {
115 var junctions = join(extract({
116 ade: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
117 abc: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1]]}
118 }));
119 testSetEqual(test, junctions.values(), [[0, 0], [2, 0], [2, 1]]);
120 test.end();
121 });
122
123 tape("join ring ABA has no junctions", function(test) {
124 var junctions = join(extract({
125 aba: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 0]]]},
126 }));
127 testSetEqual(test, junctions.values(), []);
128 test.end();
129 });
130
131 tape("join ring AA has no junctions", function(test) {
132 var junctions = join(extract({
133 aa: {type: "Polygon", coordinates: [[[0, 0], [0, 0]]]},
134 }));
135 testSetEqual(test, junctions.values(), []);
136 test.end();
137 });
138
139 tape("join degenerate ring A has no junctions", function(test) {
140 var junctions = join(extract({
141 a: {type: "Polygon", coordinates: [[[0, 0]]]},
142 }));
143 testSetEqual(test, junctions.values(), []);
144 test.end();
145 });
146
147 tape("join when a new line DEC shares its end with an old line ABC, there is a junction at C", function(test) {
148 var junctions = join(extract({
149 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
150 dec: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 0]]}
151 }));
152 testSetEqual(test, junctions.values(), [[0, 0], [2, 0], [0, 1]]);
153 test.end();
154 });
155
156 tape("join when a new line ABC extends an old line AB, there is a junction at B", function(test) {
157 var junctions = join(extract({
158 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]},
159 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
160 }));
161 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
162 test.end();
163 });
164
165 tape("join when a new line ABC extends a reversed old line BA, there is a junction at B", function(test) {
166 var junctions = join(extract({
167 ba: {type: "LineString", coordinates: [[1, 0], [0, 0]]},
168 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
169 }));
170 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
171 test.end();
172 });
173
174 tape("join when a new line starts BC in the middle of an old line ABC, there is a junction at B", function(test) {
175 var junctions = join(extract({
176 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
177 bc: {type: "LineString", coordinates: [[1, 0], [2, 0]]}
178 }));
179 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
180 test.end();
181 });
182
183 tape("join when a new line BC starts in the middle of a reversed old line CBA, there is a junction at B", function(test) {
184 var junctions = join(extract({
185 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
186 bc: {type: "LineString", coordinates: [[1, 0], [2, 0]]}
187 }));
188 testSetEqual(test, junctions.values(), [[0, 0], [1, 0], [2, 0]]);
189 test.end();
190 });
191
192 tape("join when a new line ABD deviates from an old line ABC, there is a junction at B", function(test) {
193 var junctions = join(extract({
194 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
195 abd: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0]]}
196 }));
197 testSetEqual(test, junctions.values(), [[0, 0], [2, 0], [1, 0], [3, 0]]);
198 test.end();
199 });
200
201 tape("join when a new line ABD deviates from a reversed old line CBA, there is a junction at B", function(test) {
202 var junctions = join(extract({
203 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
204 abd: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0]]}
205 }));
206 testSetEqual(test, junctions.values(), [[2, 0], [0, 0], [1, 0], [3, 0]]);
207 test.end();
208 });
209
210 tape("join when a new line DBC merges into an old line ABC, there is a junction at B", function(test) {
211 var junctions = join(extract({
212 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
213 dbc: {type: "LineString", coordinates: [[3, 0], [1, 0], [2, 0]]}
214 }));
215 testSetEqual(test, junctions.values(), [[0, 0], [2, 0], [1, 0], [3, 0]]);
216 test.end();
217 });
218
219 tape("join when a new line DBC merges into a reversed old line CBA, there is a junction at B", function(test) {
220 var junctions = join(extract({
221 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
222 dbc: {type: "LineString", coordinates: [[3, 0], [1, 0], [2, 0]]}
223 }));
224 testSetEqual(test, junctions.values(), [[2, 0], [0, 0], [1, 0], [3, 0]]);
225 test.end();
226 });
227
228 tape("join when a new line DBE shares a single midpoint with an old line ABC, there is a junction at B", function(test) {
229 var junctions = join(extract({
230 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
231 dbe: {type: "LineString", coordinates: [[0, 1], [1, 0], [2, 1]]}
232 }));
233 testSetEqual(test, junctions.values(), [[0, 0], [2, 0], [2, 1], [1, 0], [0, 1]]);
234 test.end();
235 });
236
237 tape("join when a new line ABDE skips a point with an old line ABCDE, there is a junction at B and D", function(test) {
238 var junctions = join(extract({
239 abcde: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]]},
240 abde: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]}
241 }));
242 testSetEqual(test, junctions.values(), [[0, 0], [4, 0], [1, 0], [3, 0]]);
243 test.end();
244 });
245
246 tape("join when a new line ABDE skips a point with a reversed old line EDCBA, there is a junction at B and D", function(test) {
247 var junctions = join(extract({
248 edcba: {type: "LineString", coordinates: [[4, 0], [3, 0], [2, 0], [1, 0], [0, 0]]},
249 abde: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]}
250 }));
251 testSetEqual(test, junctions.values(), [[4, 0], [0, 0], [1, 0], [3, 0]]);
252 test.end();
253 });
254
255 tape("join when a line ABCDBE self-intersects with its middle, there are no junctions", function(test) {
256 var junctions = join(extract({
257 abcdbe: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]}
258 }));
259 testSetEqual(test, junctions.values(), [[0, 0], [4, 0]]);
260 test.end();
261 });
262
263 tape("join when a line ABACD self-intersects with its start, there are no junctions", function(test) {
264 var junctions = join(extract({
265 abacd: {type: "LineString", coordinates: [[0, 0], [1, 0], [0, 0], [3, 0], [4, 0]]}
266 }));
267 testSetEqual(test, junctions.values(), [[0, 0], [4, 0]]);
268 test.end();
269 });
270
271 tape("join when a line ABCDBD self-intersects with its end, there are no junctions", function(test) {
272 var junctions = join(extract({
273 abcdbd: {type: "LineString", coordinates: [[0, 0], [1, 0], [4, 0], [3, 0], [4, 0]]}
274 }));
275 testSetEqual(test, junctions.values(), [[0, 0], [4, 0]]);
276 test.end();
277 });
278
279 tape("join when an old line ABCDBE self-intersects and shares a point B, there is a junction at B", function(test) {
280 var junctions = join(extract({
281 abcdbe: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]},
282 fbg: {type: "LineString", coordinates: [[0, 1], [1, 0], [2, 1]]}
283 }));
284 testSetEqual(test, junctions.values(), [[0, 0], [4, 0], [1, 0], [0, 1], [2, 1]]);
285 test.end();
286 });
287
288 tape("join when a line ABCA is closed, there is a junction at A", function(test) {
289 var junctions = join(extract({
290 abca: {type: "LineString", coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]}
291 }));
292 testSetEqual(test, junctions.values(), [[0, 0]]);
293 test.end();
294 });
295
296 tape("join when a ring ABCA is closed, there are no junctions", function(test) {
297 var junctions = join(extract({
298 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]}
299 }));
300 testSetEqual(test, junctions.values(), []);
301 test.end();
302 });
303
304 tape("join exact duplicate rings ABCA & ABCA share the arc ABCA", function(test) {
305 var junctions = join(extract({
306 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
307 abca2: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]}
308 }));
309 testSetEqual(test, junctions.values(), []);
310 test.end();
311 });
312
313 tape("join reversed duplicate rings ABCA & ACBA share the arc ABCA", function(test) {
314 var junctions = join(extract({
315 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
316 acba: {type: "Polygon", coordinates: [[[0, 0], [0, 1], [1, 0], [0, 0]]]}
317 }));
318 testSetEqual(test, junctions.values(), []);
319 test.end();
320 });
321
322 tape("join coincident rings ABCA & BCAB share the arc BCAB", function(test) {
323 var junctions = join(extract({
324 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
325 bcab: {type: "Polygon", coordinates: [[[1, 0], [0, 1], [0, 0], [1, 0]]]}
326 }));
327 testSetEqual(test, junctions.values(), []);
328 test.end();
329 });
330
331 tape("join coincident rings ABCA & BACB share the arc BCAB", function(test) {
332 var junctions = join(extract({
333 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
334 bacb: {type: "Polygon", coordinates: [[[1, 0], [0, 0], [0, 1], [1, 0]]]}
335 }));
336 testSetEqual(test, junctions.values(), []);
337 test.end();
338 });
339
340 tape("join coincident rings ABCA & DBED share the point B", function(test) {
341 var junctions = join(extract({
342 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
343 dbed: {type: "Polygon", coordinates: [[[2, 1], [1, 0], [2, 2], [2, 1]]]}
344 }));
345 testSetEqual(test, junctions.values(), [[1, 0]]);
346 test.end();
347 });
348
349 tape("join coincident ring ABCA & line DBE share the point B", function(test) {
350 var junctions = join(extract({
351 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
352 dbe: {type: "LineString", coordinates: [[2, 1], [1, 0], [2, 2]]}
353 }));
354 testSetEqual(test, junctions.values(), [[2, 1], [2, 2], [1, 0]]);
355 test.end();
356 });
357
358 function testSetEqual(test, pointsA, pointsB) {
359 test.deepEqual(pointsA.sort(comparePoint), pointsB.sort(comparePoint));
360 }
361
362 function comparePoint(pointA, pointB) {
363 return pointA[0] - pointB[0] || pointA[1] - pointB[1];
364 }