def surface(ctx, mapid, layer, fields, features, zoom, interpolate, geojson, output): """Mapbox Surface API enables flexible querying of data stored in vector tiles on Mapbox, to create results like elevation profiles. $ mapbox surface mapbox.mapbox-terrain-v1 contour ele \\ \b "[-122.781, 45.528]" "[-122.716, 45.525]" An access token is required, see `mapbox --help`. """ stdout = click.open_file(output, 'w') access_token = (ctx.obj and ctx.obj.get('access_token')) or None fields = fields.split(",") service = mapbox.Surface(access_token=access_token) try: res = service.surface(features, mapid=mapid, layer=layer, fields=fields, geojson=geojson, interpolate=interpolate, zoom=zoom) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if res.status_code == 200: if geojson: click.echo(json.dumps(res.geojson()), file=stdout) else: click.echo(res.text, file=stdout) else: raise MapboxCLIException(res.text.strip())
def test_surface(): body = """{"results":[{"id":0,"latlng":{"lat":36.05322,"lng":-112.084004},"ele":2186.361304424316},{"id":1,"latlng":{"lat":36.053573,"lng":-112.083914},"ele":2187.6233827411997},{"id":2,"latlng":{"lat":36.053845,"lng":-112.083965},"ele":2163.921475128245}],"attribution":"<a href='https://www.mapbox.com/about/maps/' target='_blank'>&copy; Mapbox</a>"}""" responses.add( responses.GET, 'https://api.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?access_token=pk.test&points=-112.084004%2C36.053220%3B-112.083914%2C36.053573%3B-112.083965%2C36.053845&geojson=true&fields=ele&layer=contour', match_querystring=True, body=body, status=200, content_type='application/json') res = mapbox.Surface(access_token='pk.test').surface(points) assert res.status_code == 200
def test_surface_geojson(): body = """{"results":{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.084004,36.05322]},"properties":{"id":0,"ele":2186.361304424316}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.083914,36.053573]},"properties":{"id":1,"ele":2187.6233827411997}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-112.083965,36.053845]},"properties":{"id":2,"ele":2163.921475128245}}]},"attribution":"<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox</a>"}""" responses.add( responses.GET, 'https://api.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?access_token=pk.test&fields=ele&layer=contour&geojson=true&points=-112.084004%2C36.053220%3B-112.083914%2C36.053573%3B-112.083965%2C36.053845', match_querystring=True, body=body, status=200, content_type='application/json') res = mapbox.Surface(access_token='pk.test').surface(points) fc = res.geojson() assert fc['type'] == 'FeatureCollection' assert len(fc['features']) == 3
def test_surface_params(): params = "&encoded_polyline=~kbkTss%60%7BEQeAHu%40&zoom=16&interpolate=false" responses.add( responses.GET, 'https://api.mapbox.com/v4/surface/mapbox.mapbox-terrain-v1.json?access_token=pk.test&fields=ele&layer=contour&geojson=true' + params, match_querystring=True, body="just testing URI templating", status=200, content_type='application/json') res = mapbox.Surface(access_token='pk.test').surface(points, zoom=16, polyline=True, interpolate=False) assert res.status_code == 200
def test_class_attrs(): """Get expected class attr values""" serv = mapbox.Surface() assert serv.api_name == 'surface' assert serv.api_version == 'v4'