-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gpx_data_utils.py
133 lines (102 loc) · 4.47 KB
/
test_gpx_data_utils.py
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
import unittest
import numpy as np
import pandas as pd
from gpxpy.gpx import GPXTrackPoint, GPXTrackSegment
from gpx_data_utils import (
gpx_point_from_array,
gpx_point_to_array,
gpx_segment_to_array,
gpx_segment_from_array,
gpx_segment_from_data_frame,
)
class TestPointConversions(unittest.TestCase):
def setUp(self):
self.point_as_array = np.array([1.0, 2.0, 3.0])
self.point = GPXTrackPoint(longitude=1.0, latitude=2.0, elevation=3.0)
self.attributes_to_test = ["longitude", "latitude", "elevation"]
def test_point_to_array(self):
point_as_array = gpx_point_to_array(self.point)
np.testing.assert_array_almost_equal(point_as_array, self.point_as_array)
def test_array_to_point(self):
point = gpx_point_from_array(self.point_as_array)
for attribute in self.attributes_to_test:
self.assertAlmostEqual(
getattr(point, attribute), getattr(self.point, attribute)
)
def test_roundtrip(self):
for i in range(25):
data = np.random.normal(size=(3,))
point = gpx_point_from_array(data)
data_from_point = gpx_point_to_array(point)
np.testing.assert_array_almost_equal(data, data_from_point)
class TestSegmentConversions(unittest.TestCase):
def setUp(self):
self.short_segment_as_array = np.array([[1.0, 0.0, 3]])
self.short_segment = GPXTrackSegment(
[gpx_point_from_array(self.short_segment_as_array[0])]
)
self.long_segment_as_array = np.random.normal(size=(25, 3))
self.long_segment = GPXTrackSegment(
[
gpx_point_from_array(point_data)
for point_data in self.long_segment_as_array
]
)
def test_segment_to_array_short(self):
short_segment_as_array = gpx_segment_to_array(self.short_segment)
self.assertEqual(short_segment_as_array.shape[1], 3)
np.testing.assert_array_almost_equal(
self.short_segment_as_array, short_segment_as_array
)
def test_array_to_segment_short(self):
short_segment = gpx_segment_from_array(self.short_segment_as_array)
self.assertEqual(len(short_segment.points), len(self.short_segment.points))
for i in range(len(short_segment.points)):
np.testing.assert_array_almost_equal(
gpx_point_to_array(short_segment.points[i]),
gpx_point_to_array(self.short_segment.points[i]),
)
def test_segment_to_array_long(self):
long_segment_as_array = gpx_segment_to_array(self.long_segment)
self.assertEqual(long_segment_as_array.shape[1], 3)
np.testing.assert_array_almost_equal(
self.long_segment_as_array, long_segment_as_array
)
def test_array_to_segment_long(self):
long_segment = gpx_segment_from_array(self.long_segment_as_array)
self.assertEqual(len(long_segment.points), len(self.long_segment.points))
for i in range(len(long_segment.points)):
np.testing.assert_array_almost_equal(
gpx_point_to_array(long_segment.points[i]),
gpx_point_to_array(self.long_segment.points[i]),
)
def test_bad_input_dim(self):
with self.assertRaises(AssertionError):
gpx_segment_from_array(np.random.normal(size=(7, 4)))
with self.assertRaises(AssertionError):
gpx_segment_from_array(np.random.normal(size=(7, 2)))
class TestSegmentDataFrameConversions(unittest.TestCase):
def setUp(self):
self.df = pd.DataFrame(
{
"longitude": [1, -1.5, 0.5],
"latitude": [-2, 37, 15],
"elevation": [5, 1005, 3],
}
)
def test_empty(self):
result = gpx_segment_from_data_frame(
pd.DataFrame({"longitude": [], "latitude": [], "elevation": []})
)
self.assertEqual(len(result.points), 0)
def test_simple(self):
segment_from_df = gpx_segment_from_data_frame(self.df)
segment_from_array = gpx_segment_from_array(self.df.values)
self.assertEqual(len(segment_from_df.points), len(segment_from_array.points))
for i in range(len(segment_from_df.points)):
np.testing.assert_array_almost_equal(
gpx_point_to_array(segment_from_df.points[i]),
gpx_point_to_array(segment_from_array.points[i]),
)
if __name__ == "__main__":
unittest.main()