This repository was archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon_test.go
144 lines (125 loc) · 3.96 KB
/
polygon_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
142
143
144
package cap
import (
"encoding/xml"
"testing"
)
func TestSinglePolygonFromXML(t *testing.T) {
sourceXML := []byte("<polygon>43.5481,-65.8528 43.613,-66.0816 43.8302,-66.2464 43.9343,-66.2609 44.0034,-66.154 44.0035,-66.1537 44.1327,-65.8891 44.2147,-65.458 44.227,-65.3927 44.227,-65.3927 44.227,-65.3927 43.7465,-65.6514 43.6446,-65.6121 43.5749,-65.7862 43.5734,-65.7898 43.5481,-65.8528</polygon>")
expectedCoords := [][]float64{
{-65.8528, 43.5481},
{-66.0816, 43.613},
{-66.2464, 43.8302},
{-66.2609, 43.9343},
{-66.154, 44.0034},
{-66.1537, 44.0035},
{-65.8891, 44.1327},
{-65.458, 44.2147},
{-65.3927, 44.227},
{-65.6514, 43.7465},
{-65.6121, 43.6446},
{-65.7862, 43.5749},
{-65.7898, 43.5734},
{-65.8528, 43.5481},
}
var polygon Polygon
if err := xml.Unmarshal(sourceXML, &polygon); err != nil {
t.Fatal(err)
}
if polygon.Type != "polygon" {
t.Errorf("Unexpected polygon type, got: %s, want: %s.", polygon.Type, "polygon")
}
if len(polygon.Coordinates) != 1 {
t.Errorf("Unexpected number of polygons, got: %d, want: %d.", len(polygon.Coordinates), 1)
}
if len(polygon.Coordinates[0]) != len(expectedCoords) {
t.Fatalf("Unexpected number of coordinates, got: %d, want: %d.", len(polygon.Coordinates[0]), len(expectedCoords))
}
for i, coord := range polygon.Coordinates[0] {
if coord[0] != expectedCoords[i][0] || coord[1] != expectedCoords[i][1] {
t.Errorf("Unexpected coordinate at index %d, got: %f,%f, want: %f,%f",
i,
coord[0], coord[1],
expectedCoords[i][0], expectedCoords[i][1])
}
}
}
func TestSinglePolygonMissingFinalPointFromXML(t *testing.T) {
sourceXML := []byte("<polygon>43.5481,-65.8528 43.613,-66.0816 43.8302,-66.2464 43.9343,-66.2609 44.0034,-66.154 44.0035,-66.1537 44.1327,-65.8891 44.2147,-65.458 44.227,-65.3927 44.227,-65.3927 44.227,-65.3927 43.7465,-65.6514 43.6446,-65.6121 43.5749,-65.7862 43.5734,-65.7898</polygon>")
expectedCoords := [][]float64{
{-65.8528, 43.5481},
{-66.0816, 43.613},
{-66.2464, 43.8302},
{-66.2609, 43.9343},
{-66.154, 44.0034},
{-66.1537, 44.0035},
{-65.8891, 44.1327},
{-65.458, 44.2147},
{-65.3927, 44.227},
{-65.6514, 43.7465},
{-65.6121, 43.6446},
{-65.7862, 43.5749},
{-65.7898, 43.5734},
{-65.8528, 43.5481},
}
var polygon Polygon
if err := xml.Unmarshal(sourceXML, &polygon); err != nil {
t.Fatal(err)
}
if polygon.Type != "polygon" {
t.Errorf("Unexpected polygon type, got: %s, want: %s.", polygon.Type, "polygon")
}
if len(polygon.Coordinates) != 1 {
t.Errorf("Unexpected number of polygons, got: %d, want: %d.", len(polygon.Coordinates), 1)
}
if len(polygon.Coordinates[0]) != len(expectedCoords) {
t.Fatalf("Unexpected number of coordinates, got: %d, want: %d.", len(polygon.Coordinates[0]), len(expectedCoords))
}
for i, coord := range polygon.Coordinates[0] {
if coord[0] != expectedCoords[i][0] || coord[1] != expectedCoords[i][1] {
t.Errorf("Unexpected coordinate at index %d, got: %f,%f, want: %f,%f",
i,
coord[0], coord[1],
expectedCoords[i][0], expectedCoords[i][1])
}
}
}
func TestMultiplePoylgonsFromXML(t *testing.T) {
sourceXML := []byte(`
<area>
<polygon>1,1 2,2, 3,3 1,1</polygon>
<polygon>-1,-1 -2,-2, -3,-3 -4,-4 -1,-1</polygon>
</area>
`)
expectedCoords := [][][]float64{
{
{1, 1},
{2, 2},
{3, 3},
{1, 1},
},
{
{-1, -1},
{-2, -2},
{-3, -3},
{-4, -4},
{-1, -1},
},
}
var area struct {
Polygons Polygons `xml:"polygon"`
}
if err := xml.Unmarshal(sourceXML, &area); err != nil {
t.Fatal(err)
}
if len(area.Polygons) != 2 {
t.Errorf("Unexpected number of polyongs, got: %d, expected: %d", len(area.Polygons), 2)
}
for i, p := range area.Polygons {
if p.Type != "polygon" {
t.Errorf("Unexpected polygon type, got: %s, want: %s.", p.Type, "polygon")
}
if len(expectedCoords[i]) != len(p.Coordinates[0]) {
t.Errorf("Unexpected number of coordinates, got: %d, want: %d.", len(p.Coordinates[0]), len(expectedCoords[i]))
}
}
}