-
Notifications
You must be signed in to change notification settings - Fork 8
/
cheapruler_test.go
274 lines (220 loc) Β· 17.2 KB
/
cheapruler_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
package cheapruler
import (
"fmt"
"math"
"strconv"
"testing"
)
//points := [-96.920341,32.838261],[-96.920421,32.838295],[-96.920421,32.838295],[-96.920536,32.838297],[-96.920684,32.838293],[-96.920818,32.838342]],[[-96.920349,32.838306],[-96.920421,32.838295]],[[-96.919874,32.837479],[-96.920097,32.837684],[-96.92018,32.837844],[-96.92029,32.838216],[-96.920341,32.838261]],[[-96.915781,32.834689],[-96.915735,32.834529],[-96.91573,32.834443],[-96.915733,32.834286],[-96.915853,32.8337]],[[-96.915092,32.836889],[-96.915915,32.836977]],[[-96.914617,32.83752],[-96.914507,32.838106],[-96.914319,32.839063]],[[-96.915671,32.833443],[-96.915427,32.833817],[-96.91532,32.833963],[-96.915247,32.834072],[-96.915062,32.834252],[-96.914775,32.834518]],[[-96.9104,32.837119],[-96.910567,32.837069],[-96.9111,32.836941]],[[-96.91441,32.836582],[-96.914225,32.836544],[-96.914158,32.836499],[-96.914107,32.836449],[-96.914075,32.836393],[-96.914059,32.836292],[-96.914091,32.836073],[-96.914649,32.834919],[-96.914837,32.834601],[-96.914858,32.834583],[-96.914912,32.834563],[-96.914968,32.834561],[-96.91503,32.83457],[-96.915081,32.834592],[-96.915116,32.834628],[-96.915134,32.834671],[-96.91514,32.834718],[-96.915124,32.834793],[-96.914931,32.835336],[-96.914888,32.835509],[-96.91481,32.835911]],[[-96.914155,32.836758],[-96.913391,32.836697],[-96.912991,32.836643],[-96.912847,32.836589],[-96.912761,32.836508],[-96.912573,32.836433]],[[-96.917377,32.837281],[-96.917589,32.837317],[-96.918117,32.837339]],[[-96.920815,32.836594],[-96.919732,32.836643],[-96.918975,32.836702],[-96.918077,32.836729],[-96.916661,32.83672],[-96.916358,32.836774]],[[-96.911543,32.836925],[-96.911806,32.837006],[-96.911994,32.837028],[-96.912326,32.837114],[-96.912552,32.837202],[-96.912656,32.83729]],[[-96.914657,32.836659],[-96.914518,32.837522],[-96.914472,32.837673],[-96.914383,32.837772],[-96.914284,32.837858],[-96.914153,32.837905],[-96.914024,32.837912],[-96.913925,32.837885],[-96.913804,32.837828],[-96.913678,32.837729],[-96.913391,32.837479],[-96.913278,32.837328],[-96.913252,32.837238],[-96.913262,32.837148],[-96.913305,32.837078],[-96.91337,32.837024],[-96.91348,32.837004],[-96.914483,32.83688]],[[-96.915915,32.836977],[-96.916183,32.837004],[-96.916425,32.837042],[-96.916862,32.837164]],[[-96.915183,32.840102],[-96.915151,32.839929],[-96.915113,32.839539],[-96.915008,32.838973],[-96.914949,32.83873],[-96.914909,32.838491],[-96.914834,32.838234]],[[-96.914728,32.842674],[-96.914772,32.84257],[-96.914872,32.842277],[-96.914974,32.841939],[-96.915014,32.841646],[-96.915065,32.84138],[-96.915113,32.84099],[-96.915175,32.840321],[-96.915193,32.839827],[-96.915204,32.839345],[-96.91525,32.839045],[-96.915325,32.838759],[-96.915483,32.838464],[-96.915658,32.838236],[-96.915907,32.837943],[-96.91621,32.837709],[-96.91636,32.837612],[-96.916588,32.837481],[-96.916948,32.837324],[-96.91731,32.837188],[-96.918026,32.837098],[-96.918847,32.83706],[-96.919391,32.83704],[-96.919847,32.83701],[-96.920174,32.836961]],[[-96.914603,32.839688],[-96.914335,32.841087],[-96.914099,32.841896],[-96.913823,32.842577],[-96.91377,32.842674]],[[-96.915915,32.836977],[-96.916105,32.837042],[-96.916183,32.837116],[-96.916218,32.837164],[-96.916232,32.83722],[-96.916223,32.837308],[-96.916202,32.837355],[-96.916006,32.837556],[-96.915714,32.837779],[-96.915588,32.83784],[-96.915419,32.837871],[-96.915293,32.837867],[-96.91514,32.837828],[-96.915062,32.837774],[-96.915014,32.837734],[-96.914925,32.837614],[-96.914888,32.837515],[-96.914831,32.837297]],[[-96.917743,32.833443],[-96.917766,32.836553]],[[-96.912399,32.842674],[-96.911487,32.841373],[-96.9104,32.841399]],[[-96.91072,32.84264],[-96.910717,32.842674]],[[-96.911584,32.833443],[-96.911581,32.833684],[-96.911559,32.835271],[-96.911554,32.83642]],[[-96.920403,32.833443],[-96.920397,32.833894]],[[-96.9104,32.833662],[-96.911581,32.833684]],[[-96.914775,32.834518],[-96.914512,32.835038],[-96.914327,32.835437],[-96.914016,32.836136],[-96.913973,32.83621],[-96.913906,32.83628],[-96.913769,32.836359],[-96.913675,32.836384],[-96.913203,32.836445]],[[-96.9104,32.837231],[-96.910695,32.837202],[-96.911025,32.837182],[-96.911355,32.837168],[-96.911948,32.837168],[-96.912391,32.837222],[-96.912656,32.83729],[-96.913072,32.83752]],[[-96.916189,32.83818],[-96.918117,32.837339]],[[-96.918165,32.84179],[-96.917329,32.840988],[-96.91702,32.840704],[-96.916811,32.840587],[-96.916661,32.840519],[-96.916368,32.840454],[-96.916164,32.840438],[-96.915263,32.840458]],[[-96.914137,32.840553],[-96.914316,32.840555]],[[-96.91415,32.840452],[-96.914054,32.841182],[-96.913976,32.841572],[-96.913842,32.842049],[-96.913691,32.842414],[-96.913569,32.842674]],[[-96.914919,32.842674],[-96.91492,32.842671],[-96.9151,32.842171],[-96.915244,32.84152],[-96.915322,32.840979],[-96.915376,32.840569],[-96.915384,32.840454]],[[-96.915474,32.833443],[-96.915379,32.833664],[-96.915314,32.833772],[-96.915261,32.833849],[-96.915081,32.834083],[-96.914775,32.834518]],[[-96.913072,32.83752],[-96.913195,32.837608],[-96.913675,32.838002],[-96.913694,32.838011],[-96.91396,32.838241],[-96.914054,32.838345],[-96.914161,32.838507],[-96.914255,32.838714],[-96.914292,32.83889],[-96.914319,32.839063],[-96.914182,32.840098],[-96.91415,32.840452]],[[-96.917406,32.836557],[-96.91588,32.836517],[-96.915778,32.836501],[-96.915727,32.836481],[-96.91569,32.836458],[-96.915644,32.836422],[-96.915609,32.836382],[-96.915588,32.836348],[-96.915572,32.836305],[-96.915564,32.836235],[-96.915564,32.836154],[-96.915668,32.835469],[-96.915754,32.834827],[-96.915781,32.834689],[-96.915835,32.834556],[-96.916052,32.834074],[-96.916283,32.833443]],[[-96.915384,32.840454],[-96.915408,32.839794],[-96.915411,32.839345],[-96.915459,32.839068],[-96.91554,32.838845],[-96.915636,32.838687],[-96.915765,32.838529],[-96.915945,32.838358],[-96.916189,32.83818]],[[-96.919421,32.837159],[-96.919557,32.837216],[-96.919697,32.837288],[-96.919812,32.8374],[-96.919874,32.837479],[-96.920172,32.838358],[-96.920295,32.838845],[-96.920333,32.839068],[-96.920327,32.839422],[-96.920223,32.839746],[-96.920072,32.840046],[-96.919898,32.840276],[-96.919812,32.840355],[-96.919426,32.84067],[-96.918012,32.84172],[-96.91753,32.84207],[-96.917374,32.842196],[-96.916727,32.842674]],[[-96.916981,32.842674],[-96.917079,32.842602],[-96.917487,32.842291],[-96.918114,32.841826],[-96.919984,32.840454],[-96.920236,32.840233],[-96.920429,32.839965],[-96.920512,32.839816],[-96.92059,32.839557],[-96.920612,32.839271],[-96.920604,32.839181],[-96.920574,32.839043],[-96.920416,32.83857],[-96.920365,32.83839],[-96.920341,32.838261],[-96.9203,32.838119],[-96.920282,32.837943],[-96.920274,32.83775],[-96.920295,32.837603],[-96.920319,32.837536],[-96.920397,32.837407],[-96.920515,32.837288],[-96.920601,32.837227],[-96.920727,32.83717]],[[-96.91441,32.836582],[-96.914188,32.836571],[-96.913203,32.836445]],[[-96.914324,32.840454],[-96.91415,32.840452]],[[-96.921387,32.836595],[-96.920815,32.836594],[-96.917766,32.836553],[-96.917406,32.836557],[-96.915851,32.836621],[-96.915459,32.836621],[-96.915129,32.836607]],[[-96.918117,32.837339],[-96.918324,32.837252],[-96.918586,32.837186],[-96.918844,32.837161],[-96.919421,32.837159],[-96.920727,32.83717],[-96.921387,32.83717]],[[-96.913203,32.836445],[-96.913142,32.836438],[-96.912999,32.836438],[-96.912774,32.836427],[-96.912573,32.836433],[-96.911554,32.83642],[-96.9104,32.836416]],[[-96.915253,32.840564],[-96.915376,32.840569],[-96.915816,32.840562],[-96.916197,32.840567],[-96.916478,32.840598],[-96.916623,32.840641],[-96.916757,32.840702],[-96.916913,32.840794],[-96.917326,32.841157],[-96.917452,32.841272],[-96.917956,32.841761],[-96.918061,32.841867]],[[-96.9145,32.842674],[-96.914547,32.842534],[-96.914708,32.841971],[-96.914802,32.841473],[-96.914858,32.841078],[-96.914882,32.840742],[-96.914907,32.840224],[-96.914915,32.839706],[-96.914885,32.839084],[-96.914826,32.83811],[-96.914831,32.837297]],[[-96.914987,32.836886],[-96.915092,32.836889]],[[-96.914526,32.836763],[-96.914155,32.836758],[-96.9104,32.836758]],[[-96.921387,32.836813],[-96.918825,32.836796],[-96.916358,32.836774],[-96.915003,32.836767]],[[-96.914831,32.837297],[-96.914872,32.836663],[-96.915033,32.835872],[-96.91514,32.83546],[-96.915226,32.835212],[-96.915853,32.8337],[-96.91595,32.833443]],[[-96.915092,32.836889],[-96.916398,32.836891],[-96.919547,32.836963],[-96.921322,32.836952],[-96.921387,32.836952]],[[-96.9104,32.837008],[-96.910671,32.836977],[-96.9111,32.836941],[-96.911543,32.836925],[-96.912637,32.836898],[-96.913501,32.836886],[-96.914528,32.83688]],[[-96.91577,32.833443],[-96.915671,32.833693],[-96.915344,32.834518],[-96.91481,32.835911],[-96.914754,32.836127],[-96.914657,32.836659],[-96.914617,32.83752],[-96.914595,32.838561],[-96.914603,32.839688],[-96.914587,32.840693],[-96.914512,32.841211],[-96.914421,32.841666],[-96.914284,32.842196],[-96.914097,32.842674]],[[-96.914316,32.840555],[-96.915253,32.840564]],[[-96.916862,32.837164],[-96.917377,32.837281]],[[-96.915129,32.836607],[-96.91441,32.836582]],[[-96.915263,32.840458],[-96.914324,32.840454]],[[-96.914528,32.83688],[-96.914987,32.836886]],[[-96.915003,32.836767],[-96.914526,32.836763]
func assertErr(t *testing.T, actual float64, expected float64, maxErr float64, description string) {
errVal := math.Abs((actual - expected) / expected)
if errVal > maxErr {
strErrVal := strconv.FormatFloat(errVal, 'f', 6, 64)
strMaxErr := strconv.FormatFloat(maxErr, 'f', 6, 64)
t.Error("Error margin was too high, error was "+strErrVal+", max error is "+strMaxErr, "description: "+description)
}
}
func TestNewCheapruler(t *testing.T) {
t.Log("Check NewCheapruler constructor functions appropriately")
cr, err := NewCheapruler(1.0, "metres")
if err != nil {
t.Error(err)
}
t.Log("OK", cr)
}
func TestInvalidNewCheapruler(t *testing.T) {
t.Log("Check NewCheapruler constructor fails as expected")
_, err := NewCheapruler(1.0, "nonesense")
if err != nil {
t.Log("Errored as expected")
} else {
t.Error("Cheapruler constructor did not throw an error as expected for wrong units")
}
}
func TestCheaprulerHasMethods(t *testing.T) {
t.Log("Check NewCheapruler has all exported methods")
cr, err := NewCheapruler(1.0, "metres")
if err != nil {
t.Error(err)
}
t.Log("OK", cr.Along)
t.Log("OK", cr.Area)
t.Log("OK", cr.Bearing)
t.Log("OK", cr.BufferBBox)
t.Log("OK", cr.BufferPoint)
t.Log("OK", cr.Destination)
t.Log("OK", cr.Distance)
t.Log("OK", cr.InsideBBox)
t.Log("OK", cr.LineDistance)
t.Log("OK", cr.Factors)
}
func TestNewCheaprulerFromTile(t *testing.T) {
t.Log("Check NewCheaprulerFromTile works correctly")
cr, err := NewCheaprulerFromTile(1.0, 1.0, "metres")
if err != nil {
t.Error(err)
}
t.Log("OK", cr)
}
func TestInvalidNewCheaprulerFromTile(t *testing.T) {
t.Log("Check NewCheaprulerFromTile errors correctly")
_, err := NewCheaprulerFromTile(1.0, 1.0, "nonesense")
if err != nil {
t.Log("Errored as expected")
} else {
t.Error("Cheapruler constructor did not throw an error as expected for wrong units")
}
}
func TestDistanceKilometers(t *testing.T) {
t.Log("Check distance calculations are accurate in kilometers")
kilocr, _ := NewCheapruler(32.8351, "kilometers")
expected := kilocr.Distance([]float64{-96.920341, 32.838261}, []float64{-96.920421, 32.838295})
actual := 0.008385790760648736
assertErr(t, expected, actual, 0.003, "distance in kilometres")
}
func ExampleCheapRuler_Distance() {
cr, _ := NewCheapruler(32.8351, "kilometers")
pointA := []float64{-96.920341, 32.838261}
pointB := []float64{-96.920421, 32.838295}
dist := cr.Distance(pointA, pointB)
fmt.Println(dist)
// Output: 0.008385790760648736
}
func TestDistanceMiles(t *testing.T) {
t.Log("Check distance calculations are accurate in miles")
milescr, _ := NewCheapruler(32.8351, "miles")
kilocr, _ := NewCheapruler(32.8351, "kilometers")
d := kilocr.Distance([]float64{30.5, 32.8351}, []float64{30.51, 32.8451})
d2 := milescr.Distance([]float64{30.5, 32.8351}, []float64{30.51, 32.8451})
assertErr(t, d/d2, 1.609344, 1e-12, "distance in miles")
}
func TestBearing(t *testing.T) {
t.Log("Check bearing calculations are accurate in miles")
kilocr, _ := NewCheapruler(32.8351, "kilometers")
actual := kilocr.Bearing([]float64{-96.920341, 32.838261}, []float64{-96.920421, 32.838295})
expected := -63.279807556490866
assertErr(t, actual, expected, 0.005, "bearing")
}
func TestBearingZero(t *testing.T) {
t.Log("Test when bearing is zero")
kilocr, _ := NewCheapruler(30, "kilometers")
actual := kilocr.Bearing([]float64{10, 10}, []float64{10, 10})
expected := 0.0
assertErr(t, actual, expected, 0.005, "zero bearing")
}
func TestDestination(t *testing.T) {
t.Log("Test destination function")
kilocr, _ := NewCheapruler(0, "kilometers")
actual := kilocr.Destination([]float64{0, 0}, 10000, 0)
expected := []float64{0.000000000000005500534937111209, 90.44270255818994}
assertErr(t, actual[0], expected[0], 0.005, "destination x")
assertErr(t, actual[1], expected[1], 0.005, "destination y")
}
func TestLineDistance(t *testing.T) {
t.Log("Test LineDistance function")
kilocr, _ := NewCheapruler(0, "kilometers")
point1 := []float64{0, 0}
point2 := []float64{0, 50}
actual := kilocr.LineDistance([][]float64{point1, point2})
expected := 5528.362
assertErr(t, actual, expected, 0.005, "line distance")
}
func TestArea(t *testing.T) {
t.Log("Test Area function")
kilocr, _ := NewCheapruler(0, "kilometers")
point1 := []float64{-67.031, 50.458}
point2 := []float64{-67.031, 50.534}
point3 := []float64{-66.929, 50.534}
point4 := []float64{-66.929, 50.458}
points := [][]float64{point1, point2, point3, point4, point1}
outer := [][][]float64{points}
actual := kilocr.Area(outer)
// todo: why is result 0.0?
expected := 0.0
assertErr(t, actual, expected, 0.005, "area")
}
func TestAlong(t *testing.T) {
t.Log("Along function")
kilocr, _ := NewCheapruler(0, "kilometers")
point1 := []float64{0, 0}
point2 := []float64{0, 50}
actual := kilocr.Along([][]float64{point1, point2}, 25)
expectedX := 0.0
expectedY := 0.2261067563954748
assertErr(t, actual[0], expectedX, 0.005, "line along x")
assertErr(t, actual[1], expectedY, 0.005, "line along y")
}
func TestPointOnLine(t *testing.T) {
t.Log("Point on line function")
kilocr, _ := NewCheapruler(0, "kilometers")
point1 := []float64{0, 0}
point2 := []float64{0, 50}
pointOnLine := []float64{0.5, 0.5}
actual := kilocr.PointOnLine([][]float64{point1, point2}, pointOnLine)
assertErr(t, actual.Point[0], 0.0, 0.005, "Point on line x")
assertErr(t, actual.Point[1], 0.5, 0.005, "Point on line y")
}
func TestInsideBBox(t *testing.T) {
t.Log("InsideBBox function")
kilocr, _ := NewCheapruler(0, "kilometers")
p := []float64{1, 1}
actual := kilocr.InsideBBox(p, []float64{0, 0, 2, 2})
if actual != true {
t.Error("point should be inside bounding box")
}
}
func TestBufferPoint(t *testing.T) {
t.Log("Test buffer point function")
kilocr, _ := NewCheapruler(0, "kilometers")
p := []float64{0, 0}
actual := kilocr.BufferPoint(p, 500)
// BufferPoint returns: []float64{w, s, e, n}
expected := []float64{-4.491527631428836, -4.522135127909497, 4.491527631428836, 4.522135127909497}
assertErr(t, actual[0], expected[0], 0.005, "Buffer point first")
assertErr(t, actual[1], expected[1], 0.005, "Buffer point second")
assertErr(t, actual[2], expected[2], 0.005, "Buffer point third")
assertErr(t, actual[3], expected[3], 0.005, "Buffer point third")
}
func TestBufferBbox(t *testing.T) {
t.Log("Test buffer bbox function")
kilocr, _ := NewCheapruler(0, "kilometers")
bbox := []float64{30.5, 50.5, 31, 51}
actual := kilocr.BufferBBox(bbox, 0.2)
expected := []float64{30.498203388947427, 50.49819114594884, 31.001796611052573, 51.00180885405116}
assertErr(t, actual[0], expected[0], 0.005, "Buffer point first")
assertErr(t, actual[1], expected[1], 0.005, "Buffer point second")
assertErr(t, actual[2], expected[2], 0.005, "Buffer point third")
assertErr(t, actual[3], expected[3], 0.005, "Buffer point third")
}
func TestLineSlice(t *testing.T) {
t.Log("Test line slice function")
kilocr, _ := NewCheapruler(0, "kilometers")
p1 := []float64{-67.04, 50.5}
p2 := []float64{-67.05, 50.56}
line := [][]float64{{-67.031, 50.458}, {-67.031, 50.534}, {-66.929, 50.534}, {-66.929, 50.458}}
actual := kilocr.LineSlice(p1, p2, line)
sl1 := []float64{-67.031, 50.5}
sl2 := []float64{-67.031, 50.534}
assertErr(t, actual[0][0], sl1[0], 0.005, "Line slice first")
assertErr(t, actual[0][1], sl1[1], 0.005, "Line slice second")
assertErr(t, actual[1][0], sl2[0], 0.005, "Line slice third")
assertErr(t, actual[1][1], sl2[1], 0.005, "Line slice fourth")
}
func TestLineSliceAlong(t *testing.T) {
t.Log("Test line slice along function")
kilocr, _ := NewCheapruler(0, "kilometers")
line := [][]float64{{-67.031, 50.458}, {-67.031, 50.534}, {-66.929, 50.534}, {-66.929, 50.458}}
actual := kilocr.LineSliceAlong(10, 20, line)
p1 := []float64{-67.01665505103723, 50.534}
p2 := []float64{-66.929, 50.534}
p3 := []float64{-66.929, 50.53180967346205}
if len(actual) != 3 {
t.Error("the length should be 3")
}
assertErr(t, actual[0][0], p1[0], 0.005, "Line slice first")
assertErr(t, actual[0][1], p1[1], 0.005, "Line slice second")
assertErr(t, actual[1][0], p2[0], 0.005, "Line slice third")
assertErr(t, actual[1][1], p2[1], 0.005, "Line slice fourth")
assertErr(t, actual[2][0], p3[0], 0.005, "Line slice fifth")
assertErr(t, actual[2][1], p3[1], 0.005, "Line slice sixt")
}