def geocode(self, searchtext, city=None, state_province=None, country=None): if searchtext: searchtext = searchtext.decode('utf-8') if city: city = city.decode('utf-8') if state_province: state_province = state_province.decode('utf-8') if country: country = country.decode('utf-8') if not self._validate_input(searchtext, city, state_province, country): return [] address = [] if searchtext and searchtext.strip(): address.append(normalize(searchtext)) if city: address.append(normalize(city)) if state_province: address.append(normalize(state_province)) uri = self._uri(searchtext=', '.join(address), countries=country) try: response = requests.get(uri) if response.status_code == requests.codes.ok: return self._parse_geocoder_response(response.text) elif response.status_code == requests.codes.bad_request: return [] elif response.status_code == requests.codes.unprocessable_entity: return [] else: raise ServiceException(response.status_code, response) except requests.Timeout as te: # In case of timeout we want to stop the job because the server # could be down self._logger.error('Timeout connecting to TomTom geocoding server', te) raise ServiceException('Error geocoding {0} using TomTom'.format( searchtext), None) except requests.ConnectionError as ce: # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to TomTom geocoding server', exception=ce) return []
def geocode_meta(self, searchtext, city=None, state_province=None, country=None): if not self._validate_input(searchtext, city, state_province, country): return EMPTY_RESPONSE address = [] if searchtext and searchtext.strip(): address.append(normalize(searchtext)) if city: address.append(normalize(city)) if state_province: address.append(normalize(state_province)) free_search = ', '.join(address) response = self.geocode_free_text_meta([free_search], country) return response[0] if response else EMPTY_RESPONSE
def _geocode_meta(self, searchtext, city=None, state_province=None, country=None): if searchtext: searchtext = searchtext.decode('utf-8') if city: city = city.decode('utf-8') if state_province: state_province = state_province.decode('utf-8') if country: country = country.decode('utf-8') if not self._validate_input(searchtext, city, state_province, country): return (EMPTY_RESPONSE, None) address = [] if searchtext and searchtext.strip(): address.append(normalize(searchtext)) if city: address.append(normalize(city)) if state_province: address.append(normalize(state_province)) uri = self._uri(searchtext=', '.join(address), country=country) try: response = requests.get(uri) return (self._parse_response(response.status_code, response.text), response) except requests.Timeout as te: # In case of timeout we want to stop the job because the server # could be down msg = 'Timeout connecting to TomTom geocoding server' self._logger.error(msg, te) return (geocoder_error_response(msg), None) except requests.ConnectionError as ce: # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to TomTom geocoding server', exception=ce) return (EMPTY_RESPONSE, None)