From b03baa90d63792a7dcf895c4ff65fce2642a4161 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Tue, 6 Nov 2018 11:46:12 +0000 Subject: [PATCH 1/4] Update mapbox tiles interface --- lib/cartopy/io/img_tiles.py | 61 ++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 5094be204..3b1fa969e 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -285,7 +285,7 @@ class MapboxTiles(GoogleTiles): For terms of service, see https://www.mapbox.com/tos/. """ - def __init__(self, access_token, map_id): + def __init__(self, access_token, map_name): """ Set up a new Mapbox tiles instance. @@ -296,21 +296,66 @@ def __init__(self, access_token, map_id): ---------- access_token A valid Mapbox API access token. + map_name + A map name 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 + self.map_name = map_name + super(MapboxTiles, self).__init__() + + def _image_url(self, tile): + x, y, z = tile + url = ('https://api.mapbox.com/v4/mapbox.{name}/{z}/{x}/{y}.png' + '?access_token={token}'.format(z=z, y=y, x=x, + name=self.map_name, + 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 publicly accessible map. This is the map whose - tiles will be retrieved through this process. + 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(MapboxTiles, self).__init__() + super(MapboxStyleTiles, self).__init__() 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/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 From 6eb618000d0fb4b629f8b3398013913ae92dac7e Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Thu, 8 Nov 2018 16:33:26 +0000 Subject: [PATCH 2/4] Update tests --- lib/cartopy/tests/test_img_tiles.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index e3ec56566..29b849edc 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.map_name' + '/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/' + 'username/map_id/tiles/256/2/0/1' + '?access_token=token') - 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 From 4275cd4fd30302a9f6b5090175e52a00fd8b6937 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Mon, 12 Nov 2018 10:31:06 +0000 Subject: [PATCH 3/4] Revert breaking API change --- lib/cartopy/io/img_tiles.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 3b1fa969e..74ce7e0a9 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -285,7 +285,7 @@ class MapboxTiles(GoogleTiles): For terms of service, see https://www.mapbox.com/tos/. """ - def __init__(self, access_token, map_name): + def __init__(self, access_token, map_id): """ Set up a new Mapbox tiles instance. @@ -296,20 +296,20 @@ def __init__(self, access_token, map_name): ---------- access_token A valid Mapbox API access token. - map_name - A map name for a publicly accessible map (provided by Mapbox). + map_id + 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 - self.map_name = map_name + self.map_id = map_id super(MapboxTiles, self).__init__() def _image_url(self, tile): x, y, z = tile - url = ('https://api.mapbox.com/v4/mapbox.{name}/{z}/{x}/{y}.png' + url = ('https://api.mapbox.com/v4/mapbox.{id}/{z}/{x}/{y}.png' '?access_token={token}'.format(z=z, y=y, x=x, - name=self.map_name, + id=self.map_id, token=self.access_token)) return url From 90b2ca15172a8da8d7541c3faf27af4bc8995108 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Mon, 12 Nov 2018 11:16:04 +0000 Subject: [PATCH 4/4] Correct expected API URLs --- lib/cartopy/tests/test_img_tiles.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index 29b849edc..d70aa5b75 100644 --- a/lib/cartopy/tests/test_img_tiles.py +++ b/lib/cartopy/tests/test_img_tiles.py @@ -220,7 +220,7 @@ def test_mapbox_tiles_api_url(): token = 'foo' map_name = 'bar' tile = [0, 1, 2] - exp_url = ('https://api.mapbox.com/v4/mapbox.map_name' + exp_url = ('https://api.mapbox.com/v4/mapbox.bar' '/2/0/1.png?access_token=foo') mapbox_sample = cimgt.MapboxTiles(token, map_name) @@ -234,8 +234,8 @@ def test_mapbox_style_tiles_api_url(): map_id = 'bar' tile = [0, 1, 2] exp_url = ('https://api.mapbox.com/styles/v1/' - 'username/map_id/tiles/256/2/0/1' - '?access_token=token') + 'baz/bar/tiles/256/2/0/1' + '?access_token=foo') mapbox_sample = cimgt.MapboxStyleTiles(token, username, map_id) url_str = mapbox_sample._image_url(tile)