This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation_test.go
309 lines (277 loc) · 13.4 KB
/
transformation_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package turfgo
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLineDiff(t *testing.T) {
Convey("Given empty first line, should return empty array", t, func() {
points1 := []*Point{}
points2 := []*Point{&Point{1, 0}, &Point{1, 2}, &Point{2, 2}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 0)
})
Convey("Given empty second line, should return first line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, lineString1)
})
// 0 0 0 0
Convey("Given non intersecting line segments, should give full line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{1, 0}, &Point{1, 2}, &Point{2, 2}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, lineString1)
})
// X X X X
Convey("Given full intersection, should give no line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 0)
})
// X 0 0 X
Convey("Given line have common start and end point, should give full line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 0}, &Point{1, 2}, &Point{2, 2}, &Point{4, 5}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, lineString1)
})
// 0 X 0 0
Convey("Given line cross each other only once, should give full line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 2}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, lineString1)
})
// 0 X 0 0 X 0
Convey("Given line cross each other multiple times but not overlap, should give full line", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{4, 4}, &Point{4, 9}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 2}, &Point{4, 4}, &Point{8, 2}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, lineString1)
})
// 0 X X 0
Convey("Given one overlap, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diff1 := NewLineString([]*Point{&Point{0, 0}, &Point{1, 1}})
diff2 := NewLineString([]*Point{&Point{2, 3}, &Point{4, 5}})
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 2)
So(diffs[0], ShouldResemble, diff1)
So(diffs[1], ShouldResemble, diff2)
})
// 0 X X X
Convey("Given one overlap which proceed till end, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diff1 := NewLineString([]*Point{&Point{0, 0}, &Point{1, 1}})
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, diff1)
})
// X X 0 0
Convey("Given one overlap which start from beginning, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 4}, &Point{4, 6}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diff1 := NewLineString([]*Point{&Point{1, 1}, &Point{2, 3}, &Point{4, 5}})
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 1)
So(diffs[0], ShouldResemble, diff1)
})
// 0 X X 0 0 X X X 0 0 0 0 0
Convey("Given multiple overlap, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}, &Point{12, 7}, &Point{21, 7}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 4}, &Point{6, 7}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{13, 6}, &Point{12, 7}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diff1 := NewLineString([]*Point{&Point{0, 0}, &Point{1, 1}})
diff2 := NewLineString([]*Point{&Point{2, 3}, &Point{4, 5}, &Point{9, 5}, &Point{8, 5}})
diff3 := NewLineString([]*Point{&Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}, &Point{12, 7}, &Point{21, 7}})
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 3)
So(diffs[0], ShouldResemble, diff1)
So(diffs[1], ShouldResemble, diff2)
So(diffs[2], ShouldResemble, diff3)
})
//X X 0 % X X X 0 0 0 X X
Convey("Given multiple overlap which start and end with line, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}, &Point{12, 7}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{12, 3}, &Point{4, 4}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{21, 4}, &Point{13, 6}, &Point{12, 7}, &Point{12, 21}, &Point{12, 7}, &Point{23, 7}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
diff1 := NewLineString([]*Point{&Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{9, 5}})
diff2 := NewLineString([]*Point{&Point{4, 5}, &Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}})
diffs := LineDiff(lineString1, lineString2)
So(len(diffs), ShouldEqual, 2)
So(diffs[0], ShouldResemble, diff1)
So(diffs[1], ShouldResemble, diff2)
})
}
func TestReduceDiffSegment(t *testing.T) {
ls1 := NewLineString([]*Point{&Point{0, 0}, &Point{1, 1}})
ls2 := NewLineString([]*Point{&Point{1, 1}, &Point{2, 3}})
ls3 := NewLineString([]*Point{&Point{4, 5}, &Point{1, 1}})
ls4 := NewLineString([]*Point{&Point{1, 1}, &Point{7, 8}})
ls5 := NewLineString([]*Point{&Point{9, 7}, &Point{2, 4}})
Convey("Given empty array, should return same", t, func() {
emptySeg := []*LineString{}
So(reduceDiffSegment(emptySeg), ShouldResemble, emptySeg)
})
Convey("Given one element array, should return same", t, func() {
seg := []*LineString{ls1}
So(reduceDiffSegment(seg), ShouldResemble, seg)
})
Convey("Given multiple segments ending on non continous, should stitch them", t, func() {
seg := []*LineString{ls1, ls2, ls3, ls4, ls5}
merged1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}}
merged2 := []*Point{&Point{4, 5}, &Point{1, 1}, &Point{7, 8}}
merged3 := []*Point{&Point{9, 7}, &Point{2, 4}}
exptectedResult := []*LineString{NewLineString(merged1), NewLineString(merged2), NewLineString(merged3)}
So(reduceDiffSegment(seg), ShouldResemble, exptectedResult)
})
Convey("Given multiple segments ending on contionus, should stitch them", t, func() {
seg := []*LineString{ls1, ls2, ls3, ls4}
merged1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}}
merged2 := []*Point{&Point{4, 5}, &Point{1, 1}, &Point{7, 8}}
exptectedResult := []*LineString{NewLineString(merged1), NewLineString(merged2)}
So(reduceDiffSegment(seg), ShouldResemble, exptectedResult)
})
Convey("Given three continous segments, should stitch them", t, func() {
ls3 = NewLineString([]*Point{&Point{2, 3}, &Point{4, 5}})
seg := []*LineString{ls1, ls2, ls3}
merged := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
exptectedResult := []*LineString{NewLineString(merged)}
So(reduceDiffSegment(seg), ShouldResemble, exptectedResult)
})
}
func TestContainLocationPair(t *testing.T) {
points := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{1, 1}}
Convey("Given two points, should return true if points are present after each other", t, func() {
found := containLocationPair(points, &Point{1, 1}, &Point{2, 3})
So(found, ShouldBeTrue)
})
Convey("Given two points, should return false if points are not present after each other", t, func() {
found := containLocationPair(points, &Point{1, 1}, &Point{4, 5})
So(found, ShouldBeFalse)
})
Convey("Given two points, should return false if point is on edge", t, func() {
found := containLocationPair(points, &Point{1, 1}, &Point{0, 0})
So(found, ShouldBeFalse)
})
}
func TestLineDiffPercentage(t *testing.T) {
Convey("Given empty first line, should return 0", t, func() {
points1 := []*Point{}
points2 := []*Point{&Point{1, 0}, &Point{1, 2}, &Point{2, 2}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 0)
})
Convey("Given empty second line, should return 100 percent", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 100)
})
// X X X X
Convey("Given full intersection, should give 0 percent", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 0)
})
// 0 0 0 0
Convey("Given non intersecting line segments, should give 100 percentage", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{1, 0}, &Point{1, 2}, &Point{2, 2}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 100)
})
// 0 X X 0
Convey("Given one overlap, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 4}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 50)
})
// 0 X X X
Convey("Given one overlap which proceed till end, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 25)
})
// X X 0 0
Convey("Given one overlap which start from beginning, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 4}, &Point{4, 6}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldEqual, 50)
})
// 0 X X 0 0 X X X 0 0 0 0 0
Convey("Given multiple overlap, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}, &Point{12, 7}, &Point{21, 7}}
points2 := []*Point{&Point{0, 1}, &Point{1, 1}, &Point{2, 3}, &Point{4, 4}, &Point{6, 7}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{13, 6}, &Point{12, 7}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldAlmostEqual, float64(8)/float64(13)*100)
})
//X X 0 % X X X 0 0 0 X X
Convey("Given multiple overlap which start and end with line, should give correct result", t, func() {
points1 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{2, 3}, &Point{4, 5}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{11, 7}, &Point{9, 2}, &Point{4, 9}, &Point{12, 21}, &Point{12, 7}}
points2 := []*Point{&Point{0, 0}, &Point{1, 1}, &Point{12, 3}, &Point{4, 4}, &Point{9, 5}, &Point{8, 5},
&Point{4, 5}, &Point{21, 4}, &Point{13, 6}, &Point{12, 7}, &Point{12, 21}, &Point{12, 7}, &Point{23, 7}}
lineString1 := NewLineString(points1)
lineString2 := NewLineString(points2)
p := LineDiffPercentage(lineString1, lineString2)
So(p, ShouldAlmostEqual, float64(5)/float64(12)*100)
})
}