1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 Turn handling.
24 """
25 from geom import Point
26 from utils import get_from_id
27
28
29
30
31
33 """
34 A C{Turn} object sub-classes L{Point} and adds fromto, cue, and comments to
35 describe a turn. Turns don't natively have C{lat} and C{lon} values set,
36 since the SABX file doesn't have those for turns. Those values typically
37 get set when a turn is processed.
38
39 @ivar fromto: from segment to segment
40 @type fromto: C{string}
41 @ivar cue: instructions for executing the turn
42 @type cue: C{string}
43 @ivar comments: additional info about the turn
44 @type comments: C{string}
45 """
46 - def __init__(self, id, fromto, cue, comments):
47 """
48 Save the passed-in data.
49
50 @param id: id of the turn
51 @type id: C{string}
52 @param fromto: from segment to segment
53 @type fromto: C{string}
54 @param cue: instructions for executing the turn
55 @type cue: C{string}
56 @param comments: additional info about the turn
57 @type comments: C{string}
58 """
59 Point.__init__(self, id=id)
60 self.fromto = fromto
61 self.cue = cue
62 self.comments = comments
63
65 """
66 Take the C{Element} for a turn and turn it into a L{Turn} object.
67
68 @param xml_turn: C{Element} for a turn
69 @type xml_turn: C{Element}
70
71 @return: L{Turn} object
72 @rtype: L{Turn}
73 """
74 return Turn(id = xml_turn.attrib['id'],
75 fromto = xml_turn.findtext('fromto'),
76 cue = xml_turn.findtext('cue'),
77 comments = xml_turn.findtext('comments'))
78
80 """
81 Get all the turn elements in the given C{Element} tree and create a list of
82 them with L{Turn} objects.
83
84 @param xml_tree: root of C{Element} tree that has turns in it
85 @type xml_tree: C{Element} or C{ElementTree}
86
87 @return: L{Turn}s in a list and a dictionary
88 @rtype: (C{list} of L{Turn},C{dict} of L{Turn})
89 """
90 xml_turns = xml_tree.findall('turn')
91 turn_list = []
92 turn_dict = {}
93 for xml_turn in xml_turns:
94 new_turn = _parse_turn_xml(xml_turn)
95 turn_list.append(new_turn)
96 turn_dict[new_turn.id]= new_turn
97
98 return turn_list, turn_dict
99
100
101
102
103
105 """
106 Process the turn references for the given ride and generate a list of
107 L{Turn} objects for it.
108
109 @param ride_segs: C{list} of L{Segment} objects for the ride
110 @type ride_segs: C{list} of L{Segment} objects
111 @param xml_ride: C{Element} root of ride data tree
112 @type xml_ride: C{Element}
113 @param xml_turns: C{list} of C{Element} turn objects
114 @type xml_turns: C{list} of C{Element}s
115
116 @return: C{list} of L{Turn}
117 @rtype: C{list} of L{Turn}
118 """
119 ride_turns = [_parse_turn_xml(get_from_id(xml_turns, xml_turn.attrib['id']))
120 for xml_turn in xml_ride.findall('turn_ref')]
121
122
123 if len(ride_segs) != len(ride_turns):
124 raise Exception('turn and seg counts do not match')
125
126
127 for index, (turn, seg) in enumerate(zip(ride_turns, ride_segs)):
128 turn.comments = " ".join(turn.comments.split())
129 turn.distance = seg.start_dist
130 turn.lat = seg.lat
131 turn.lon = seg.lon
132 turn.index = index
133
134 return ride_turns
135