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

Exporting chart as PNG does not include map tiles when a provider is specified #38

Closed
tillnagel opened this issue Apr 12, 2024 · 9 comments

Comments

@tillnagel
Copy link

tillnagel commented Apr 12, 2024

When using (the currently only supported vl-convert) to save a chart including altair-tiles with my_chart.save('chart.png') the tiles are not shown / rendered in the resulting image.

Using the "Save as PNG" in the context menu works though (but low res).

@binste
Copy link
Collaborator

binste commented Apr 12, 2024

Thanks for the feedback Till! Do you have a minimal reproducible example I could test against? It works for me using the following code with Altair 5.3.0, altair_tiles 0.3.0, vl-convert-python 1.3.0:

import altair as alt
import altair_tiles as til
import geopandas as gpd

gdf_ne = gpd.read_file("https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip")  # zipped shapefile
extent_roi = gdf_ne.loc[gdf_ne["CONTINENT"] == "Africa", ["CONTINENT", "geometry"]]

chart = (
    alt.Chart(extent_roi)
    .mark_geoshape(fillOpacity=0.1, stroke="green", strokeWidth=2)
    .project(type="mercator")
)
chart = til.add_tiles(chart).properties(width=600, height=400)
chart.save("chart.png")

By the way, if you want to increase the resolution of the PNG you get from the context menu, you can run alt.renderers.set_embed_options(scaleFactor=3) and then display the chart again (rerun the notebook cell).

@tillnagel
Copy link
Author

tillnagel commented Apr 12, 2024

Thanks for the quick response! It seems this does not work only if a provider is specified.

Works for me as well with your code from above. The following however creates a chart.png without a base map.

import altair as alt
import altair_tiles as til
import geopandas as gpd

gdf_ne = gpd.read_file("https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip")  # zipped shapefile
extent_roi = gdf_ne.loc[gdf_ne["CONTINENT"] == "Africa", ["CONTINENT", "geometry"]]

chart = (
    alt.Chart(extent_roi)
    .mark_geoshape(fillOpacity=0.1, stroke="green", strokeWidth=2)
    .project(type="mercator")
)
chart = til.add_tiles(chart, provider=til.providers.BaseMapDE.Grey).properties(width=600, height=400)
chart.save("chart.png")

@tillnagel
Copy link
Author

And thanks for the tip @binste to set the scaleFactor. Works like a charm.

@tillnagel tillnagel changed the title Exporting chart as PNG does not include map tiles Exporting chart as PNG does not include map tiles when a provider is specified Apr 12, 2024
@binste
Copy link
Collaborator

binste commented Apr 12, 2024

I think we're getting closer to the issue. Can you try it with a different provider such as OpenStreetMap.HOT? At least in this example, the basemap is also not included when displaying the chart. I think it's because BaseMapDE is just for Germany according to https://gdz.bkg.bund.de/index.php/default/gdz-basemapde-vektor-gdz-basemapde-vektor.html.

Some context to this: BaseMapDE will only work if we have a map of Germany. In addition, altair_tiles needs to ensure that only valid tiles are being loaded from the server, else no tiles at all will show up. This is happening at https://github.com/vega/altair_tiles/blob/main/altair_tiles/__init__.py#L281 in the code. However, I think right now it will even fail then, because the package xyzservices usually provides a bounds attribute on providers if they do not cover the whole world. However, BaseMapDE is missing that attribute.

Do you know by any chance which bounds are valid for BaseMapDE? I can't find anything on their website. We could then open an issue in the repo of xyzservices to include it.

@tillnagel
Copy link
Author

tillnagel commented Apr 12, 2024

Weird, some work, some do not.

It does not seem to be related to the bounds (my actual map is in Germany; just for the MRE I used your example).

OpenStreetMap.HOT works
Esri.WorldGrayCanvas fails
BaseMapDE.Grey fails
OpenStreetMap.BlackAndWhite fails
OpenStreetMap.Mapnik works

@binste
Copy link
Collaborator

binste commented Apr 14, 2024

Interesting! OpenStreetMap.BlackAndWhite seems to have been deprecated leaflet-extras/leaflet-providers#316 and also does not show up for me in a notebook but the others are weird. I've opened vega/vl-convert#162 to investigate this further, thanks again! Hope the "Save as PNG" context menu works for you in the meantime.

@jonmmease
Copy link

This has been fixed in vl-convert-python 1.4.0. The issue was that vl-convert was failing to handle tile providers that don't include file extensions in the url.

So https://tile.openstreetmap.org/{z}/{x}/{y}.png was working, but not https://server.arcgisonline.com/ArcGIS/rest/services/{variant}/MapServer/tile/{z}/{y}/{x}

See vega/vl-convert#163

@binste
Copy link
Collaborator

binste commented Apr 16, 2024

Thank you very much @jonmmease!! Really appreciate it.

@tillnagel could you check if this solves the issue you had with BaseMapDE.Grey?

@tillnagel
Copy link
Author

Works now. Great work @jonmmease! Thanks for the support and this great lib @binste!

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

No branches or pull requests

3 participants