Skip to content

img_tiles.py: Add Defaults 'RGBA' for 'only_streets' - #2728

Open
DavidVadnais wants to merge 1 commit into
SciTools:mainfrom
DavidVadnais:fix-762-GoogleTiles-transparency
Open

img_tiles.py: Add Defaults 'RGBA' for 'only_streets'#2728
DavidVadnais wants to merge 1 commit into
SciTools:mainfrom
DavidVadnais:fix-762-GoogleTiles-transparency

Conversation

@DavidVadnais

@DavidVadnais DavidVadnais commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Rationale

Fixes: #762

Code to exercise change:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import img_tiles

tiles_sat = img_tiles.GoogleTiles(style='satellite')
tiles_str = img_tiles.GoogleTiles(style='only_streets')

ax = plt.axes(projection=tiles_sat.crs)
ax.set_extent([31, 35.5, 34, 36], ccrs.Geodetic())
ax.add_image(tiles_sat, 8)
ax.add_image(tiles_str, 8)
plt.title('Cyprus - Hybrid (satellite + streets overlay)')
plt.show()

Output before change

image

Output after change

image

Implications

With this setup it only affects when style='only_streets'. It might be cleaner to just change the default itself to RGBA like below:

....
class GoogleTiles(GoogleWTS):
    def __init__(self,
                 desired_tile_form='RGBA',
                 style="street",
                 url=('https://mts0.google.com/vt/lyrs={style}'
...

Let me know if this is your preference.

It does look like for stadia the just set all defaults to RGBA #2269

@DavidVadnais
DavidVadnais force-pushed the fix-762-GoogleTiles-transparency branch from d0d5776 to 850e86a Compare September 4, 2026 06:32
@DavidVadnais

Copy link
Copy Markdown
Contributor Author

😢 I think this is a rerun just like #2727 unless I'm doing something wrong?

@greglucas

Copy link
Copy Markdown
Contributor

With this setup it only affects when style='only_streets'. It might be cleaner to just change the default itself to RGBA like below:

I think the satellite images and base layers are only opaque RGB, do you know if there is extra overhead (all 1s in alpha channel) if we request RGBA instead or does it still return RGB to us and so it doesn't really matter which request we submit?

@DavidVadnais

Copy link
Copy Markdown
Contributor Author

Google's tile server returns the same PNG/JPEG either way and desired_tile_form controls how Cartopy converts the fetched image via PIL. So requesting RGBA for an opaque tile just adds an alpha channel without network cost. I believe this based on the below code extract:

lib/cartopy/io/img_tiles.py line 256ish:

fh = urlopen(request)
im_data = io.BytesIO(fh.read())
fh.close()
img = Image.open(im_data)

...

img = img.convert(self.desired_tile_form)

I think changing the class default to RGBA (like Stadia does) is cleaner so all styles work correctly without special-casing but I'm new to this code base so I don't know all the possible knock-on consequences.

@greglucas

greglucas commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

OK, so it doesn't request anything differently which is good. But the issue with opening everything as RGBA is that you then add an extra 33% in memory usage unnecessarily, so I think the None and then selecting defaults if not specified makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transparencey issue using hybrid style from GoogleTiles

2 participants