2
2
Wraps leaflet TileLayer, WmsTileLayer (TileLayer.WMS), ImageOverlay, and VideoOverlay
3
3
4
4
"""
5
+ from typing import TYPE_CHECKING , Any , Callable , Optional , Union
5
6
6
7
from branca .element import Element , Figure
7
8
from jinja2 import Environment , PackageLoader , Template
8
9
9
10
from folium .map import Layer
10
- from folium .utilities import image_to_url , mercator_transform , parse_options
11
+ from folium .utilities import (
12
+ TypeBounds ,
13
+ TypeJsonValue ,
14
+ image_to_url ,
15
+ mercator_transform ,
16
+ parse_options ,
17
+ )
18
+
19
+ if TYPE_CHECKING :
20
+ import xyzservices
21
+
11
22
12
23
ENV = Environment (loader = PackageLoader ("folium" , "templates" ))
13
24
@@ -78,30 +89,30 @@ class TileLayer(Layer):
78
89
79
90
def __init__ (
80
91
self ,
81
- tiles = "OpenStreetMap" ,
82
- min_zoom = 0 ,
83
- max_zoom = 18 ,
84
- max_native_zoom = None ,
85
- attr = None ,
86
- detect_retina = False ,
87
- name = None ,
88
- overlay = False ,
89
- control = True ,
90
- show = True ,
91
- no_wrap = False ,
92
- subdomains = "abc" ,
93
- tms = False ,
94
- opacity = 1 ,
92
+ tiles : Union [ str , "xyzservices.TileProvider" ] = "OpenStreetMap" ,
93
+ min_zoom : int = 0 ,
94
+ max_zoom : int = 18 ,
95
+ max_native_zoom : Optional [ int ] = None ,
96
+ attr : Optional [ str ] = None ,
97
+ detect_retina : bool = False ,
98
+ name : Optional [ str ] = None ,
99
+ overlay : bool = False ,
100
+ control : bool = True ,
101
+ show : bool = True ,
102
+ no_wrap : bool = False ,
103
+ subdomains : str = "abc" ,
104
+ tms : bool = False ,
105
+ opacity : float = 1 ,
95
106
** kwargs
96
107
):
97
108
98
109
# check for xyzservices.TileProvider without importing it
99
110
if isinstance (tiles , dict ):
100
- attr = attr if attr else tiles .html_attribution
111
+ attr = attr if attr else tiles .html_attribution # type: ignore
101
112
min_zoom = tiles .get ("min_zoom" , min_zoom )
102
113
max_zoom = tiles .get ("max_zoom" , max_zoom )
103
114
subdomains = tiles .get ("subdomains" , subdomains )
104
- tiles = tiles .build_url (fill_subdomain = False , scale_factor = "{r}" )
115
+ tiles = tiles .build_url (fill_subdomain = False , scale_factor = "{r}" ) # type: ignore
105
116
106
117
self .tile_name = (
107
118
name if name is not None else "" .join (tiles .lower ().strip ().split ())
@@ -198,17 +209,17 @@ class WmsTileLayer(Layer):
198
209
199
210
def __init__ (
200
211
self ,
201
- url ,
202
- layers ,
203
- styles = "" ,
204
- fmt = "image/jpeg" ,
205
- transparent = False ,
206
- version = "1.1.1" ,
207
- attr = "" ,
208
- name = None ,
209
- overlay = True ,
210
- control = True ,
211
- show = True ,
212
+ url : str ,
213
+ layers : str ,
214
+ styles : str = "" ,
215
+ fmt : str = "image/jpeg" ,
216
+ transparent : bool = False ,
217
+ version : str = "1.1.1" ,
218
+ attr : str = "" ,
219
+ name : Optional [ str ] = None ,
220
+ overlay : bool = True ,
221
+ control : bool = True ,
222
+ show : bool = True ,
212
223
** kwargs
213
224
):
214
225
super ().__init__ (name = name , overlay = overlay , control = control , show = show )
@@ -289,16 +300,16 @@ class ImageOverlay(Layer):
289
300
290
301
def __init__ (
291
302
self ,
292
- image ,
293
- bounds ,
294
- origin = "upper" ,
295
- colormap = None ,
296
- mercator_project = False ,
297
- pixelated = True ,
298
- name = None ,
299
- overlay = True ,
300
- control = True ,
301
- show = True ,
303
+ image : Any ,
304
+ bounds : TypeBounds ,
305
+ origin : str = "upper" ,
306
+ colormap : Optional [ Callable ] = None ,
307
+ mercator_project : bool = False ,
308
+ pixelated : bool = True ,
309
+ name : Optional [ str ] = None ,
310
+ overlay : bool = True ,
311
+ control : bool = True ,
312
+ show : bool = True ,
302
313
** kwargs
303
314
):
304
315
super ().__init__ (name = name , overlay = overlay , control = control , show = show )
@@ -313,7 +324,7 @@ def __init__(
313
324
314
325
self .url = image_to_url (image , origin = origin , colormap = colormap )
315
326
316
- def render (self , ** kwargs ):
327
+ def render (self , ** kwargs ) -> None :
317
328
super ().render ()
318
329
319
330
figure = self .get_root ()
@@ -338,7 +349,7 @@ def render(self, **kwargs):
338
349
Element (pixelated ), name = "leaflet-image-layer"
339
350
) # noqa
340
351
341
- def _get_self_bounds (self ):
352
+ def _get_self_bounds (self ) -> TypeBounds :
342
353
"""
343
354
Computes the bounds of the object itself (not including it's children)
344
355
in the form [[lat_min, lon_min], [lat_max, lon_max]].
@@ -388,15 +399,15 @@ class VideoOverlay(Layer):
388
399
389
400
def __init__ (
390
401
self ,
391
- video_url ,
392
- bounds ,
393
- autoplay = True ,
394
- loop = True ,
395
- name = None ,
396
- overlay = True ,
397
- control = True ,
398
- show = True ,
399
- ** kwargs
402
+ video_url : str ,
403
+ bounds : TypeBounds ,
404
+ autoplay : bool = True ,
405
+ loop : bool = True ,
406
+ name : Optional [ str ] = None ,
407
+ overlay : bool = True ,
408
+ control : bool = True ,
409
+ show : bool = True ,
410
+ ** kwargs : TypeJsonValue
400
411
):
401
412
super ().__init__ (name = name , overlay = overlay , control = control , show = show )
402
413
self ._name = "VideoOverlay"
@@ -405,7 +416,7 @@ def __init__(
405
416
self .bounds = bounds
406
417
self .options = parse_options (autoplay = autoplay , loop = loop , ** kwargs )
407
418
408
- def _get_self_bounds (self ):
419
+ def _get_self_bounds (self ) -> TypeBounds :
409
420
"""
410
421
Computes the bounds of the object itself (not including it's children)
411
422
in the form [[lat_min, lon_min], [lat_max, lon_max]]
0 commit comments