<s> """<STR_LIT>""" <EOL> from geopy . geocoders . base import ( <EOL> Geocoder , <EOL> DEFAULT_FORMAT_STRING , <EOL> DEFAULT_TIMEOUT , <EOL> DEFAULT_SCHEME <EOL> ) <EOL> from geopy . compat import urlencode <EOL> from geopy . location import Location <EOL> from geopy . util import logger <EOL> from geopy . exc import GeocoderQueryError <EOL> __all__ = ( "<STR_LIT>" , ) <EOL> class Nominatim ( Geocoder ) : <EOL> """<STR_LIT>""" <EOL> structured_query_params = { <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:state>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> } <EOL> def __init__ ( <EOL> self , <EOL> format_string = DEFAULT_FORMAT_STRING , <EOL> view_box = None , <EOL> country_bias = None , <EOL> timeout = DEFAULT_TIMEOUT , <EOL> proxies = None , <EOL> domain = '<STR_LIT>' , <EOL> scheme = DEFAULT_SCHEME , <EOL> user_agent = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> super ( Nominatim , self ) . __init__ ( <EOL> format_string , scheme , timeout , proxies , user_agent = user_agent <EOL> ) <EOL> self . country_bias = country_bias <EOL> self . format_string = format_string <EOL> self . view_box = view_box <EOL> self . domain = domain . strip ( '<STR_LIT:/>' ) <EOL> self . api = "<STR_LIT>" % ( self . scheme , self . domain ) <EOL> self . reverse_api = "<STR_LIT>" % ( self . scheme , self . domain ) <EOL> def geocode ( <EOL> self , <EOL> query , <EOL> exactly_one = True , <EOL> timeout = None , <EOL> addressdetails = False , <EOL> language = False , <EOL> geometry = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( query , dict ) : <EOL> params = { <EOL> key : val <EOL> for key , val <EOL> in query . items ( ) <EOL> if key in self . structured_query_params <EOL> } <EOL> else : <EOL> params = { '<STR_LIT:q>' : self . format_string % query } <EOL> params . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> if self . view_box : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( self . view_box ) <EOL> if self . country_bias : <EOL> params [ '<STR_LIT>' ] = self . country_bias <EOL> if addressdetails : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> if language : <EOL> params [ '<STR_LIT>' ] = language <EOL> if geometry is not None : <EOL> geometry = geometry . lower ( ) <EOL> if geometry == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> elif geometry == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> elif geometry == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> elif geometry == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> else : <EOL> raise GeocoderQueryError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> url = "<STR_LIT:?>" . join ( ( self . api , urlencode ( params ) ) ) <EOL> logger . debug ( "<STR_LIT>" , self . __class__ . __name__ , url ) <EOL> return self . _parse_json ( <EOL> self . _call_geocoder ( url , timeout = timeout ) , exactly_one <EOL> ) <EOL> def reverse ( <EOL> self , <EOL> query , <EOL> exactly_one = True , <EOL> timeout = None , <EOL> language = False , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> lat , lon = [ <EOL> x . strip ( ) for x in <EOL> self . _coerce_point_to_string ( query ) . split ( '<STR_LIT:U+002C>' ) <EOL> ] <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> params = { <EOL> '<STR_LIT>' : lat , <EOL> '<STR_LIT>' : lon , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> if language : <EOL> params [ '<STR_LIT>' ] = language <EOL> url = "<STR_LIT:?>" . join ( ( self . reverse_api , urlencode ( params ) ) ) <EOL> logger . debug ( "<STR_LIT>" , self . __class__ . __name__ , url ) <EOL> return self . _parse_json ( <EOL> self . _call_geocoder ( url , timeout = timeout ) , exactly_one <EOL> ) <EOL> @ staticmethod <EOL> def parse_code ( place ) : <EOL> """<STR_LIT>""" <EOL> latitude = place . get ( '<STR_LIT>' , None ) <EOL> longitude = place . get ( '<STR_LIT>' , None ) <EOL> placename = place . get ( '<STR_LIT>' , None ) <EOL> if latitude and longitude : <EOL> latitude = float ( latitude ) <EOL> longitude = float ( longitude ) <EOL> return Location ( placename , ( latitude , longitude ) , place ) <EOL> def _parse_json ( self , places , exactly_one ) : <EOL> if places is None : <EOL> return None <EOL> if not isinstance ( places , list ) : <EOL> places = [ places ] <EOL> if not len ( places ) : <EOL> return None <EOL> if exactly_one is True : <EOL> return self . parse_code ( places [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return [ self . parse_code ( place ) for place in places ] </s>
