Skip to content

Commit

Permalink
Updated Ordnance Survey img_tile to new OS API (#2105)
Browse files Browse the repository at this point in the history
* Updated Ordnance Survey provider in img_tiles.py
  • Loading branch information
dchirst authored Nov 30, 2022
1 parent 3400f31 commit 397cbe0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
24 changes: 12 additions & 12 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,20 @@ class OrdnanceSurvey(GoogleWTS):
"""
Implement web tile retrieval from Ordnance Survey map data.
To use this tile image source you will need to obtain an
API key from Ordnance Survey.
API key from Ordnance Survey. You can get a free API key from
https://osdatahub.os.uk
For more details on Ordnance Survey layer styles, see
https://apidocs.os.uk/docs/map-styles.
https://osdatahub.os.uk/docs/wmts/technicalSpecification.
For the API framework agreement, see
https://developer.ordnancesurvey.co.uk/os-api-framework-agreement.
https://osdatahub.os.uk/legal/apiTermsConditions.
"""
# API Documentation: https://apidocs.os.uk/docs/os-maps-wmts
# API Documentation: https://osdatahub.os.uk/docs/wmts/overview

def __init__(self,
apikey,
layer='Road',
layer='Road_3857',
desired_tile_form='RGB',
cache=False):
"""
Expand All @@ -577,19 +578,18 @@ def __init__(self,
cache=cache)
self.apikey = apikey

if layer not in ['Outdoor', 'Road', 'Light', 'Night', 'Leisure']:
if layer not in ("Road_3857", "Outdoor_3857", "Light_3857",
"Road", "Outdoor", "Light"):
raise ValueError(f'Invalid layer {layer}')
elif layer in ("Road", "Outdoor", "Light"):
layer += "_3857"

self.layer = layer

def _image_url(self, tile):
x, y, z = tile
return (
f'https://api2.ordnancesurvey.co.uk/mapping_api/v1/service/wmts?'
f'key={self.apikey}&height=256&width=256&version=1.0.0&'
f'tilematrixSet=EPSG%3A3857&style=true&layer={self.layer}%203857&'
f'SERVICE=WMTS&REQUEST=GetTile&format=image%2Fpng&'
f'TileMatrix=EPSG%3A3857%3A{z}&TileRow={y}&TileCol={x}')
return f"https://api.os.uk/maps/raster/v1/zxy/" \
f"{self.layer}/{z}/{x}/{y}.png?key={self.apikey}"


def _merge_tiles(tiles):
Expand Down
30 changes: 14 additions & 16 deletions lib/cartopy/tests/test_img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def test_google_wts():
with pytest.raises(AssertionError):
list(gt.find_images(target_domain, -1))
assert (tuple(gt.find_images(target_domain, 0)) ==
((0, 0, 0),))
((0, 0, 0),))
assert (tuple(gt.find_images(target_domain, 2)) ==
((1, 1, 2), (2, 1, 2)))
((1, 1, 2), (2, 1, 2)))

assert (list(gt.subtiles((0, 0, 0))) ==
[(0, 0, 1), (0, 1, 1), (1, 0, 1), (1, 1, 1)])
Expand Down Expand Up @@ -222,23 +222,21 @@ def test_ordnance_survey_tile_styles():
"""
dummy_apikey = "None"

ref_url = ('https://api2.ordnancesurvey.co.uk/'
'mapping_api/v1/service/wmts?'
'key=None&height=256&width=256&version=1.0.0&'
'tilematrixSet=EPSG%3A3857&style=true&layer={layer}%203857&'
'SERVICE=WMTS&REQUEST=GetTile&format=image%2Fpng&'
'TileMatrix=EPSG%3A3857%3A{z}&TileRow={y}&TileCol={x}')
ref_url = "https://api.os.uk/maps/raster/v1/zxy/" \
"{layer}/{z}/{x}/{y}.png?key=None"
tile = ["1", "2", "3"]

# Default is Road.
# Default is Road_3857.
os = cimgt.OrdnanceSurvey(dummy_apikey)
url = os._image_url(tile)
assert url == ref_url.format(layer="Road",
assert url == ref_url.format(layer="Road_3857",
z=tile[2], y=tile[1], x=tile[0])

for layer in ['Outdoor', 'Light', 'Night', 'Leisure']:
for layer in ("Road_3857", "Light_3857", "Outdoor_3857",
"Road", "Light", "Outdoor"):
os = cimgt.OrdnanceSurvey(dummy_apikey, layer=layer)
url = os._image_url(tile)
layer = layer if layer.endswith("_3857") else layer + "_3857"
assert url == ref_url.format(layer=layer,
z=tile[2], y=tile[1], x=tile[0])

Expand All @@ -256,8 +254,8 @@ def test_ordnance_survey_get_image():
except KeyError:
pytest.skip('ORDNANCE_SURVEY_API_KEY environment variable is unset.')

os1 = cimgt.OrdnanceSurvey(api_key, layer="Outdoor")
os2 = cimgt.OrdnanceSurvey(api_key, layer="Night")
os1 = cimgt.OrdnanceSurvey(api_key, layer="Outdoor_3857")
os2 = cimgt.OrdnanceSurvey(api_key, layer="Light_3857")

tile = (500, 300, 10)

Expand Down Expand Up @@ -368,9 +366,9 @@ def test_cache(cache_dir, tmp_path):
files = [i for i in os.listdir(cache_dir_res)]
hashes = {
f:
hashlib.md5(
np.load(os.path.join(cache_dir_res, f), allow_pickle=True).data
).hexdigest()
hashlib.md5(
np.load(os.path.join(cache_dir_res, f), allow_pickle=True).data
).hexdigest()
for f in files
}

Expand Down

0 comments on commit 397cbe0

Please sign in to comment.