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

Updated Ordnance Survey img_tile to new OS API #2105

Merged
merged 9 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
15 changes: 7 additions & 8 deletions lib/cartopy/io/img_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ class OrdnanceSurvey(GoogleWTS):

def __init__(self,
apikey,
layer='Road',
layer='Road_3857',
desired_tile_form='RGB',
cache=False):
"""
Expand All @@ -577,19 +577,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"
dchirst marked this conversation as resolved.
Show resolved Hide resolved

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/" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like they do still have the WMTS endpoint with query parameters if you want to use that still?
https://api.os.uk/maps/raster/v1/wmts?request=getcapabilities&key=
from: https://osdatahub.os.uk/docs/wmts/technicalSpecification

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the WMTS endpoint is still available. I thought the zxy endpoint was a bit more concise, so I went with that instead. Do you think it'd be better to keep it as a WMTS endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greglucas just following up on this - would a WMTS be better than a ZXY endpoint?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I don't think we need to at this point. If you or someone else wants more flexibility in the future that can be a follow-up PR.

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