From e9ed6f6bec074b43b374e8aeda144a91c33cf3d1 Mon Sep 17 00:00:00 2001 From: Marat Kopytjuk Date: Sun, 10 Jan 2021 19:26:29 +0100 Subject: [PATCH 1/6] implemente tiles for azure maps --- lib/cartopy/io/img_tiles.py | 33 ++++++++++++++++++++++++++ lib/cartopy/tests/test_img_tiles.py | 36 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index b048dbd4f..0d49fa0f8 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -680,3 +680,36 @@ def _merge_tiles(tiles): img[img_slice] = tile_img return img, [min(xs), max(xs), min(ys), max(ys)], 'lower' + + +class AzureMapsTiles(GoogleWTS): + + def __init__(self, subscription_key, tileset_id = "microsoft.imagery", api_version = "2.0", + desired_tile_form='RGB'): + """ + Set up a new instance to retrieve tiles from Azure Maps. + + Access to Azure Maps REST API requires an subscription key. + See https://docs.microsoft.com/en-us/azure/azure-maps/azure-maps-authentication#shared-key-authentication/ for details. + + Parameters + ---------- + subscription_key + A valid Azure Maps subscription key. + username + The username for the Mapbox user who defined the Mapbox style. + tileset_id + A tileset ID for a map defined by a Mapbox style. See + https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details + api_version + API version to use. Defaults to 2.0 as recommended by Microsoft. + + """ + super().__init__(desired_tile_form=desired_tile_form) + self.subscription_key = subscription_key + self.tileset_id = tileset_id + self.api_version = api_version + + def _image_url(self, tile): + return ('https://atlas.microsoft.com/map/tile?api-version={self.api_version}&tilesetId={self.tileset_id}&x={x}&y={y}&zoom={z}&subscription-key={self.subscription_key}' + .format(self=self, x=tile[0], y=tile[1], z=tile[2])) diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index a4f5d032a..26a72d8a1 100644 --- a/lib/cartopy/tests/test_img_tiles.py +++ b/lib/cartopy/tests/test_img_tiles.py @@ -290,6 +290,42 @@ def test_ordnance_survey_get_image(): assert extent1 == extent2 +def test_azuremaps_tiles_api_url(): + subscription_key = 'foo' + tileset_id = 'bar' + tile = [0, 1, 2] + + exp_url = ('https://atlas.microsoft.com/map/tile?api-version=2.0&tilesetId=bar&x=0&y=1&zoom=2&subscription-key=foo') + + az_maps_sample = cimgt.AzureMapsTiles(subscription_key, tileset_id) + url_str = az_maps_sample._image_url(tile) + assert url_str == exp_url + + +@pytest.mark.network +def test_azuremaps_get_image(): + # In order to test fetching map images from OS + # an API key needs to be provided + try: + api_key = os.environ['AZURE_MAPS_SUBSCRIPTION_KEY'] + except KeyError: + pytest.skip('AZURE_MAPS_SUBSCRIPTION_KEY environment variable is unset.') + + am1 = cimgt.AzureMapsTiles(api_key, tileset_id="microsoft.imagery") + am2 = cimgt.AzureMapsTiles(api_key, tileset_id="microsoft.base.road") + + tile = (500, 300, 10) + + img1, extent1, _ = am1.get_image(tile) + img2, extent2, _ = am2.get_image(tile) + + # Different images for different layers + assert img1 != img2 + + # The extent is the same though + assert extent1 == extent2 + + @pytest.mark.network @pytest.mark.parametrize('cache_dir', ["tmpdir", True, False]) def test_cache(cache_dir, tmpdir): From 14cb5dd08164384b9fdd80500f44d7dcea68d682 Mon Sep 17 00:00:00 2001 From: Marat Kopytjuk Date: Sun, 10 Jan 2021 19:40:05 +0100 Subject: [PATCH 2/6] Fix linting and prettify --- lib/cartopy/io/img_tiles.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 0d49fa0f8..97ad7a20f 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -684,8 +684,8 @@ def _merge_tiles(tiles): class AzureMapsTiles(GoogleWTS): - def __init__(self, subscription_key, tileset_id = "microsoft.imagery", api_version = "2.0", - desired_tile_form='RGB'): + def __init__(self, subscription_key, tileset_id="microsoft.imagery", api_version="2.0", + desired_tile_form='RGB', cache=False): """ Set up a new instance to retrieve tiles from Azure Maps. @@ -700,16 +700,20 @@ def __init__(self, subscription_key, tileset_id = "microsoft.imagery", api_versi The username for the Mapbox user who defined the Mapbox style. tileset_id A tileset ID for a map defined by a Mapbox style. See - https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details + https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid + for details api_version API version to use. Defaults to 2.0 as recommended by Microsoft. """ - super().__init__(desired_tile_form=desired_tile_form) + super().__init__(desired_tile_form=desired_tile_form, cache=cache) self.subscription_key = subscription_key self.tileset_id = tileset_id self.api_version = api_version def _image_url(self, tile): - return ('https://atlas.microsoft.com/map/tile?api-version={self.api_version}&tilesetId={self.tileset_id}&x={x}&y={y}&zoom={z}&subscription-key={self.subscription_key}' - .format(self=self, x=tile[0], y=tile[1], z=tile[2])) + url = ('https://atlas.microsoft.com/map/tile?' + 'api-version={self.api_version}&tilesetId={self.tileset_id}' + '&x={x}&y={y}&zoom={z}&' + 'subscription-key={self.subscription_key}') + return url.format(self=self, x=tile[0], y=tile[1], z=tile[2]) From bb762b524e491cbb76cea44115d1e88fa6aae108 Mon Sep 17 00:00:00 2001 From: Marat Kopytjuk Date: Sun, 10 Jan 2021 19:42:29 +0100 Subject: [PATCH 3/6] Satisfy linter for python code --- lib/cartopy/io/img_tiles.py | 2 +- lib/cartopy/tests/test_img_tiles.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 97ad7a20f..0970e4e1a 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -699,7 +699,7 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", api_version username The username for the Mapbox user who defined the Mapbox style. tileset_id - A tileset ID for a map defined by a Mapbox style. See + A tileset ID for a map defined by a Mapbox style. See https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details api_version diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index 26a72d8a1..efc14cb41 100644 --- a/lib/cartopy/tests/test_img_tiles.py +++ b/lib/cartopy/tests/test_img_tiles.py @@ -295,7 +295,8 @@ def test_azuremaps_tiles_api_url(): tileset_id = 'bar' tile = [0, 1, 2] - exp_url = ('https://atlas.microsoft.com/map/tile?api-version=2.0&tilesetId=bar&x=0&y=1&zoom=2&subscription-key=foo') + exp_url = ('https://atlas.microsoft.com/map/tile?api-version=2.0' + '&tilesetId=bar&x=0&y=1&zoom=2&subscription-key=foo') az_maps_sample = cimgt.AzureMapsTiles(subscription_key, tileset_id) url_str = az_maps_sample._image_url(tile) @@ -309,7 +310,8 @@ def test_azuremaps_get_image(): try: api_key = os.environ['AZURE_MAPS_SUBSCRIPTION_KEY'] except KeyError: - pytest.skip('AZURE_MAPS_SUBSCRIPTION_KEY environment variable is unset.') + pytest.skip('AZURE_MAPS_SUBSCRIPTION_KEY environment variable ' + 'is unset.') am1 = cimgt.AzureMapsTiles(api_key, tileset_id="microsoft.imagery") am2 = cimgt.AzureMapsTiles(api_key, tileset_id="microsoft.base.road") From c6136d9b471370609bb02ebfe163633f7b1b08cd Mon Sep 17 00:00:00 2001 From: Marat Kopytjuk Date: Sun, 10 Jan 2021 19:48:40 +0100 Subject: [PATCH 4/6] Remove mapbox strings --- lib/cartopy/io/img_tiles.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 0970e4e1a..2253578d5 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -684,8 +684,8 @@ def _merge_tiles(tiles): class AzureMapsTiles(GoogleWTS): - def __init__(self, subscription_key, tileset_id="microsoft.imagery", api_version="2.0", - desired_tile_form='RGB', cache=False): + def __init__(self, subscription_key, tileset_id="microsoft.imagery", + api_version="2.0", desired_tile_form='RGB', cache=False): """ Set up a new instance to retrieve tiles from Azure Maps. @@ -696,12 +696,9 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", api_version ---------- subscription_key A valid Azure Maps subscription key. - username - The username for the Mapbox user who defined the Mapbox style. tileset_id - A tileset ID for a map defined by a Mapbox style. See - https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid - for details + A tileset ID for a map. See + https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details. api_version API version to use. Defaults to 2.0 as recommended by Microsoft. From b671bbba347ffee6ff142caa441799869a916fa8 Mon Sep 17 00:00:00 2001 From: Marat Kopytjuk Date: Sun, 10 Jan 2021 20:22:25 +0100 Subject: [PATCH 5/6] Fix linting --- lib/cartopy/io/img_tiles.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 2253578d5..69eecc990 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -690,7 +690,8 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", Set up a new instance to retrieve tiles from Azure Maps. Access to Azure Maps REST API requires an subscription key. - See https://docs.microsoft.com/en-us/azure/azure-maps/azure-maps-authentication#shared-key-authentication/ for details. + See https://docs.microsoft.com/en-us/azure/azure-maps/azure-maps-authentication#shared-key-authentication/ # noqa: E501 + for details. Parameters ---------- @@ -698,7 +699,7 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", A valid Azure Maps subscription key. tileset_id A tileset ID for a map. See - https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details. + https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details. # noqa: E501 api_version API version to use. Defaults to 2.0 as recommended by Microsoft. From cfea1da4afcea385fa8daf8f1122ab146e313ff9 Mon Sep 17 00:00:00 2001 From: Marat K Date: Mon, 18 Jan 2021 17:06:06 +0100 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Elliott Sales de Andrade --- lib/cartopy/io/img_tiles.py | 5 +++-- lib/cartopy/tests/test_img_tiles.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 69eecc990..7d201d0c4 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -689,7 +689,7 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", """ Set up a new instance to retrieve tiles from Azure Maps. - Access to Azure Maps REST API requires an subscription key. + Access to Azure Maps REST API requires a subscription key. See https://docs.microsoft.com/en-us/azure/azure-maps/azure-maps-authentication#shared-key-authentication/ # noqa: E501 for details. @@ -699,7 +699,8 @@ def __init__(self, subscription_key, tileset_id="microsoft.imagery", A valid Azure Maps subscription key. tileset_id A tileset ID for a map. See - https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid for details. # noqa: E501 + https://docs.microsoft.com/en-us/rest/api/maps/renderv2/getmaptilepreview#tilesetid # noqa: E501 + for details. api_version API version to use. Defaults to 2.0 as recommended by Microsoft. diff --git a/lib/cartopy/tests/test_img_tiles.py b/lib/cartopy/tests/test_img_tiles.py index efc14cb41..c5e292ab5 100644 --- a/lib/cartopy/tests/test_img_tiles.py +++ b/lib/cartopy/tests/test_img_tiles.py @@ -305,7 +305,7 @@ def test_azuremaps_tiles_api_url(): @pytest.mark.network def test_azuremaps_get_image(): - # In order to test fetching map images from OS + # In order to test fetching map images from Azure Maps # an API key needs to be provided try: api_key = os.environ['AZURE_MAPS_SUBSCRIPTION_KEY']