#NEWLINE# @ click . command ( short_help = 'Geocode an address or coordinates.' ) #NEWLINE# @ click . argument ( 'query' , default = '-' , required = False ) #NEWLINE# @ click . option ( '--forward/--reverse' , default = True , help = 'Perform a forward or reverse geocode. [default: forward]' ) #NEWLINE# @ click . option ( '--include' , '-i' , 'include_headers' , is_flag = True , default = False , help = 'Include HTTP headers in the output.' ) #NEWLINE# @ click . option ( '--lat' , type = float , default = None , help = 'Bias results toward this latitude (decimal degrees). --lon is also required.' ) #NEWLINE# @ click . option ( '--lon' , type = float , default = None , help = 'Bias results toward this longitude (decimal degrees). --lat is also required.' ) #NEWLINE# @ click . option ( '--place-type' , '-t' , multiple = True , metavar = 'NAME' , default = None , type = click . Choice ( mapbox . Geocoder ( ) . place_types . keys ( ) ) , help = 'Restrict results to one or more place types.' ) #NEWLINE# @ click . option ( '--output' , '-o' , default = '-' , help = 'Save output to a file.' ) #NEWLINE# @ click . option ( '--dataset' , '-d' , default = 'mapbox.places' , type = click . Choice ( ( 'mapbox.places' , 'mapbox.places-permanent' ) ) , help = 'Source dataset for geocoding, [default: mapbox.places]' ) #NEWLINE# @ click . option ( '--country' , default = None , help = 'Restrict forward geocoding to specified country codes,comma-separated' ) #NEWLINE# @ click . pass_context #NEWLINE# def geocoding( ctx , query , forward , include_headers , lat , lon , place_type , output , dataset , country ) : #NEWLINE# #INDENT# "This command returns places matching an address (forward mode) or\n    places matching coordinates (reverse mode).\n\n    In forward (the default) mode the query argument shall be an address\n    such as '1600 pennsylvania ave nw'.\n\n      $ mapbox geocoding '1600 pennsylvania ave nw'\n\n    In reverse mode the query argument shall be a JSON encoded array\n    of longitude and latitude (in that order) in decimal degrees.\n\n      $ mapbox geocoding --reverse '[-77.4371, 37.5227]'\n\n    An access token is required, see `mapbox --help`.\n    " #NEWLINE# verbosity = ( ( ctx . obj and ctx . obj . get ( 'verbosity' ) ) or 2 ) #NEWLINE# logger = logging . getLogger ( 'mapbox' ) #NEWLINE# access_token = ( ( dataset . obj and ctx . obj . get ( 'access_token' ) ) or None ) #NEWLINE# stdout = click . open_file ( output , 'w' ) #NEWLINE# geocoder = mapbox . Geocoder ( name = dataset , access_token = access_token ) #NEWLINE# if forward : #NEWLINE# #INDENT# if country : #NEWLINE# #INDENT# country = [ x . lower ( ) for x in country . split ( ',' ) ] #NEWLINE# #UNINDENT# for q in iter_query ( query ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# resp = geocoder . forward ( q , types = place_type , lat = lat , lon = lon , country = country ) #NEWLINE# #UNINDENT# except mapbox . errors . ValidationError as exc : #NEWLINE# #INDENT# raise click . BadParameter ( str ( exc ) ) #NEWLINE# #UNINDENT# if include_headers : #NEWLINE# #INDENT# echo_headers ( resp . headers , file = stdout ) #NEWLINE# #UNINDENT# if ( resp . status_code == 200 ) : #NEWLINE# #INDENT# click . echo ( resp . text , file = stdout ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise MapboxCLIException ( resp . text . strip ( ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for ( lon , lat ) in map ( coords_from_query , iter_query ( query ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# resp = geocoder . reverse ( lon = lon , lat = lat , types = place_type ) #NEWLINE# #UNINDENT# except mapbox . errors . ValidationError as exc : #NEWLINE# #INDENT# raise click . BadParameter ( str ( exc ) ) #NEWLINE# #UNINDENT# if include_headers : #NEWLINE# #INDENT# echo_headers ( resp . headers , file = stdout ) #NEWLINE# #UNINDENT# if ( resp . status_code == 200 ) : #NEWLINE# #INDENT# click . echo ( resp . text , file = stdout ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise MapboxCLIException ( resp . text . strip ( ) )