def parse_place(place): location = util.get_first_text(place, ['address', 'name']) or None points = place.getElementsByTagName('Point') point = points and points[0] or None coords = util.get_first_text(point, 'coordinates') or None if coords: longitude, latitude = [float(f) for f in coords.split(',')[:2]] else: latitude = longitude = None _, (latitude, longitude) = self.geocode(location) return (location, (latitude, longitude))
def parse_xml(self, page, exactly_one=True): """Parse a location name, latitude, and longitude from an XML response. """ if not isinstance(page, basestring): page = util.decode_page(page) try: doc = xml.dom.minidom.parseString(page) except ExpatError: places = [] doc = None else: places = doc.getElementsByTagName('Placemark') if len(places) == 0 and doc is not None: # Got empty result. Parse out the status code and raise an error if necessary. status = doc.getElementsByTagName("Status") status_code = int(util.get_first_text(status[0], 'code')) self.check_status_code(status_code) if exactly_one and len(places) != 1: raise ValueError("Didn't find exactly one placemark! " \ "(Found %d.)" % len(places)) def parse_place(place): location = util.get_first_text(place, ['address', 'name']) or None points = place.getElementsByTagName('Point') point = points and points[0] or None coords = util.get_first_text(point, 'coordinates') or None if coords: longitude, latitude = [float(f) for f in coords.split(',')[:2]] else: latitude = longitude = None _, (latitude, longitude) = self.geocode(location) return (location, (latitude, longitude)) if exactly_one: return parse_place(places[0]) else: return [parse_place(place) for place in places]