Debian Codegraph @

topojson/test/pre-quantize-test.js

raw
1 var tape = require("tape"),
2 quantize = require("../lib/topojson/pre-quantize");
3
4 tape("pre-quantize returns the quantization transform", function(test) {
5 test.deepEqual(quantize({}, [0, 0, 1, 1], 1e4), {
6 scale: [1 / 9999, 1 / 9999],
7 translate: [0, 0]
8 });
9 test.end();
10 });
11
12 tape("pre-quantize converts coordinates to fixed precision", function(test) {
13 var objects = {
14 foo: {
15 type: "LineString",
16 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
17 }
18 };
19 quantize(objects, [0, 0, 1, 1], 1e4);
20 test.deepEqual(objects.foo.coordinates, [[0, 0], [9999, 0], [0, 9999], [0, 0]]);
21 test.end();
22 });
23
24 tape("pre-quantize observes the quantization parameter", function(test) {
25 var objects = {
26 foo: {
27 type: "LineString",
28 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
29 }
30 };
31 quantize(objects, [0, 0, 1, 1], 10);
32 test.deepEqual(objects.foo.coordinates, [[0, 0], [9, 0], [0, 9], [0, 0]]);
33 test.end();
34 });
35
36 tape("pre-quantize observes the bounding box", function(test) {
37 var objects = {
38 foo: {
39 type: "LineString",
40 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
41 }
42 };
43 quantize(objects, [-1, -1, 2, 2], 10);
44 test.deepEqual(objects.foo.coordinates, [[3, 3], [6, 3], [3, 6], [3, 3]]);
45 test.end();
46 });
47
48 tape("pre-quantize applies to points as well as arcs", function(test) {
49 var objects = {
50 foo: {
51 type: "MultiPoint",
52 coordinates: [[0, 0], [1, 0], [0, 1], [0, 0]]
53 }
54 };
55 quantize(objects, [0, 0, 1, 1], 1e4);
56 test.deepEqual(objects.foo.coordinates, [[0, 0], [9999, 0], [0, 9999], [0, 0]]);
57 test.end();
58 });
59
60 tape("pre-quantize skips coincident points in lines", function(test) {
61 var objects = {
62 foo: {
63 type: "LineString",
64 coordinates: [[0, 0], [0.9, 0.9], [1.1, 1.1], [2, 2]]
65 }
66 };
67 quantize(objects, [0, 0, 2, 2], 3);
68 test.deepEqual(objects.foo.coordinates, [[0, 0], [1, 1], [2, 2]]);
69 test.end();
70 });
71
72 tape("pre-quantize skips coincident points in polygons", function(test) {
73 var objects = {
74 foo: {
75 type: "Polygon",
76 coordinates: [[[0, 0], [0.9, 0.9], [1.1, 1.1], [2, 2], [0, 0]]]
77 }
78 };
79 quantize(objects, [0, 0, 2, 2], 3);
80 test.deepEqual(objects.foo.coordinates, [[[0, 0], [1, 1], [2, 2], [0, 0]]]);
81 test.end();
82 });
83
84 tape("pre-quantize does not skip coincident points in points", function(test) {
85 var objects = {
86 foo: {
87 type: "MultiPoint",
88 coordinates: [[0, 0], [0.9, 0.9], [1.1, 1.1], [2, 2], [0, 0]]
89 }
90 };
91 quantize(objects, [0, 0, 2, 2], 3);
92 test.deepEqual(objects.foo.coordinates, [[0, 0], [1, 1], [1, 1], [2, 2], [0, 0]]);
93 test.end();
94 });
95
96 tape("pre-quantize includes closing point in degenerate lines", function(test) {
97 var objects = {
98 foo: {
99 type: "LineString",
100 coordinates: [[1, 1], [1, 1], [1, 1]]
101 }
102 };
103 quantize(objects, [0, 0, 2, 2], 3);
104 test.deepEqual(objects.foo.coordinates, [[1, 1], [1, 1]]);
105 test.end();
106 });
107
108 tape("pre-quantize includes closing point in degenerate polygons", function(test) {
109 var objects = {
110 foo: {
111 type: "Polygon",
112 coordinates: [[[0.9, 1], [1.1, 1], [1.01, 1], [0.9, 1]]]
113 }
114 };
115 quantize(objects, [0, 0, 2, 2], 3);
116 test.deepEqual(objects.foo.coordinates, [[[1, 1], [1, 1], [1, 1], [1, 1]]]);
117 test.end();
118 });