Debian Codegraph @

topojson/test/topology/cut-test.js

raw
1 var tape = require("tape"),
2 extract = require("../../lib/topojson/topology/extract"),
3 cut = require("../../lib/topojson/topology/cut");
4
5 tape("cut exact duplicate lines ABC & ABC have no cuts", function(test) {
6 var topology = cut(extract({
7 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
8 abc2: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
9 }));
10 test.deepEqual(topology.objects, {
11 abc: {type: "LineString", arcs: {0: 0, 1: 2}},
12 abc2: {type: "LineString", arcs: {0: 3, 1: 5}}
13 });
14 test.end();
15 });
16
17 tape("cut reversed duplicate lines ABC & CBA have no cuts", function(test) {
18 var topology = cut(extract({
19 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
20 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]}
21 }));
22 test.deepEqual(topology.objects, {
23 abc: {type: "LineString", arcs: {0: 0, 1: 2}},
24 cba: {type: "LineString", arcs: {0: 3, 1: 5}}
25 });
26 test.end();
27 });
28
29 tape("cut exact duplicate rings ABCA & ABCA have no cuts", function(test) {
30 var topology = cut(extract({
31 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
32 abca2: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]}
33 }));
34 test.deepEqual(topology.objects, {
35 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
36 abca2: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
37 });
38 test.end();
39 });
40
41 tape("cut reversed duplicate rings ACBA & ABCA have no cuts", function(test) {
42 var topology = cut(extract({
43 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
44 acba: {type: "Polygon", coordinates: [[[0, 0], [2, 0], [1, 0], [0, 0]]]}
45 }));
46 test.deepEqual(topology.objects, {
47 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
48 acba: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
49 });
50 test.end();
51 });
52
53 tape("cut rotated duplicate rings BCAB & ABCA have no cuts", function(test) {
54 var topology = cut(extract({
55 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
56 bcab: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [0, 0], [1, 0]]]}
57 }));
58 test.deepEqual(topology.objects, {
59 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
60 bcab: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
61 });
62 test.end();
63 });
64
65 tape("cut ring ABCA & line ABCA have no cuts", function(test) {
66 var topology = cut(extract({
67 abcaLine: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [0, 0]]},
68 abcaPolygon: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
69 }));
70 test.deepEqual(topology.objects, {
71 abcaLine: {type: "LineString", arcs: {0: 0, 1: 3}},
72 abcaPolygon: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
73 });
74 test.end();
75 });
76
77 tape("cut ring BCAB & line ABCA have no cuts", function(test) {
78 var topology = cut(extract({
79 abcaLine: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [0, 0]]},
80 bcabPolygon: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [0, 0], [1, 0]]]},
81 }));
82 test.deepEqual(topology.objects, {
83 abcaLine: {type: "LineString", arcs: {0: 0, 1: 3}},
84 bcabPolygon: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
85 });
86 test.deepEqual(topology.coordinates.slice(4, 8), [[0, 0], [1, 0], [2, 0], [0, 0]]);
87 test.end();
88 });
89
90 tape("cut ring ABCA & line BCAB have no cuts", function(test) {
91 var topology = cut(extract({
92 bcabLine: {type: "LineString", coordinates: [[1, 0], [2, 0], [0, 0], [1, 0]]},
93 abcaPolygon: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [2, 0], [0, 0]]]},
94 }));
95 test.deepEqual(topology.objects, {
96 bcabLine: {type: "LineString", arcs: {0: 0, 1: 3}},
97 abcaPolygon: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
98 });
99 test.end();
100 });
101
102 tape("cut when an old arc ABC extends a new arc AB, ABC is cut into AB-BC", function(test) {
103 var topology = cut(extract({
104 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
105 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]}
106 }));
107 test.deepEqual(topology.objects, {
108 abc: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
109 ab: {type: "LineString", arcs: {0: 3, 1: 4}}
110 });
111 test.end();
112 });
113
114 tape("cut when a reversed old arc CBA extends a new arc AB, CBA is cut into CB-BA", function(test) {
115 var topology = cut(extract({
116 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
117 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]}
118 }));
119 test.deepEqual(topology.objects, {
120 cba: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
121 ab: {type: "LineString", arcs: {0: 3, 1: 4}}
122 });
123 test.end();
124 });
125
126 tape("cut when a new arc ADE shares its start with an old arc ABC, there are no cuts", function(test) {
127 var topology = cut(extract({
128 ade: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
129 abc: {type: "LineString", coordinates: [[0, 0], [1, 1], [2, 1]]}
130 }));
131 test.deepEqual(topology.objects, {
132 ade: {type: "LineString", arcs: {0: 0, 1: 2}},
133 abc: {type: "LineString", arcs: {0: 3, 1: 5}}
134 });
135 test.end();
136 });
137
138 tape("cut ring ABA has no cuts", function(test) {
139 var topology = cut(extract({
140 aba: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 0]]]},
141 }));
142 test.deepEqual(topology.objects, {
143 aba: {type: "Polygon", arcs: [{0: 0, 1: 2}]}
144 });
145 test.end();
146 });
147
148 tape("cut ring AA has no cuts", function(test) {
149 var topology = cut(extract({
150 aa: {type: "Polygon", coordinates: [[[0, 0], [0, 0]]]},
151 }));
152 test.deepEqual(topology.objects, {
153 aa: {type: "Polygon", arcs: [{0: 0, 1: 1}]}
154 });
155 test.end();
156 });
157
158 tape("cut degenerate ring A has no cuts", function(test) {
159 var topology = cut(extract({
160 a: {type: "Polygon", coordinates: [[[0, 0]]]},
161 }));
162 test.deepEqual(topology.objects, {
163 a: {type: "Polygon", arcs: [{0: 0, 1: 0}]}
164 });
165 test.end();
166 });
167
168 tape("cut when a new line DEC shares its end with an old line ABC, there are no cuts", function(test) {
169 var topology = cut(extract({
170 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
171 dec: {type: "LineString", coordinates: [[0, 1], [1, 1], [2, 0]]}
172 }));
173 test.deepEqual(topology.objects, {
174 abc: {type: "LineString", arcs: {0: 0, 1: 2}},
175 dec: {type: "LineString", arcs: {0: 3, 1: 5}}
176 });
177 test.end();
178 });
179
180 tape("cut when a new line ABC extends an old line AB, ABC is cut into AB-BC", function(test) {
181 var topology = cut(extract({
182 ab: {type: "LineString", coordinates: [[0, 0], [1, 0]]},
183 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
184 }));
185 test.deepEqual(topology.objects, {
186 ab: {type: "LineString", arcs: {0: 0, 1: 1}},
187 abc: {type: "LineString", arcs: {0: 2, 1: 3, next: {0: 3, 1: 4}}}
188 });
189 test.end();
190 });
191
192 tape("cut when a new line ABC extends a reversed old line BA, ABC is cut into AB-BC", function(test) {
193 var topology = cut(extract({
194 ba: {type: "LineString", coordinates: [[1, 0], [0, 0]]},
195 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]}
196 }));
197 test.deepEqual(topology.objects, {
198 ba: {type: "LineString", arcs: {0: 0, 1: 1}},
199 abc: {type: "LineString", arcs: {0: 2, 1: 3, next: {0: 3, 1: 4}}}
200 });
201 test.end();
202 });
203
204 tape("cut when a new line starts BC in the middle of an old line ABC, ABC is cut into AB-BC", function(test) {
205 var topology = cut(extract({
206 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
207 bc: {type: "LineString", coordinates: [[1, 0], [2, 0]]}
208 }));
209 test.deepEqual(topology.objects, {
210 abc: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
211 bc: {type: "LineString", arcs: {0: 3, 1: 4}}
212 });
213 test.end();
214 });
215
216 tape("cut when a new line BC starts in the middle of a reversed old line CBA, CBA is cut into CB-BA", function(test) {
217 var topology = cut(extract({
218 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
219 bc: {type: "LineString", coordinates: [[1, 0], [2, 0]]}
220 }));
221 test.deepEqual(topology.objects, {
222 cba: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
223 bc: {type: "LineString", arcs: {0: 3, 1: 4}}
224 });
225 test.end();
226 });
227
228 tape("cut when a new line ABD deviates from an old line ABC, ABD is cut into AB-BD and ABC is cut into AB-BC", function(test) {
229 var topology = cut(extract({
230 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
231 abd: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0]]}
232 }));
233 test.deepEqual(topology.objects, {
234 abc: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
235 abd: {type: "LineString", arcs: {0: 3, 1: 4, next: {0: 4, 1: 5}}}
236 });
237 test.end();
238 });
239
240 tape("cut when a new line ABD deviates from a reversed old line CBA, CBA is cut into CB-BA and ABD is cut into AB-BD", function(test) {
241 var topology = cut(extract({
242 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
243 abd: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0]]}
244 }));
245 test.deepEqual(topology.objects, {
246 cba: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
247 abd: {type: "LineString", arcs: {0: 3, 1: 4, next: {0: 4, 1: 5}}}
248 });
249 test.end();
250 });
251
252 tape("cut when a new line DBC merges into an old line ABC, DBC is cut into DB-BC and ABC is cut into AB-BC", function(test) {
253 var topology = cut(extract({
254 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
255 dbc: {type: "LineString", coordinates: [[3, 0], [1, 0], [2, 0]]}
256 }));
257 test.deepEqual(topology.objects, {
258 abc: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
259 dbc: {type: "LineString", arcs: {0: 3, 1: 4, next: {0: 4, 1: 5}}}
260 });
261 test.end();
262 });
263
264 tape("cut when a new line DBC merges into a reversed old line CBA, DBC is cut into DB-BC and CBA is cut into CB-BA", function(test) {
265 var topology = cut(extract({
266 cba: {type: "LineString", coordinates: [[2, 0], [1, 0], [0, 0]]},
267 dbc: {type: "LineString", coordinates: [[3, 0], [1, 0], [2, 0]]}
268 }));
269 test.deepEqual(topology.objects, {
270 cba: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
271 dbc: {type: "LineString", arcs: {0: 3, 1: 4, next: {0: 4, 1: 5}}}
272 });
273 test.end();
274 });
275
276 tape("cut when a new line DBE shares a single midpoint with an old line ABC, DBE is cut into DB-BE and ABC is cut into AB-BC", function(test) {
277 var topology = cut(extract({
278 abc: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0]]},
279 dbe: {type: "LineString", coordinates: [[0, 1], [1, 0], [2, 1]]}
280 }));
281 test.deepEqual(topology.objects, {
282 abc: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 2}}},
283 dbe: {type: "LineString", arcs: {0: 3, 1: 4, next: {0: 4, 1: 5}}}
284 });
285 test.end();
286 });
287
288 tape("cut when a new line ABDE skips a point with an old line ABCDE, ABDE is cut into AB-BD-DE and ABCDE is cut into AB-BCD-DE", function(test) {
289 var topology = cut(extract({
290 abcde: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]]},
291 abde: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]}
292 }));
293 test.deepEqual(topology.objects, {
294 abcde: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 3, next: {0: 3, 1: 4}}}},
295 abde: {type: "LineString", arcs: {0: 5, 1: 6, next: {0: 6, 1: 7, next: {0: 7, 1: 8}}}}
296 });
297 test.end();
298 });
299
300 tape("cut when a new line ABDE skips a point with a reversed old line EDCBA, ABDE is cut into AB-BD-DE and EDCBA is cut into ED-DCB-BA", function(test) {
301 var topology = cut(extract({
302 edcba: {type: "LineString", coordinates: [[4, 0], [3, 0], [2, 0], [1, 0], [0, 0]]},
303 abde: {type: "LineString", coordinates: [[0, 0], [1, 0], [3, 0], [4, 0]]}
304 }));
305 test.deepEqual(topology.objects, {
306 edcba: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 3, next: {0: 3, 1: 4}}}},
307 abde: {type: "LineString", arcs: {0: 5, 1: 6, next: {0: 6, 1: 7, next: {0: 7, 1: 8}}}}
308 });
309 test.end();
310 });
311
312 tape("cut when a line ABCDBE self-intersects with its middle, it is not cut", function(test) {
313 var topology = cut(extract({
314 abcdbe: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]}
315 }));
316 test.deepEqual(topology.objects, {
317 abcdbe: {type: "LineString", arcs: {0: 0, 1: 5}}
318 });
319 test.end();
320 });
321
322 tape("cut when a line ABACD self-intersects with its start, it is cut into ABA-ACD", function(test) {
323 var topology = cut(extract({
324 abacd: {type: "LineString", coordinates: [[0, 0], [1, 0], [0, 0], [3, 0], [4, 0]]}
325 }));
326 test.deepEqual(topology.objects, {
327 abacd: {type: "LineString", arcs: {0: 0, 1: 2, next: {0: 2, 1: 4}}}
328 });
329 test.end();
330 });
331
332 tape("cut when a line ABDCD self-intersects with its end, it is cut into ABD-DCD", function(test) {
333 var topology = cut(extract({
334 abdcd: {type: "LineString", coordinates: [[0, 0], [1, 0], [4, 0], [3, 0], [4, 0]]}
335 }));
336 test.deepEqual(topology.objects, {
337 abdcd: {type: "LineString", arcs: {0: 0, 1: 2, next: {0: 2, 1: 4}}}
338 });
339 test.end();
340 });
341
342 tape("cut when an old line ABCDBE self-intersects and shares a point B, ABCDBE is cut into AB-BCDB-BE and FBG is cut into FB-BG", function(test) {
343 var topology = cut(extract({
344 abcdbe: {type: "LineString", coordinates: [[0, 0], [1, 0], [2, 0], [3, 0], [1, 0], [4, 0]]},
345 fbg: {type: "LineString", coordinates: [[0, 1], [1, 0], [2, 1]]}
346 }));
347 test.deepEqual(topology.objects, {
348 abcdbe: {type: "LineString", arcs: {0: 0, 1: 1, next: {0: 1, 1: 4, next: {0: 4, 1: 5}}}},
349 fbg: {type: "LineString", arcs: {0: 6, 1: 7, next: {0: 7, 1: 8}}}
350 });
351 test.end();
352 });
353
354 tape("cut when a line ABCA is closed, there are no cuts", function(test) {
355 var topology = cut(extract({
356 abca: {type: "LineString", coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]}
357 }));
358 test.deepEqual(topology.objects, {
359 abca: {type: "LineString", arcs: {0: 0, 1: 3}}
360 });
361 test.end();
362 });
363
364 tape("cut when a ring ABCA is closed, there are no cuts", function(test) {
365 var topology = cut(extract({
366 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]}
367 }));
368 test.deepEqual(topology.objects, {
369 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]}
370 });
371 test.end();
372 });
373
374 tape("cut exact duplicate rings ABCA & ABCA have no cuts", function(test) {
375 var topology = cut(extract({
376 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
377 abca2: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]}
378 }));
379 test.deepEqual(topology.objects, {
380 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
381 abca2: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
382 });
383 test.end();
384 });
385
386 tape("cut reversed duplicate rings ABCA & ACBA have no cuts", function(test) {
387 var topology = cut(extract({
388 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
389 acba: {type: "Polygon", coordinates: [[[0, 0], [0, 1], [1, 0], [0, 0]]]}
390 }));
391 test.deepEqual(topology.objects, {
392 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
393 acba: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
394 });
395 test.end();
396 });
397
398 tape("cut coincident rings ABCA & BCAB have no cuts", function(test) {
399 var topology = cut(extract({
400 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
401 bcab: {type: "Polygon", coordinates: [[[1, 0], [0, 1], [0, 0], [1, 0]]]}
402 }));
403 test.deepEqual(topology.objects, {
404 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
405 bcab: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
406 });
407 test.end();
408 });
409
410 tape("cut coincident rings ABCA & BACB have no cuts", function(test) {
411 var topology = cut(extract({
412 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
413 bacb: {type: "Polygon", coordinates: [[[1, 0], [0, 0], [0, 1], [1, 0]]]}
414 }));
415 test.deepEqual(topology.objects, {
416 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
417 bacb: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
418 });
419 test.end();
420 });
421
422 tape("cut coincident rings ABCDA, EFAE & GHCG are cut into ABC-CDA, EFAE and GHCG", function(test) {
423 var topology = cut(extract({
424 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]},
425 efae: {type: "Polygon", coordinates: [[[0, -1], [1, -1], [0, 0], [0, -1]]]},
426 ghcg: {type: "Polygon", coordinates: [[[0, 2], [1, 2], [1, 1], [0, 2]]]}
427 }));
428 test.deepEqual(topology.objects, {
429 abcda: {type: "Polygon", arcs: [{0: 0, 1: 2, next: {0: 2, 1: 4}}]},
430 efae: {type: "Polygon", arcs: [{0: 5, 1: 8}]},
431 ghcg: {type: "Polygon", arcs: [{0: 9, 1: 12}]}
432 });
433 test.end();
434 });
435
436 tape("cut coincident rings ABCA & DBED have no cuts, but are rotated to share B", function(test) {
437 var topology = cut(extract({
438 abca: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [0, 1], [0, 0]]]},
439 dbed: {type: "Polygon", coordinates: [[[2, 1], [1, 0], [2, 2], [2, 1]]]}
440 }));
441 test.deepEqual(topology.objects, {
442 abca: {type: "Polygon", arcs: [{0: 0, 1: 3}]},
443 dbed: {type: "Polygon", arcs: [{0: 4, 1: 7}]}
444 });
445 test.deepEqual(topology.coordinates.slice(0, 4), [[1, 0], [0, 1], [0, 0], [1, 0]]);
446 test.deepEqual(topology.coordinates.slice(4, 8), [[1, 0], [2, 2], [2, 1], [1, 0]]);
447 test.end();
448 });
449
450 tape("cut overlapping rings ABCDA and BEFCB are cut into BC-CDAB and BEFC-CB", function(test) {
451 var topology = cut(extract({
452 abcda: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}, // rotated to BCDAB, cut BC-CDAB
453 befcb: {type: "Polygon", coordinates: [[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]},
454 }));
455 test.deepEqual(topology.objects, {
456 abcda: {type: "Polygon", arcs: [{0: 0, 1: 1, next: {0: 1, 1: 4}}]},
457 befcb: {type: "Polygon", arcs: [{0: 5, 1: 8, next: {0: 8, 1: 9}}]}
458 });
459 test.end();
460 });