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
/
Copy pathmisc_test.go
167 lines (151 loc) · 6.71 KB
/
misc_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
package turfgo
import (
"fmt"
"io/ioutil"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestPointOnLine(t *testing.T) {
pointOutsideLine := &Point{38.884017, -77.037076}
point1 := &Point{38.878605, -77.031669}
point2 := &Point{38.881946, -77.029609}
point3 := &Point{38.884084, -77.020339}
point4 := &Point{38.885821, -77.025661}
point5 := &Point{38.889563, -77.021884}
point6 := &Point{38.892368, -77.019824}
lineString := NewLineString([]*Point{point1, point2, point3, point4, point5, point6})
Convey("Given a wrong unit, should throw error", t, func() {
_, _, _, err := PointOnLine(pointOutsideLine, lineString, "invalidUnit")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, fmt.Sprintf(unitError, "invalidUnit"))
})
Convey("Given a point and a lineString, should calculate a point on line", t, func() {
expected := &Point{38.881361463229524, -77.02996941477018}
exptectedDistance := 0.4241146325840119
result, distance, index, _ := PointOnLine(pointOutsideLine, lineString, "mi")
So(result, ShouldResemble, expected)
So(distance, ShouldEqual, exptectedDistance)
So(index, ShouldEqual, 0)
})
Convey("Other tests copied from turfjs", t, func() {
gj1, _ := ioutil.ReadFile("./testFiles/pointOnLine/line1.geojson")
ls1, _ := DecodeLineStringFromFeatureJSON(gj1)
p1 := &Point{22.254624939561698, -97.79617309570312}
expected1 := &Point{22.247393614241208, -97.83572934173806}
exptectedDistance1 := 2.5792333253307405
result1, distance1, index1, _ := PointOnLine(p1, ls1, "mi")
So(result1, ShouldResemble, expected1)
So(distance1, ShouldAlmostEqual, exptectedDistance1)
So(index1, ShouldEqual, 0)
gj2, _ := ioutil.ReadFile("./testFiles/pointOnLine/route1.geojson")
ls2, _ := DecodeLineStringFromFeatureJSON(gj2)
p2 := &Point{37.60117623656667, -79.0850830078125}
expected2 := &Point{37.578608, -79.049412}
exptectedDistance2 := 2.4998919202861694
result2, distance2, index2, _ := PointOnLine(p2, ls2, "mi")
So(result2, ShouldResemble, expected2)
So(distance2, ShouldAlmostEqual, exptectedDistance2)
So(index2, ShouldEqual, 1449)
gj3, _ := ioutil.ReadFile("./testFiles/pointOnLine/route2.geojson")
ls3, _ := DecodeLineStringFromFeatureJSON(gj3)
p3 := &Point{45.96021963947196, -112.60660171508789}
expected3 := &Point{45.970203, -112.614288}
exptectedDistance3 := 0.7825944108810942
result3, distance3, index3, _ := PointOnLine(p3, ls3, "mi")
So(result3, ShouldResemble, expected3)
So(distance3, ShouldAlmostEqual, exptectedDistance3)
So(index3, ShouldEqual, 3759)
})
}
func TestIsTriangleObtuse(t *testing.T) {
Convey("Given three sides of triangle", t, func() {
Convey("Should return true if triangle is obtuse", func() {
result := isAnyBaseAngleObtuse(NewPoint(0, 0), NewPoint(3, 0), NewPoint(5, 3))
So(result, ShouldBeTrue)
})
Convey("Should return false if triangle is not obtuse triangle", func() {
result := isAnyBaseAngleObtuse(NewPoint(0, 0), NewPoint(3, 0), NewPoint(2, 3))
So(result, ShouldBeFalse)
})
})
}
func TestTriangularProjection(t *testing.T) {
Convey("Should find the projection without bearing if previous point is nil and current point fall in middle", t, func() {
a := NewPoint(40.88969576429507, -74.02225255966187)
b := NewPoint(40.890660929502566, -74.02167320251465)
c := NewPoint(40.891504423701505, -74.02114748954773)
p := NewPoint(40.89128544066409, -74.02228474617003)
expectedProj := NewPoint(40.89099374267134, -74.02146577465254)
lineString := NewLineString([]*Point{a, b, c})
proj, dist, index, err := TriangularProjection(p, nil, lineString, "mi")
So(err, ShouldBeNil)
So(proj, ShouldResemble, expectedProj)
So(dist, ShouldAlmostEqual, 0.047301111679236334)
So(index, ShouldEqual, 1)
})
Convey("Should pass if previous point is given and bearing is within 45 degree of either side", t, func() {
a := NewPoint(40.88969576429507, -74.02225255966187)
b := NewPoint(40.890660929502566, -74.02167320251465)
c := NewPoint(40.891504423701505, -74.02114748954773)
p := NewPoint(40.89128544066409, -74.02228474617003)
p1 := NewPoint(40.89057171314116, -74.02269244194031)
expectedProj := NewPoint(40.89099374267134, -74.02146577465254)
lineString := NewLineString([]*Point{a, b, c})
proj, dist, index, err := TriangularProjection(p, p1, lineString, "mi")
So(err, ShouldBeNil)
So(proj, ShouldResemble, expectedProj)
So(dist, ShouldAlmostEqual, 0.047301111679236334)
So(index, ShouldEqual, 1)
})
Convey("Should pass if previous point is given and bearing is outside 45 degree of either side", t, func() {
a := NewPoint(40.88969576429507, -74.02225255966187)
b := NewPoint(40.890660929502566, -74.02167320251465)
c := NewPoint(40.891504423701505, -74.02114748954773)
p := NewPoint(40.89128544066409, -74.02228474617003)
p1 := NewPoint(40.891155672596184, -74.02328252792358)
lineString := NewLineString([]*Point{a, b, c})
proj, dist, index, err := TriangularProjection(p, p1, lineString, "mi")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "No Projection found")
So(proj, ShouldBeNil)
So(dist, ShouldEqual, -1)
So(index, ShouldEqual, -1)
})
Convey("Should fail if no projection on line", t, func() {
a := NewPoint(40.88969576429507, -74.02225255966187)
b := NewPoint(40.890660929502566, -74.02167320251465)
c := NewPoint(40.891504423701505, -74.02114748954773)
p := NewPoint(40.892404679685235, -74.02269244194031)
lineString := NewLineString([]*Point{a, b, c})
proj, dist, index, err := TriangularProjection(p, nil, lineString, "mi")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "No Projection found")
So(proj, ShouldBeNil)
So(dist, ShouldEqual, -1)
So(index, ShouldEqual, -1)
})
Convey("Should fail if not enough points on linestring", t, func() {
a := NewPoint(40.88969576429507, -74.02225255966187)
p := NewPoint(40.892404679685235, -74.02269244194031)
lineString := NewLineString([]*Point{a})
proj, dist, index, err := TriangularProjection(p, nil, lineString, "mi")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "No Projection found")
So(proj, ShouldBeNil)
So(dist, ShouldEqual, -1)
So(index, ShouldEqual, -1)
})
Convey("Should pass if previous point is given and bearings are near 180", t, func() {
a := NewPoint(12.992734807875454, 80.252972)
b := NewPoint(12.99245733348597, 80.25298118591307)
p := NewPoint(12.992617473880916, 80.25296505845765)
p1 := NewPoint(12.992492785541987, 80.25295291147245)
expectedProj := NewPoint(12.992493636372885, 80.25297998408985)
lineString := NewLineString([]*Point{a, b})
proj, dist, index, err := TriangularProjection(p1, p, lineString, "mi")
So(err, ShouldBeNil)
So(proj, ShouldResemble, expectedProj)
So(dist, ShouldAlmostEqual, 0.001824171398145269)
So(index, ShouldEqual, 0)
})
}