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

Implement tiles for Azure Maps #1709

Merged
merged 6 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 34 additions & 0 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,37 @@ 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', cache=False):
"""
Set up a new instance to retrieve tiles from Azure Maps.

Access to Azure Maps REST API requires an subscription key.
kopytjuk marked this conversation as resolved.
Show resolved Hide resolved
See https://docs.microsoft.com/en-us/azure/azure-maps/azure-maps-authentication#shared-key-authentication/ for details.
kopytjuk marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
subscription_key
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.
kopytjuk marked this conversation as resolved.
Show resolved Hide resolved
api_version
API version to use. Defaults to 2.0 as recommended by Microsoft.

"""
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):
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])
38 changes: 38 additions & 0 deletions lib/cartopy/tests/test_img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,44 @@ 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
kopytjuk marked this conversation as resolved.
Show resolved Hide resolved
# 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):
Expand Down