def __parse_response(self, response): try: parsed_json_response = json.loads(response) feature = parsed_json_response['features'][0] return self._extract_lng_lat_from_result(feature) except IndexError: return [] except KeyError: raise MalformedResult()
def geocode_meta(self, searchtext, city=None, state=None, country=None): address = compose_address(searchtext, city, state, country) try: opt_params = self._build_optional_parameters(city, state, country) results = self.geocoder.geocode(address=address, components=opt_params) return self._process_results(results) except KeyError as e: self._logger.error('address: {}'.format(address), e) raise MalformedResult()
def geocode(self, searchtext, city=None, state=None, country=None): try: opt_params = self._build_optional_parameters(city, state, country) results = self.geocoder.geocode(address=searchtext, components=opt_params) if results: return self._extract_lng_lat_from_result(results[0]) else: return [] except KeyError: raise MalformedResult()
def __parse_routing_response(self, response): try: parsed_json_response = json.loads(response) legs = parsed_json_response['trip']['legs'][0] shape = PolyLine().decode(legs['shape']) length = legs['summary']['length'] duration = legs['summary']['time'] return MapzenRoutingResponse(shape, length, duration) except IndexError: return [] except KeyError: raise MalformedResult()
def _parse_response(self, response): try: json_response = response.json() isochrones = [] for feature in json_response['features']: # Coordinates could have more than one isochrone. For the # moment we're getting the first polygon only coordinates = feature['geometry']['coordinates'] duration = feature['properties']['contour'] mapzen_response = MapzenIsochronesResponse( coordinates, duration) isochrones.append(mapzen_response) return isochrones except IndexError: return [] except KeyError: self._logger.error( 'Non existing key for mapzen isochrones response', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers }) raise MalformedResult() except ValueError: # JSON decode error self._logger.error('JSON decode error for Mapzen isochrones', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers }) return []