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

Source Code for Module sabx10.oxm.stop

  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  Stop handling. 
 24  """ 
 25  from geom import Point, Box 
 26  from utils import get_from_id 
 27   
 28  ############################################################ 
 29  # XML 
 30  ############################################################ 
 31   
32 -class Stop(Point):
33 """ 34 A C{Stop} object sub-classes L{Point} and adds a description and type to 35 describe a stop. 36 37 @ivar type: type of stop 38 @type type: C{string} 39 @ivar description: description of the stop 40 @type description: C{string} 41 """
42 - def __init__(self, id, type, description, lat, lon):
43 """ 44 Save the passed-in data. 45 46 @param id: id of the stop 47 @type id: C{string} 48 @param type: type of stop (convenience store, restaurant, etc...) 49 @type type: C{string} 50 @param description: description of the stop 51 @type description: C{string} 52 @param lat: latitude of the stop 53 @type lat: C{float} 54 @param lon: longitude of the stop 55 @type lon: C{float} 56 """ 57 Point.__init__(self, lat, lon, id=id) 58 self.type = type 59 self.description = description
60
61 -def _parse_stop_xml(xml_stop):
62 """ 63 Take the C{Element} for a stop and turn it into a L{Stop} object. 64 65 @param xml_stop: C{Element} for a stop 66 @type xml_stop: C{Element} 67 68 @return: L{Stop} object 69 @rtype: L{Stop} 70 """ 71 return Stop(id = xml_stop.attrib['id'], 72 type = xml_stop.findtext('type'), 73 description = xml_stop.findtext('description'), 74 lat = xml_stop.findtext('lat'), 75 lon = xml_stop.findtext('lon'))
76
77 -def parse_stops(xml_tree):
78 """ 79 Get all the stop elements in the given C{Element} tree and create a list of 80 them with L{Stop} objects. 81 82 @param xml_tree: root of C{Element} tree that has stops in it 83 @type xml_tree: C{Element} or C{ElementTree} 84 85 @return: stops in a list and a dictionary 86 @rtype: (C{list} of L{Stop},C{dict} of L{Stop}) 87 """ 88 xml_stops = xml_tree.findall('stop') 89 stop_list = [] 90 stop_dict = {} 91 for xml_stop in xml_stops: 92 new_stop = _parse_stop_xml(xml_stop) 93 stop_list.append(new_stop) 94 stop_dict[new_stop.id] = new_stop 95 96 return stop_list, stop_dict
97 98 ############################################################ 99 # RIDES 100 ############################################################ 101
102 -def _stop_pts_dist(points):
103 """ 104 Generator that iterates through all the L{Point} objects, yielding the 105 point and the distance to it along the line for each point. 106 107 @param points: list of points for a segment 108 @type points: C{list} of L{Point} objects 109 110 @return: (L{Point},distance) 111 @rtype: (L{Point},C{float}) 112 """ 113 dist = 0.0 114 for index in range(len(points)-1): 115 yield points[index], dist 116 dist += points[index].calculate_distance(points[index+1]) 117 yield points[-1], dist
118
119 -def _process_stops_in_seg_data(ride_seg, xml_stops, index):
120 """ 121 Process the segment, extracting the stops and getting the bounds of the 122 stops. 123 124 @param ride_seg: L{Segment} to process 125 @type ride_seg: L{Segment} 126 @param xml_stops: C{list} of C{Element}s for stops in rideset 127 @type xml_stops: C{list} of C{Element} 128 @param index: next index to use for a stop 129 @type index: C{int} 130 131 @return: (stops for segment, bounds, updated index) 132 @rtype: (C{list} of L{Stop},L{Box},C{int}) 133 """ 134 seg_stops = [] 135 bounds = Box() 136 for waypoint, dist in _stop_pts_dist(ride_seg.waypoints): 137 if waypoint.stop is not None: 138 stop_list = waypoint.stop.split() 139 for stop_item in stop_list: 140 xml_stop = get_from_id(xml_stops, stop_item.strip()) 141 142 new_stop = _parse_stop_xml(xml_stop) 143 new_stop.description = " ".join(new_stop.description.split()) 144 new_stop.distance = ride_seg.start_dist + dist 145 new_stop.off_route = new_stop.calculate_distance(waypoint) 146 new_stop.index = index 147 index += 1 148 149 seg_stops.append(new_stop) 150 bounds.expand_to_point(new_stop.lat, new_stop.lon) 151 152 return seg_stops, bounds, index
153
154 -def process_ride_stops(seg_set, xml_segs, xml_stops):
155 """ 156 Process the stop references for the given ride and generate a list of 157 L{Stop} objects for it and the bounding box for all of the stops. 158 159 @param seg_set: C{list} of L{Segment} objects for the ride 160 @type seg_set: C{list} of L{Segment} objects 161 @param xml_segs: C{list} of C{Element} segment objects for this ride 162 @type xml_segs: C{list} of C{Element}s 163 @param xml_stops: C{list} of C{Element} stop objects 164 @type xml_stops: C{list} of C{Element}s 165 166 @return: (C{list} of L{Stop},C{bounds}) 167 @rtype: (C{list},L{Box}) 168 """ 169 index = 1 170 stop_list = [] 171 bounds = Box() 172 for ride_seg in seg_set: 173 new_stops, new_bounds, index = \ 174 _process_stops_in_seg_data(ride_seg, xml_stops, index) 175 stop_list.extend(new_stops) 176 bounds.expand_to_box(new_bounds) 177 178 return stop_list, bounds
179