Debian Codegraph @

topojson/test/feature-test.js

raw
1 var tape = require("tape"),
2 topojson = require("../");
3
4 tape("feature the geometry type is preserved", function(test) {
5 var t = simpleTopology({type: "Polygon", arcs: [[0]]});
6 test.equal(topojson.feature(t, t.objects.foo).geometry.type, "Polygon");
7 test.end();
8 });
9
10 tape("feature Point is a valid geometry type", function(test) {
11 var t = simpleTopology({type: "Point", coordinates: [0, 0]});
12 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "Point", coordinates: [0, 0]}});
13 test.end();
14 });
15
16 tape("feature MultiPoint is a valid geometry type", function(test) {
17 var t = simpleTopology({type: "MultiPoint", coordinates: [[0, 0]]});
18 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "MultiPoint", coordinates: [[0, 0]]}});
19 test.end();
20 });
21
22 tape("feature LineString is a valid geometry type", function(test) {
23 var t = simpleTopology({type: "LineString", arcs: [0]});
24 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "LineString", coordinates: [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]}});
25 test.end();
26 });
27
28 tape("feature MultiLineString is a valid geometry type", function(test) {
29 var t = simpleTopology({type: "LineString", arcs: [0]});
30 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "LineString", coordinates: [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]}});
31 test.end();
32 });
33
34 tape("feature line-strings have at least two coordinates", function(test) {
35 var t = simpleTopology({type: "LineString", arcs: [3]});
36 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "LineString", coordinates: [[1, 1], [1, 1]]}});
37 var t = simpleTopology({type: "MultiLineString", arcs: [[3], [4]]});
38 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "MultiLineString", coordinates: [[[1, 1], [1, 1]], [[0, 0], [0, 0]]]}});
39 test.end();
40 });
41
42 tape("feature Polygon is a valid feature type", function(test) {
43 var t = simpleTopology({type: "Polygon", arcs: [[0]]});
44 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "Polygon", coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}});
45 test.end();
46 });
47
48 tape("feature MultiPolygon is a valid feature type", function(test) {
49 var t = simpleTopology({type: "MultiPolygon", arcs: [[[0]]]});
50 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "Feature", properties: {}, geometry: {type: "MultiPolygon", coordinates: [[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]]}});
51 test.end();
52 });
53
54 tape("feature polygons are closed, with at least four coordinates", function(test) {
55 var topology = {
56 type: "Topology",
57 transform: {scale: [1, 1], translate: [0, 0]},
58 objects: {foo: {type: "Polygon", arcs: [[0]]}, bar: {type: "Polygon", arcs: [[0, 1]]}},
59 arcs: [[[0, 0], [1, 1]], [[1, 1], [-1, -1]]]
60 };
61 test.deepEqual(topojson.feature(topology, topology.objects.foo).geometry.coordinates, [[[0, 0], [1, 1], [0, 0], [0, 0]]]);
62 test.deepEqual(topojson.feature(topology, topology.objects.bar).geometry.coordinates, [[[0, 0], [1, 1], [0, 0], [0, 0]]]);
63 test.end();
64 });
65
66 tape("feature top-level geometry collections are mapped to feature collections", function(test) {
67 var t = simpleTopology({type: "GeometryCollection", geometries: [{type: "MultiPolygon", arcs: [[[0]]]}]});
68 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "FeatureCollection", features: [{type: "Feature", properties: {}, geometry: {type: "MultiPolygon", coordinates: [[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]]}}]});
69 test.end();
70 });
71
72 tape("feature geometry collections can be nested", function(test) {
73 var t = simpleTopology({type: "GeometryCollection", geometries: [{type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}]}]});
74 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "FeatureCollection", features: [{type: "Feature", properties: {}, geometry: {type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}]}}]});
75 test.end();
76 });
77
78 tape("feature top-level geometry collections do not have ids, but second-level geometry collections can", function(test) {
79 var t = simpleTopology({type: "GeometryCollection", id: "collection", geometries: [{type: "GeometryCollection", id: "feature", geometries: [{type: "Point", id: "geometry", coordinates: [0, 0]}]}]});
80 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "FeatureCollection", features: [{type: "Feature", id: "feature", properties: {}, geometry: {type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}]}}]});
81 test.end();
82 });
83
84 tape("feature top-level geometry collections do not have properties, but second-level geometry collections can", function(test) {
85 var t = simpleTopology({type: "GeometryCollection", properties: {collection: true}, geometries: [{type: "GeometryCollection", properties: {feature: true}, geometries: [{type: "Point", properties: {geometry: true}, coordinates: [0, 0]}]}]});
86 test.deepEqual(topojson.feature(t, t.objects.foo), {type: "FeatureCollection", features: [{type: "Feature", properties: {feature: true}, geometry: {type: "GeometryCollection", geometries: [{type: "Point", coordinates: [0, 0]}]}}]});
87 test.end();
88 });
89
90 tape("feature the object id is promoted to feature id", function(test) {
91 var t = simpleTopology({id: "foo", type: "Polygon", arcs: [[0]]});
92 test.equal(topojson.feature(t, t.objects.foo).id, "foo");
93 test.end();
94 });
95
96 tape("feature any object properties are promoted to feature properties", function(test) {
97 var t = simpleTopology({type: "Polygon", properties: {color: "orange", size: 42}, arcs: [[0]]});
98 test.deepEqual(topojson.feature(t, t.objects.foo).properties, {color: "orange", size: 42});
99 test.end();
100 });
101
102 tape("feature the object id is optional", function(test) {
103 var t = simpleTopology({type: "Polygon", arcs: [[0]]});
104 test.equal(topojson.feature(t, t.objects.foo).id, undefined);
105 test.end();
106 });
107
108 tape("feature object properties are created if missing", function(test) {
109 var t = simpleTopology({type: "Polygon", arcs: [[0]]});
110 test.deepEqual(topojson.feature(t, t.objects.foo).properties, {});
111 test.end();
112 });
113
114 tape("feature arcs are converted to coordinates", function(test) {
115 var t = simpleTopology({type: "Polygon", arcs: [[0]]});
116 test.deepEqual(topojson.feature(t, t.objects.foo).geometry.coordinates, [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
117 test.end();
118 });
119
120 tape("feature negative arc indexes indicate reversed coordinates", function(test) {
121 var t = simpleTopology({type: "Polygon", arcs: [[~0]]});
122 test.deepEqual(topojson.feature(t, t.objects.foo).geometry.coordinates, [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]);
123 test.end();
124 });
125
126 tape("feature when multiple arc indexes are specified, coordinates are stitched together", function(test) {
127 var t = simpleTopology({type: "LineString", arcs: [1, 2]});
128 test.deepEqual(topojson.feature(t, t.objects.foo).geometry.coordinates, [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
129 var t = simpleTopology({type: "Polygon", arcs: [[~2, ~1]]});
130 test.deepEqual(topojson.feature(t, t.objects.foo).geometry.coordinates, [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]);
131 test.end();
132 });
133
134 tape("feature unknown geometry types are converted to null geometries", function(test) {
135 var topology = {
136 type: "Topology",
137 transform: {scale: [1, 1], translate: [0, 0]},
138 objects: {
139 foo: {id: "foo"},
140 bar: {type: "Invalid", properties: {bar: 2}},
141 baz: {type: "GeometryCollection", geometries: [{type: "Unknown", id: "unknown"}]}
142 },
143 arcs: []
144 };
145 test.deepEqual(topojson.feature(topology, topology.objects.foo), {type: "Feature", id: "foo", properties: {}, geometry: null});
146 test.deepEqual(topojson.feature(topology, topology.objects.bar), {type: "Feature", properties: {bar: 2}, geometry: null});
147 test.deepEqual(topojson.feature(topology, topology.objects.baz), {type: "FeatureCollection", features: [{type: "Feature", id: "unknown", properties: {}, geometry: null}]});
148 test.end();
149 });
150
151 function simpleTopology(object) {
152 return {
153 type: "Topology",
154 transform: {scale: [1, 1], translate: [0, 0]},
155 objects: {foo: object},
156 arcs: [
157 [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]],
158 [[0, 0], [1, 0], [0, 1]],
159 [[1, 1], [-1, 0], [0, -1]],
160 [[1, 1]],
161 [[0, 0]]
162 ]
163 };
164 }