From f706d4d84376747e29493b6a014245cd60401ecb Mon Sep 17 00:00:00 2001 From: lgolston <30876419+lgolston@users.noreply.github.com> Date: Wed, 28 Jun 2023 22:49:03 -0500 Subject: [PATCH 1/3] allow variable Stamen file extension, opacity --- lib/cartopy/io/img_tiles.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 6b6dc8c3e..d34ab77ea 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -351,13 +351,39 @@ class Stamen(GoogleWTS): def __init__(self, style='toner', desired_tile_form='RGB', cache=False): + + # preset layer configuration + layer_config = { + 'terrain': {'extension': 'png', 'opaque': True}, + 'terrain-background': {'extension': 'png', 'opaque': True}, + 'terrain-labels': {'extension': 'png', 'opaque': False}, + 'terrain-lines': {'extension': 'png', 'opaque': False}, + 'toner-background': {'extension': 'png', 'opaque': True}, + 'toner': {'extension': 'png', 'opaque': True}, + 'toner-hybrid': {'extension': 'png', 'opaque': False}, + 'toner-labels': {'extension': 'png', 'opaque': False}, + 'toner-lines': {'extension': 'png', 'opaque': False}, + 'toner-lite': {'extension': 'png', 'opaque': True}, + 'watercolor': {'extension': 'jpg', 'opaque': True}, + } + + # get layer information from dict if known, else use defaults + layer_info = layer_config.get( + style, {'extension': '.png', 'opaque': True}) + + # allow background transparency + if not layer_info['opaque']: + desired_tile_form = 'RGBA' + super().__init__(desired_tile_form=desired_tile_form, cache=cache) self.style = style + self.extension = layer_info['extension'] def _image_url(self, tile): x, y, z = tile - return f'http://tile.stamen.com/{self.style}/{z}/{x}/{y}.png' + return 'http://tile.stamen.com/' + \ + f'{self.style}/{z}/{x}/{y}.{self.extension}' class MapboxTiles(GoogleWTS): From 2ae752839ec578a9cc7ad9648346f23ce7245105 Mon Sep 17 00:00:00 2001 From: lgolston <30876419+lgolston@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:54:10 -0500 Subject: [PATCH 2/3] use user desired_tile_form if given --- lib/cartopy/io/img_tiles.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index d34ab77ea..276a6e53c 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -350,7 +350,7 @@ class Stamen(GoogleWTS): """ def __init__(self, style='toner', - desired_tile_form='RGB', cache=False): + desired_tile_form=None, cache=False): # preset layer configuration layer_config = { @@ -367,13 +367,17 @@ def __init__(self, style='toner', 'watercolor': {'extension': 'jpg', 'opaque': True}, } - # get layer information from dict if known, else use defaults + # get layer information from dict layer_info = layer_config.get( style, {'extension': '.png', 'opaque': True}) - # allow background transparency - if not layer_info['opaque']: - desired_tile_form = 'RGBA' + # use optional desired_tile_form input if available + # otherwise, use preset value based on the layer name + if desired_tile_form is None: + if not layer_info['opaque']: + desired_tile_form = 'RGBA' + else: + desired_tile_form = 'RGB' super().__init__(desired_tile_form=desired_tile_form, cache=cache) From 44657a3628c25825c946ec88afafc5314c58c5af Mon Sep 17 00:00:00 2001 From: lgolston <30876419+lgolston@users.noreply.github.com> Date: Thu, 29 Jun 2023 16:10:38 -0500 Subject: [PATCH 3/3] simplify if-else statement Co-authored-by: Ryan May --- lib/cartopy/io/img_tiles.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cartopy/io/img_tiles.py b/lib/cartopy/io/img_tiles.py index 276a6e53c..76b939d65 100644 --- a/lib/cartopy/io/img_tiles.py +++ b/lib/cartopy/io/img_tiles.py @@ -374,10 +374,10 @@ def __init__(self, style='toner', # use optional desired_tile_form input if available # otherwise, use preset value based on the layer name if desired_tile_form is None: - if not layer_info['opaque']: - desired_tile_form = 'RGBA' - else: + if layer_info['opaque']: desired_tile_form = 'RGB' + else: + desired_tile_form = 'RGBA' super().__init__(desired_tile_form=desired_tile_form, cache=cache)