Skip to content

Commit

Permalink
Update widgets.py
Browse files Browse the repository at this point in the history
Fixes an issue where when users are using folium map and explore outside of the bounds of the raster the tileserver is continuously queried resulting in excessive 404 errors.
  • Loading branch information
tjlogue4 authored Mar 21, 2024
1 parent 1f8d6ec commit 23e3c02
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions localtileserver/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
class LocalTileServerLayerMixin:
"""Mixin class for tile layers using localtileserver."""

# Prevent the client from being garbage collected
tile_server: TileClient
pass


def get_leaflet_tile_layer(
Expand Down Expand Up @@ -176,6 +175,7 @@ def get_folium_tile_layer(
# Safely import folium
try:
from folium import TileLayer
from traitlets import Tuple, Union
except ImportError as e: # pragma: no cover
raise ImportError(f"Please install `folium`: {e}")

Expand All @@ -196,7 +196,7 @@ def get_folium_tile_layer(
)

class FoliumTileLayer(TileLayer, LocalTileServerLayerMixin):
pass
bounds = Union((Tuple(),), default_value=None, allow_none=True).tag(sync=True, o=True)

source, created = get_or_create_tile_client(
source,
Expand All @@ -213,7 +213,9 @@ class FoliumTileLayer(TileLayer, LocalTileServerLayerMixin):
)
if attr is None:
attr = DEFAULT_ATTRIBUTION
tile_layer = FoliumTileLayer(tiles=url, attr=attr, **kwargs)
b = source.bounds()
bounds = ((b[0], b[2]), (b[1], b[3]))
tile_layer = FoliumTileLayer(tiles=url, bounds=bounds, attr=attr, **kwargs)
if created:
# HACK: Prevent the client from being garbage collected
tile_layer.tile_server = source
Expand Down

0 comments on commit 23e3c02

Please sign in to comment.