forked from rubenv/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_test.go
141 lines (116 loc) · 3.25 KB
/
extract_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package topojson
import (
"reflect"
"testing"
"github.com/cheekybits/is"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
)
// See https://github.com/mbostock/topojson/blob/master/test/topology/extract-test.js
// extract copies coordinates sequentially into a buffer
func TestCopiesCoordinates(t *testing.T) {
is := is.New(t)
in := []*geojson.Feature{
NewTestFeature("foo", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
NewTestFeature("bar", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
}
expected := [][]float64{
{0, 0}, {1, 0}, {2, 0},
{0, 0}, {1, 0}, {2, 0},
}
topo := &Topology{input: in}
topo.extract()
is.Equal(len(topo.coordinates), len(expected))
for k, v := range topo.coordinates {
is.Equal(v, expected[k])
}
}
// extract includes closing coordinates in polygons
func TestClosingCoordinates(t *testing.T) {
is := is.New(t)
in := []*geojson.Feature{
NewTestFeature("foo", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0}, orb.Point{0, 0},
}),
}
expected := [][]float64{
{0, 0}, {1, 0}, {2, 0}, {0, 0},
}
topo := &Topology{input: in}
topo.extract()
is.Equal(len(topo.coordinates), len(expected))
for k, v := range topo.coordinates {
is.Equal(v, expected[k])
}
}
// extract represents lines as contiguous slices of the coordinate buffer
func TestLineSlices(t *testing.T) {
is := is.New(t)
in := []*geojson.Feature{
NewTestFeature("foo", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
NewTestFeature("bar", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
}
topo := &Topology{input: in}
topo.extract()
foo := GetFeature(topo, "foo")
is.Equal(foo.Type, geojson.TypeLineString)
is.True(reflect.DeepEqual(foo.Arc, &arc{Start: 0, End: 2}))
bar := GetFeature(topo, "bar")
is.Equal(bar.Type, geojson.TypeLineString)
is.True(reflect.DeepEqual(bar.Arc, &arc{Start: 3, End: 5}))
}
// extract exposes the constructed lines and rings in the order of construction
func TestExtractRingsOrder(t *testing.T) {
is := is.New(t)
in := []*geojson.Feature{
NewTestFeature("line", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
NewTestFeature("multiline", orb.MultiLineString{
orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
},
}),
NewTestFeature("polygon", orb.Polygon{
orb.Ring{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0}, orb.Point{0, 0},
},
}),
}
topo := &Topology{input: in}
topo.extract()
is.True(reflect.DeepEqual(topo.lines, []*arc{
{Start: 0, End: 2},
{Start: 3, End: 5},
}))
is.True(reflect.DeepEqual(topo.rings, []*arc{
{Start: 6, End: 9},
}))
}
// extract supports nested geometry collections
func TestExtractNested(t *testing.T) {
is := is.New(t)
in := []*geojson.Feature{
NewTestFeature("foo", orb.Collection{
orb.LineString{
orb.Point{0, 0}, orb.Point{0, 1},
},
}),
}
topo := &Topology{input: in}
topo.extract()
foo := GetFeature(topo, "foo")
is.Equal(foo.Type, "GeometryCollection")
geometries := foo.Geometries
is.Equal(len(geometries), 1)
is.Equal(geometries[0].Type, geojson.TypeLineString)
is.True(reflect.DeepEqual(geometries[0].Arc, &arc{Start: 0, End: 1}))
}