Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mapbox tiles interface #1170

Merged
merged 4 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ def __init__(self, access_token, map_id):
access_token
A valid Mapbox API access token.
map_id
A map ID for a publicly accessible map. This is the map whose
tiles will be retrieved through this process.
An ID for a publicly accessible map (provided by Mapbox).
This is the map whose tiles will be retrieved through this process.

"""
self.access_token = access_token
Expand All @@ -307,10 +307,55 @@ def __init__(self, access_token, map_id):

def _image_url(self, tile):
x, y, z = tile
url = ('https://api.tiles.mapbox.com/v4/{mapid}/{z}/{x}/{y}.png?'
'access_token={token}'.format(z=z, y=y, x=x,
mapid=self.map_id,
token=self.access_token))
url = ('https://api.mapbox.com/v4/mapbox.{id}/{z}/{x}/{y}.png'
'?access_token={token}'.format(z=z, y=y, x=x,
id=self.map_id,
token=self.access_token))
return url


class MapboxStyleTiles(GoogleTiles):
"""
Implement web tile retrieval from a user-defined Mapbox style. For more
details on Mapbox styles, see
https://www.mapbox.com/studio-manual/overview/map-styling/.

For terms of service, see https://www.mapbox.com/tos/.

"""
def __init__(self, access_token, username, map_id):
"""
Set up a new instance to retrieve tiles from a Mapbox style.

Access to Mapbox web services requires an access token and a map ID.
See https://www.mapbox.com/api-documentation/ for details.

Parameters
----------
access_token
A valid Mapbox API access token.
username
The username for the Mapbox user who defined the Mapbox style.
map_id
A map ID for a map defined by a Mapbox style. This is the map whose
tiles will be retrieved through this process. Note that this style
may be private and if your access token does not have permissions
to view this style, then map tile retrieval will fail.

"""
self.access_token = access_token
self.username = username
self.map_id = map_id
super(MapboxStyleTiles, self).__init__()

def _image_url(self, tile):
x, y, z = tile
url = ('https://api.mapbox.com/styles/v1/'
'{user}/{mapid}/tiles/256/{z}/{x}/{y}'
'?access_token={token}'.format(z=z, y=y, x=x,
user=self.username,
mapid=self.map_id,
token=self.access_token))
return url


Expand Down
21 changes: 18 additions & 3 deletions lib/cartopy/tests/test_img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,27 @@ def test_quadtree_wts():
KNOWN_EXTENTS[(8, 9, 4)])


def test_mapbox_tiles():
def test_mapbox_tiles_api_url():
token = 'foo'
map_name = 'bar'
tile = [0, 1, 2]
exp_url = ('https://api.mapbox.com/v4/mapbox.bar'
'/2/0/1.png?access_token=foo')

mapbox_sample = cimgt.MapboxTiles(token, map_name)
url_str = mapbox_sample._image_url(tile)
assert url_str == exp_url


def test_mapbox_style_tiles_api_url():
token = 'foo'
username = 'baz'
map_id = 'bar'
tile = [0, 1, 2]
exp_url = 'https://api.tiles.mapbox.com/v4/bar/2/0/1.png?access_token=foo'
exp_url = ('https://api.mapbox.com/styles/v1/'
'baz/bar/tiles/256/2/0/1'
'?access_token=foo')

mapbox_sample = cimgt.MapboxTiles(token, map_id)
mapbox_sample = cimgt.MapboxStyleTiles(token, username, map_id)
url_str = mapbox_sample._image_url(tile)
assert url_str == exp_url