forked from adamfranco/curvature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_output.py
297 lines (277 loc) · 13.7 KB
/
test_output.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
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
# Add our parent folder to our path
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import pytest
from curvature.output import KmlOutput
from copy import copy
@pytest.fixture
def road_a():
# Note that length, radius, and curvature do not actually match the coordinates in this
# synthetic test data. They are added to test that they remain in the output data.
return {'id': 1000,
'tags': { 'name': 'Road A',
'highway': 'unclassified',
'ref': 'VT-555;US-1'},
'refs': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'coords': [ [44.47486310000014, -73.20887729999983],
[44.47606690000014, -73.20894139999983],
[44.47617410000035, -73.20894919999994],
[44.47721230000033, -73.20902129999995],
[44.47728820000014, -73.20902659999983],
[44.477370500000326, -73.20903419999996],
[44.478012899999946, -73.20909320000007],
[44.478329400000135, -73.20911439999983],
[44.47902100000032, -73.20917629999997],
[44.47909000000013, -73.20918349999984]],
'segments': [
# Curve
{ 'start': [44.47486310000014, -73.20887729999983],
'end': [44.47606690000014, -73.20894139999983],
'length': 100,
'radius': 150,
'curvature': 100,
'curvature_level': 1},
# Short straight segment
{ 'start': [44.47606690000014, -73.20894139999983],
'end': [44.47617410000035, -73.20894919999994],
'length': 100,
'radius': 300,
'curvature': 0,
'curvature_level': 0},
# 2 Curved segments
{ 'start': [44.47617410000035, -73.20894919999994],
'end': [44.47721230000033, -73.20902129999995],
'length': 100,
'radius': 90,
'curvature': 130,
'curvature_level': 2},
{ 'start': [44.47721230000033, -73.20902129999995],
'end': [44.47728820000014, -73.20902659999983],
'length': 60,
'radius': 50,
'curvature': 96,
'curvature_level': 3},
# two straight segments over the threshold.
{ 'start': [44.47728820000014, -73.20902659999983],
'end': [44.477370500000326, -73.20903419999996],
'length': 1500,
'radius': 2000,
'curvature': 0,
'curvature_level': 0},
{ 'start': [44.477370500000326, -73.20903419999996],
'end': [44.478012899999946, -73.20909320000007],
'length': 2000,
'radius': 2000,
'curvature': 0,
'curvature_level': 0},
# Curves
{ 'start': [44.478012899999946, -73.20909320000007],
'end': [44.478329400000135, -73.20911439999983],
'length': 100,
'radius': 150,
'curvature': 100,
'curvature_level': 1},
{ 'start': [44.478329400000135, -73.20911439999983],
'end': [44.47902100000032, -73.20917629999997],
'length': 10,
'radius': 25,
'curvature': 20,
'curvature_level': 4},
{ 'start': [44.47902100000032, -73.20917629999997],
'end': [44.47909000000013, -73.20918349999984],
'length': 60,
'radius': 90,
'curvature': 78,
'curvature_level': 2}]}
@pytest.fixture
def road_b():
# Note that length, radius, and curvature do not actually match the coordinates in this
# synthetic test data. They are added to test that they remain in the output data.
return {'id': 1001,
'tags': { 'name': 'Road B',
'highway': 'unclassified',
'ref': 'VT-555'},
'refs': [10, 12, 13, 14, 15],
'coords': [ [44.47909000000013, -73.20918349999984],
[44.57606690000014, -73.30894139999983],
[44.57617410000035, -73.30894919999994],
[44.57721230000033, -73.30902129999995],
[44.57728820000014, -73.30902659999983]],
'segments': [
# Straight segment
{ 'start': [44.47909000000013, -73.20918349999984],
'end': [44.57606690000014, -73.30894139999983],
'length': 1000,
'radius': 3000,
'curvature': 0,
'curvature_level': 0},
# Straight segment
{ 'start': [44.57606690000014, -73.30894139999983],
'end': [44.57617410000035, -73.30894919999994],
'length': 1000,
'radius': 3000,
'curvature': 0,
'curvature_level': 0},
# 2 Curved segments
{ 'start': [44.57617410000035, -73.30894919999994],
'end': [44.57721230000033, -73.30902129999995],
'length': 100,
'radius': 90,
'curvature': 130,
'curvature_level': 2},
{ 'start': [44.57721230000033, -73.30902129999995],
'end': [44.57728820000014, -73.30902659999983],
'length': 60,
'radius': 50,
'curvature': 96,
'curvature_level': 3}]}
def test_get_collection_segments(road_a, road_b):
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
segments = output.get_collection_segments(collection)
assert len(segments) == 13
assert segments[0]['start'][0] == 44.47486310000014
assert segments[12]['start'][0] == 44.57721230000033
def test_get_collection_curvature(road_a, road_b):
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
curvature = output.get_collection_curvature(collection)
assert curvature == 750
def test_get_collection_curvature_on_way(road_a, road_b):
output = KmlOutput('km')
# Set a different curvature for road_b: 300 instead of 226
road_b['curvature'] = 300
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
curvature = output.get_collection_curvature(collection)
assert curvature == 824
def test_get_collection_length(road_a, road_b):
# road_a length == 4030
# road_b length == 2160
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
length = output.get_collection_length(collection)
assert length == 6190
def test_get_collection_length_on_way(road_a, road_b):
# road_a length == 4030
# road_b length == 2160
output = KmlOutput('km')
# Set a different curvature for road_b: 4000 instead of 2160
road_b['length'] = 4000
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
length = output.get_collection_length(collection)
assert length == 8030
def test_get_length_weighted_collection_tags(road_a, road_b):
# road_a length == 4030
# road_b length == 2160
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
names = output.get_length_weighted_collection_tags(collection, 'name')
assert names == ['Road A', 'Road B']
# Ensure that reversing the ways doesn't change anything.
collection = {'join_type': 'arbitrary', 'ways': [road_b, road_a]}
names = output.get_length_weighted_collection_tags(collection, 'name')
assert names == ['Road A', 'Road B']
# Ensure that giving B a larger length does change the order.
road_b['length'] = 5000
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
names = output.get_length_weighted_collection_tags(collection, 'name')
assert names == ['Road B', 'Road A']
def test_get_length_weighted_collection_tags_many_ways(road_a, road_b):
# road_a length == 4030
# road_b length == 2160
output = KmlOutput('km')
road_c = {'id': 1002,
'tags': { 'name': 'Road B',
'highway': 'tertiary',
'ref': 'VT-555'},
'length': 3000}
road_d = {'id': 1003,
'tags': { 'name': 'Road A',
'highway': 'tertiary',
'ref': 'VT-555'},
'length': 100}
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b, road_c, road_d]}
names = output.get_length_weighted_collection_tags(collection, 'name')
assert names == ['Road B', 'Road A']
def test_get_shared_collection_ref(road_a, road_b):
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
refs = output.get_shared_collection_refs(collection)
assert len(refs) == 1
assert 'VT-555' in refs
# Ensure that if a way in the collection has no refs, there are none shared.
road_c = {'id': 1002,
'tags': { 'name': 'Road B',
'highway': 'tertiary'},
'length': 3000}
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b, road_c]}
refs = output.get_shared_collection_refs(collection)
assert len(refs) == 0
# Ensure that multiple refs can be returned
road_c = {'id': 1002,
'tags': { 'name': 'Road B',
'highway': 'tertiary',
'ref': 'VT-555;VT-17'},
'length': 3000}
road_d = {'id': 1003,
'tags': { 'name': 'Road A',
'highway': 'tertiary',
'ref': 'VT-555;VT-17'},
'length': 100}
road_e = {'id': 1003,
'tags': { 'name': 'Road A',
'highway': 'tertiary',
'ref': 'VT-555;US-30;VT-17'},
'length': 100}
collection = {'join_type': 'arbitrary', 'ways': [road_c, road_d, road_e]}
refs = output.get_shared_collection_refs(collection)
assert len(refs) == 2
assert 'VT-555' in refs
assert 'VT-17' in refs
def test_get_collection_name(road_a, road_b):
# road_a length == 4030
# road_b length == 2160
output = KmlOutput('km')
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b]}
name = output.get_collection_name(collection)
assert name == 'Road A (VT-555)'
# Try with additional ways that make 'Road B' longer.
road_c = {'id': 1002,
'tags': { 'name': 'Road B',
'highway': 'tertiary',
'ref': 'VT-555;VT-17'},
'length': 3000}
road_d = {'id': 1003,
'tags': { 'name': 'Road A',
'highway': 'tertiary',
'ref': 'VT-555;VT-17'},
'length': 100}
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b, road_c, road_d]}
name = output.get_collection_name(collection)
assert name == 'Road B (VT-555)'
# Multiple route-numbers.
road_a['tags']['ref'] = 'VT-555;VT-17'
road_b['tags']['ref'] = 'VT-555;VT-17'
road_e = {'id': 1003,
'tags': { 'name': 'Road A',
'highway': 'tertiary',
'ref': 'VT-555;US-30;VT-17'},
'length': 100}
collection = {'join_type': 'arbitrary', 'ways': [road_a, road_b, road_c, road_d, road_e]}
name = output.get_collection_name(collection)
assert name == 'Road B (VT-555 / VT-17)' or name == 'Road B (VT-17 / VT-555)'
# no road name
road_f = {'id': 1004,
'tags': { 'highway': 'unclassified'},
'length': 100}
collection = {'join_type': 'none', 'ways': [road_f]}
name = output.get_collection_name(collection)
assert name == '1004'
# ref only
road_g = {'id': 1005,
'tags': { 'highway': 'tertiary',
'ref': 'VT-555;VT-17'},
'length': 100}
collection = {'join_type': 'none', 'ways': [road_g]}
name = output.get_collection_name(collection)
assert name == 'VT-555 / VT-17' or name == 'VT-17 / VT-555'