Is there a way to make folium (python) upscale tiles when zoomin

ghz 8hours ago ⋅ 2 views

Is there a way to make folium (python) upscale tiles when zooming in closer than tiles are available?

I'm plotting a map with the folium python package and it looks beautiful. However, the free tileset I'm using (the USGS one, listed here as "USGS.USImagery") only serves tiles up to zoom level 16.

I'd like to be able to zoom in closer than that, as I'm plotting some of my own features on top of those tiles at a higher resolution. Is there a way to tell folium to just upscale the level-16 tiles when zooming to anything above 16? I'm aware they will be pixelated, but I'd rather that than blank grey tiles.

Answer

Yes, you can achieve this by telling Folium to use the available zoom level 16 tiles for higher zoom levels, effectively "upscaling" the tiles by repeating them. While Folium does not have an explicit option for this out-of-the-box, you can accomplish it by customizing the tile layer.

Here's how you can do it:

Solution: Custom Tile Layer with Max Zoom

You can create a custom tile layer where the maxZoom is set to a higher value than the default, and then use the highest available zoom level (16 in this case) for zoom levels above 16.

Here's an example of how to set this up:

import folium

# Create a basic map
m = folium.Map(location=[37.7749, -122.4194], zoom_start=12)

# Add the USGS imagery layer, which has a max zoom level of 16
folium.TileLayer(
    tiles='https://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png',  # replace this URL with the USGS tiles URL
    attr='USGS US Imagery',
    name='USGS',
    max_zoom=16
).add_to(m)

# Add a custom tile layer for zoom levels beyond 16
folium.TileLayer(
    tiles='https://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png',  # same URL to upscale tiles
    attr='USGS US Imagery (upscaled)',
    name='USGS (upscaled)',
    max_zoom=22,  # You can set a higher zoom level (like 22) to allow zooming beyond 16
    min_zoom=16   # Make sure it starts at zoom level 16
).add_to(m)

# Add layer control
folium.LayerControl().add_to(m)

# Save map to an HTML file
m.save('map_with_upscaled_tiles.html')

Key Points:

  1. Max Zoom Level: In the second TileLayer, we set max_zoom to a higher value (22 in this example) so that users can zoom in beyond level 16.

  2. Same Tile URL: The URL of the USGS tiles is the same for zoom levels 16 and higher, but Folium will use the available zoom level 16 tiles for anything above that. Since the tiles are just repeated for higher zoom levels, they will appear pixelated (but you'll get tiles rather than a grey area).

  3. Layer Control: The LayerControl widget allows users to toggle between the different layers, including the upscaled one.

Adjusting the Tile URL:

The code above uses the wmflabs.org URL, but you need to replace the tiles URL with the USGS URL that you're using. The format should be similar to the example provided.

Alternative Approach: Using a Custom Image Overlay

If you want to manually upscale and use custom tiles or images, you can use a custom image overlay or dynamically generate the tiles, but this approach requires more effort. The solution above should be sufficient for most use cases where you just want to upscale the existing tiles.

Let me know if you need further adjustments or have more questions!