def _parse_route_point(self, node): lat = self.xml_parser.get_node_attribute(node, 'lat') if not lat: raise mod_gpx.GPXException('Waypoint without latitude') lon = self.xml_parser.get_node_attribute(node, 'lon') if not lon: raise mod_gpx.GPXException('Waypoint without longitude') lat = mod_utils.to_number(lat) lon = mod_utils.to_number(lon) elevation_node = self.xml_parser.get_first_child(node, 'ele') elevation = mod_utils.to_number( self.xml_parser.get_node_data(elevation_node), None) time_node = self.xml_parser.get_first_child(node, 'time') time_str = self.xml_parser.get_node_data(time_node) time = parse_time(time_str) name_node = self.xml_parser.get_first_child(node, 'name') name = self.xml_parser.get_node_data(name_node) desc_node = self.xml_parser.get_first_child(node, 'desc') desc = self.xml_parser.get_node_data(desc_node) sym_node = self.xml_parser.get_first_child(node, 'sym') sym = self.xml_parser.get_node_data(sym_node) type_node = self.xml_parser.get_first_child(node, 'type') type = self.xml_parser.get_node_data(type_node) comment_node = self.xml_parser.get_first_child(node, 'cmt') comment = self.xml_parser.get_node_data(comment_node) hdop_node = self.xml_parser.get_first_child(node, 'hdop') hdop = mod_utils.to_number(self.xml_parser.get_node_data(hdop_node)) vdop_node = self.xml_parser.get_first_child(node, 'vdop') vdop = mod_utils.to_number(self.xml_parser.get_node_data(vdop_node)) pdop_node = self.xml_parser.get_first_child(node, 'pdop') pdop = mod_utils.to_number(self.xml_parser.get_node_data(pdop_node)) return mod_gpx.GPXRoutePoint(lat, lon, elevation, time, name, desc, sym, type, comment, horizontal_dilution=hdop, vertical_dilution=vdop, position_dilution=pdop)
def _parse_waypoint(self, node): if not node.attributes.has_key('lat'): raise mod_gpx.GPXException('Waypoint without latitude') if not node.attributes.has_key('lon'): raise mod_gpx.GPXException('Waypoint without longitude') lat = mod_utils.to_number(node.attributes['lat'].nodeValue) lon = mod_utils.to_number(node.attributes['lon'].nodeValue) elevation_node = mod_utils.find_first_node(node, 'ele') elevation = mod_utils.to_number(self.get_node_data(elevation_node), 0) time_node = mod_utils.find_first_node(node, 'time') time_str = self.get_node_data(time_node) time = parse_time(time_str) name_node = mod_utils.find_first_node(node, 'name') name = self.get_node_data(name_node) desc_node = mod_utils.find_first_node(node, 'desc') desc = self.get_node_data(desc_node) sym_node = mod_utils.find_first_node(node, 'sym') sym = self.get_node_data(sym_node) type_node = mod_utils.find_first_node(node, 'type') type = self.get_node_data(type_node) comment_node = mod_utils.find_first_node(node, 'cmt') comment = self.get_node_data(comment_node) hdop_node = mod_utils.find_first_node(node, 'hdop') hdop = mod_utils.to_number(self.get_node_data(hdop_node)) vdop_node = mod_utils.find_first_node(node, 'vdop') vdop = mod_utils.to_number(self.get_node_data(vdop_node)) pdop_node = mod_utils.find_first_node(node, 'pdop') pdop = mod_utils.to_number(self.get_node_data(pdop_node)) return mod_gpx.GPXWaypoint(latitude=lat, longitude=lon, elevation=elevation, time=time, name=name, description=desc, symbol=sym, type=type, comment=comment, horizontal_dilution=hdop, vertical_dilution=vdop, position_dilution=pdop)
def parse(xml_or_file): """ Parse xml (string) or file object. This is just an wrapper for GPXParser.parse() function """ parser = mod_parser.GPXParser(xml_or_file) gpx = parser.parse() if not parser.is_valid(): raise mod_gpx.GPXException('Error parsing {0}: {1}'.format( xml_or_file[0:100], parser.get_error())) return gpx
def __parse_dom(self): node = self.xml_parser.get_first_child(name='gpx') if node is None: raise mod_gpx.GPXException('Document must have a `gpx` root node.') for node in self.xml_parser.get_children(node): node_name = self.xml_parser.get_node_name(node) if node_name == 'time': time_str = self.xml_parser.get_node_data(node) self.gpx.time = parse_time(time_str) elif node_name == 'name': self.gpx.name = self.xml_parser.get_node_data(node) elif node_name == 'desc': self.gpx.description = self.xml_parser.get_node_data(node) elif node_name == 'author': self.gpx.author = self.xml_parser.get_node_data(node) elif node_name == 'email': self.gpx.email = self.xml_parser.get_node_data(node) elif node_name == 'url': self.gpx.url = self.xml_parser.get_node_data(node) elif node_name == 'urlname': self.gpx.urlname = self.xml_parser.get_node_data(node) elif node_name == 'keywords': self.gpx.keywords = self.xml_parser.get_node_data(node) elif node_name == 'bounds': self._parse_bounds(node) elif node_name == 'wpt': self.gpx.waypoints.append(self._parse_waypoint(node)) elif node_name == 'rte': self.gpx.routes.append(self._parse_route(node)) elif node_name == 'trk': self.gpx.tracks.append(self.__parse_track(node)) else: #print 'unknown %s' % node pass self.valid = True
def parse(self): try: if self.xml_parser_type is None: if mod_etree: self.xml_parser = LXMLParser(self.xml) else: self.xml_parser = XMLParser(self.xml) elif self.xml_parser_type == 'lxml': self.xml_parser = LXMLParser(self.xml) elif self.xml_parser_type == 'minidom': self.xml_parser = XMLParser(self.xml) else: raise mod_gpx.GPXException('Invalid parser type: %s' % self.xml_parser_type) self.__parse_dom() return self.gpx except Exception as e: mod_logging.debug('Error in:\n%s\n-----------\n' % self.xml) mod_logging.exception(e) self.error = str(e) return None