def geocoding(ctx, query, forward, include_headers, lat, lon, place_type, output, dataset, country): """This command returns places matching an address (forward mode) or places matching coordinates (reverse mode). In forward (the default) mode the query argument shall be an address such as '1600 pennsylvania ave nw'. $ mapbox geocoding '1600 pennsylvania ave nw' In reverse mode the query argument shall be a JSON encoded array of longitude and latitude (in that order) in decimal degrees. $ mapbox geocoding --reverse '[-77.4371, 37.5227]' An access token is required, see `mapbox --help`. """ verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 2 logger = logging.getLogger('mapbox') access_token = (ctx.obj and ctx.obj.get('access_token')) or None stdout = click.open_file(output, 'w') geocoder = mapbox.Geocoder(name=dataset, access_token=access_token) if forward: if country: country = [x.lower() for x in country.split(",")] for q in iter_query(query): try: resp = geocoder.forward( q, types=place_type, lat=lat, lon=lon, country=country) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if include_headers: echo_headers(resp.headers, file=stdout) if resp.status_code == 200: click.echo(resp.text, file=stdout) else: raise MapboxCLIException(resp.text.strip()) else: for lon, lat in map(coords_from_query, iter_query(query)): try: resp = geocoder.reverse(lon=lon, lat=lat, types=place_type) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if include_headers: echo_headers(resp.headers, file=stdout) if resp.status_code == 200: click.echo(resp.text, file=stdout) else: raise MapboxCLIException(resp.text.strip())
def geocoding(ctx, query, forward, include_headers, lat, lon, place_type, output, dataset, country, bbox, features, limit): """This command returns places matching an address (forward mode) or places matching coordinates (reverse mode). In forward (the default) mode the query argument shall be an address such as '1600 pennsylvania ave nw'. $ mapbox geocoding '1600 pennsylvania ave nw' In reverse mode the query argument shall be a JSON encoded array of longitude and latitude (in that order) in decimal degrees. $ mapbox geocoding --reverse '[-77.4371, 37.5227]' An access token is required, see `mapbox --help`. """ verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 2 logger = logging.getLogger('mapbox') access_token = (ctx.obj and ctx.obj.get('access_token')) or None stdout = click.open_file(output, 'w') geocoder = mapbox.Geocoder(name=dataset, access_token=access_token) if forward: if country: country = [x.lower() for x in country.split(",")] if bbox: try: bbox = tuple(map(float, bbox.split(','))) except ValueError: bbox = json.loads(bbox) for q in iter_query(query): try: resp = geocoder.forward( q, types=place_type, lat=lat, lon=lon, country=country, bbox=bbox, limit=limit) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if include_headers: echo_headers(resp.headers, file=stdout) if resp.status_code == 200: if features: collection = json.loads(resp.text) for feat in collection['features']: click.echo(json.dumps(feat), file=stdout) else: click.echo(resp.text, file=stdout) else: raise MapboxCLIException(resp.text.strip()) else: for lon, lat in map(coords_from_query, iter_query(query)): try: resp = geocoder.reverse( lon=lon, lat=lat, types=place_type, limit=limit) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if include_headers: echo_headers(resp.headers, file=stdout) if resp.status_code == 200: if features: collection = json.loads(resp.text) for feat in collection['features']: click.echo(json.dumps(feat), file=stdout) else: click.echo(resp.text, file=stdout) else: raise MapboxCLIException(resp.text.strip())