diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 5094be204..74ce7e0a9 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -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 @@ -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 diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index e3ec56566..d70aa5b75 100644 --- a/lib/cartopy/tests/test_img_tiles.py +++ b/lib/cartopy/tests/test_img_tiles.py @@ -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