Package sabx10 :: Package oxm :: Module turn
[hide private]
[frames] | no frames]

Source Code for Module sabx10.oxm.turn

  1  ############################################################################### 
  2  # 
  3  # sabx10.oxm - an SABX file manipulation library 
  4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
  5  # 
  6  # This file is part of sabx10.oxm. 
  7  # 
  8  # sabx10.oxm is free software: you can redistribute it and/or modify it under 
  9  # the terms of the GNU Lesser General Public License as published by the Free 
 10  # Software Foundation, either version 3 of the License, or (at your option) any 
 11  # later version. 
 12  # 
 13  # sabx10.oxm is distributed in the hope that it will be useful, but WITHOUT ANY 
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 15  # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 16  # details. 
 17  # 
 18  # You should have received a copy of the GNU Lesser General Public License 
 19  # along with sabx10.oxm.  If not, see <http://www.gnu.org/licenses/>. 
 20  # 
 21  ############################################################################### 
 22  """ 
 23  Turn handling. 
 24  """ 
 25  from geom import Point 
 26  from utils import get_from_id 
 27   
 28  ############################################################ 
 29  # XML 
 30  ############################################################ 
 31   
32 -class Turn(Point):
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
64 -def _parse_turn_xml(xml_turn):
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
79 -def parse_turns(xml_tree):
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 # RIDE 102 ############################################################ 103
104 -def process_ride_turns(ride_segs, xml_ride, xml_turns):
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 # sanity check 123 if len(ride_segs) != len(ride_turns): 124 raise Exception('turn and seg counts do not match') 125 126 # process segs 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