-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoly_test.go
159 lines (144 loc) · 3.22 KB
/
poly_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"testing"
"github.com/pmezard/gogeos/geos"
)
func makeGeosPolygons(rings []*Linestring) []*geos.Geometry {
geoms := []*geos.Geometry{}
for _, r := range rings {
g, err := createGeosSimplePolygon(r)
if err != nil {
panic(err)
}
geoms = append(geoms, g)
}
return geoms
}
func makeTestRing(points []Point) *Linestring {
scaled := []Point{}
for _, p := range points {
p.Lon *= 10000
p.Lat *= 10000
scaled = append(scaled, p)
}
r := &Linestring{
Points: scaled,
}
if r.Start() != r.End() {
r.Points = append(r.Points, r.Start())
}
return r
}
func printNode(n *inclusionNode, w io.Writer, prefix string) {
fmt.Fprintf(w, "%s%d: [", prefix, n.Id)
for _, c := range n.Children {
fmt.Fprintf(w, "%d ", c.Id)
}
fmt.Fprintf(w, "]\n")
for _, c := range n.Children {
printNode(c, w, prefix+" ")
}
}
func printTrees(t *testing.T, rings []*Linestring) string {
geoms := makeGeosPolygons(rings)
nodes, err := makeInclusionTrees(geoms)
if err != nil {
t.Fatal(err)
}
buf := &bytes.Buffer{}
for _, n := range nodes {
printNode(n, buf, "")
}
return buf.String()
}
func stripLines(s string) string {
lines := []string{}
scanner := bufio.NewScanner(bytes.NewBuffer([]byte(s)))
for scanner.Scan() {
l := strings.TrimSpace(scanner.Text())
if l != "" {
lines = append(lines, strings.TrimSpace(scanner.Text()))
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return strings.Join(lines, "\n")
}
func checkTrees(t *testing.T, rings []*Linestring, expected string) {
s := printTrees(t, rings)
expected = stripLines(expected)
s = stripLines(s)
if expected != s {
t.Fatal(s)
}
}
func TestMakeContainmentTrees(t *testing.T) {
// Single polygon
checkTrees(t, []*Linestring{
makeTestRing([]Point{{0, 0}, {0, 1}, {1, 1}, {1, 0}}),
}, `
0: []
`)
// Simple full inclusion
checkTrees(t, []*Linestring{
makeTestRing([]Point{{0, 0}, {0, 3}, {3, 3}, {3, 0}}),
makeTestRing([]Point{{1, 1}, {1, 2}, {2, 2}, {2, 1}}),
}, `
0: [1 ]
1: []
`)
// Disjoint shapes
checkTrees(t, []*Linestring{
makeTestRing([]Point{{0, 0}, {0, 3}, {3, 3}, {3, 0}}),
makeTestRing([]Point{{4, 4}, {4, 5}, {5, 5}, {5, 4}}),
}, `
0: []
1: []
`)
// Island
checkTrees(t, []*Linestring{
makeTestRing([]Point{{0, 0}, {0, 5}, {5, 5}, {5, 0}}),
makeTestRing([]Point{{1, 1}, {1, 4}, {4, 4}, {4, 1}}),
makeTestRing([]Point{{2, 2}, {2, 3}, {3, 3}, {3, 2}}),
}, `
0: [1 ]
1: [2 ]
2: []
`)
// One hole plus one island
checkTrees(t, []*Linestring{
makeTestRing([]Point{{0, 0}, {0, 5}, {7, 5}, {7, 0}}),
makeTestRing([]Point{{1, 1}, {1, 4}, {4, 4}, {4, 1}}),
makeTestRing([]Point{{2, 2}, {2, 3}, {3, 3}, {3, 2}}),
makeTestRing([]Point{{5, 2}, {5, 3}, {6, 3}, {6, 2}}),
}, `
0: [1 3 ]
1: [2 ]
2: []
3: []
`)
// Equal shapes with a parent.
checkTrees(t, []*Linestring{
makeTestRing([]Point{{1, 1}, {1, 2}, {2, 2}, {2, 1}}),
makeTestRing([]Point{{1, 1}, {1, 2}, {2, 2}, {2, 1}}),
makeTestRing([]Point{{0, 0}, {0, 3}, {3, 3}, {3, 0}}),
}, `
2: [0 1 ]
0: []
1: []
`)
// Equal shapes without parent.
checkTrees(t, []*Linestring{
makeTestRing([]Point{{1, 1}, {1, 2}, {2, 2}, {2, 1}}),
makeTestRing([]Point{{1, 1}, {1, 2}, {2, 2}, {2, 1}}),
}, `
0: []
1: []
`)
}