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

Source Code for Module sabx10.oxm.poi

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