-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
vector_layers.py
382 lines (324 loc) · 12.7 KB
/
vector_layers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""
Wraps leaflet Polyline, Polygon, Rectangle, Circle, and CircleMarker
"""
from typing import List, Optional, Sequence, Union
from branca.element import MacroElement
from jinja2 import Template
from folium.map import Marker, Popup, Tooltip
from folium.utilities import (
TypeLine,
TypeMultiLine,
TypePathOptions,
camelize,
get_bounds,
validate_locations,
validate_multi_locations,
)
def path_options(
line: bool = False, radius: Optional[float] = None, **kwargs: TypePathOptions
):
"""
Contains options and constants shared between vector overlays
(Polygon, Polyline, Circle, CircleMarker, and Rectangle).
Parameters
----------
stroke: Bool, True
Whether to draw stroke along the path.
Set it to false to disable borders on polygons or circles.
color: str, '#3388ff'
Stroke color.
weight: int, 3
Stroke width in pixels.
opacity: float, 1.0
Stroke opacity.
line_cap: str, 'round' (lineCap)
A string that defines shape to be used at the end of the stroke.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap
line_join: str, 'round' (lineJoin)
A string that defines shape to be used at the corners of the stroke.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linejoin
dash_array: str, None (dashArray)
A string that defines the stroke dash pattern.
Doesn't work on Canvas-powered layers in some old browsers.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray
dash_offset:, str, None (dashOffset)
A string that defines the distance into the dash pattern to start the dash.
Doesn't work on Canvas-powered layers in some old browsers.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dashoffset
fill: Bool, False
Whether to fill the path with color.
Set it to false to disable filling on polygons or circles.
fill_color: str, default to `color` (fillColor)
Fill color. Defaults to the value of the color option.
fill_opacity: float, 0.2 (fillOpacity)
Fill opacity.
fill_rule: str, 'evenodd' (fillRule)
A string that defines how the inside of a shape is determined.
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule
bubbling_mouse_events: Bool, True (bubblingMouseEvents)
When true a mouse event on this path will trigger the same event on the
map (unless L.DomEvent.stopPropagation is used).
gradient: bool, default None
When a gradient on the stroke and fill is available,
allows turning it on or off.
Note that the presence of `fill_color` will override `fill=False`.
This function accepts both snake_case and lowerCamelCase equivalents.
See https://leafletjs.com/reference.html#path
"""
kwargs = {camelize(key): value for key, value in kwargs.items()}
extra_options = {}
if line:
extra_options = {
"smoothFactor": kwargs.pop("smoothFactor", 1.0),
"noClip": kwargs.pop("noClip", False),
}
if radius:
extra_options.update({"radius": radius})
color = kwargs.pop("color", "#3388ff")
fill_color = kwargs.pop("fillColor", False)
if fill_color:
fill = True
elif not fill_color:
fill_color = color
fill = kwargs.pop("fill", False) # type: ignore
gradient = kwargs.pop("gradient", None)
if gradient is not None:
extra_options.update({"gradient": gradient})
default = {
"stroke": kwargs.pop("stroke", True),
"color": color,
"weight": kwargs.pop("weight", 3),
"opacity": kwargs.pop("opacity", 1.0),
"lineCap": kwargs.pop("lineCap", "round"),
"lineJoin": kwargs.pop("lineJoin", "round"),
"dashArray": kwargs.pop("dashArray", None),
"dashOffset": kwargs.pop("dashOffset", None),
"fill": fill,
"fillColor": fill_color,
"fillOpacity": kwargs.pop("fillOpacity", 0.2),
"fillRule": kwargs.pop("fillRule", "evenodd"),
"bubblingMouseEvents": kwargs.pop("bubblingMouseEvents", True),
}
default.update(extra_options)
return default
class BaseMultiLocation(MacroElement):
"""Base class for vector classes with multiple coordinates.
Not for direct consumption
"""
def __init__(
self,
locations: TypeMultiLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
):
super().__init__()
self.locations = validate_multi_locations(locations)
if popup is not None:
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
if tooltip is not None:
self.add_child(
tooltip if isinstance(tooltip, Tooltip) else Tooltip(str(tooltip))
)
def _get_self_bounds(self) -> List[List[Optional[float]]]:
"""Compute the bounds of the object itself."""
return get_bounds(self.locations)
class PolyLine(BaseMultiLocation):
"""Draw polyline overlays on a map.
See :func:`folium.vector_layers.path_options` for the `Path` options.
Parameters
----------
locations: list of points (latitude, longitude)
Latitude and Longitude of line (Northing, Easting)
Pass multiple sequences of coordinates for a multi-polyline.
popup: str or folium.Popup, default None
Input text or visualization for object displayed when clicking.
tooltip: str or folium.Tooltip, default None
Display a text when hovering over the object.
smooth_factor: float, default 1.0
How much to simplify the polyline on each zoom level.
More means better performance and smoother look,
and less means more accurate representation.
no_clip: Bool, default False
Disable polyline clipping.
**kwargs
Other valid (possibly inherited) options. See:
https://leafletjs.com/reference.html#polyline
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = L.polyline(
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% endmacro %}
"""
)
def __init__(self, locations, popup=None, tooltip=None, **kwargs):
super().__init__(locations, popup=popup, tooltip=tooltip)
self._name = "PolyLine"
self.options = path_options(line=True, **kwargs)
class Polygon(BaseMultiLocation):
"""Draw polygon overlays on a map.
See :func:`folium.vector_layers.path_options` for the `Path` options.
Parameters
----------
locations: list of points (latitude, longitude)
- One list of coordinate pairs to define a polygon. You don't have to
add a last point equal to the first point.
- If you pass a list with multiple of those it will make a multi-
polygon.
popup: string or folium.Popup, default None
Input text or visualization for object displayed when clicking.
tooltip: str or folium.Tooltip, default None
Display a text when hovering over the object.
**kwargs
Other valid (possibly inherited) options. See:
https://leafletjs.com/reference.html#polygon
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = L.polygon(
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% endmacro %}
"""
)
def __init__(
self,
locations: TypeMultiLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
):
super().__init__(locations, popup=popup, tooltip=tooltip)
self._name = "Polygon"
self.options = path_options(line=True, radius=None, **kwargs)
class Rectangle(MacroElement):
"""Draw rectangle overlays on a map.
See :func:`folium.vector_layers.path_options` for the `Path` options.
Parameters
----------
bounds: [(lat1, lon1), (lat2, lon2)]
Two lat lon pairs marking the two corners of the rectangle.
popup: string or folium.Popup, default None
Input text or visualization for object displayed when clicking.
tooltip: str or folium.Tooltip, default None
Display a text when hovering over the object.
**kwargs
Other valid (possibly inherited) options. See:
https://leafletjs.com/reference.html#rectangle
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.rectangle(
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% endmacro %}
"""
)
def __init__(
self,
bounds: TypeLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
):
super().__init__()
self._name = "rectangle"
self.options = path_options(line=True, radius=None, **kwargs)
self.locations = validate_locations(bounds)
assert len(self.locations) == 2, "Need two lat/lon pairs"
if popup is not None:
self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup)))
if tooltip is not None:
self.add_child(
tooltip if isinstance(tooltip, Tooltip) else Tooltip(str(tooltip))
)
def _get_self_bounds(self) -> List[List[Optional[float]]]:
"""Compute the bounds of the object itself."""
return get_bounds(self.locations)
class Circle(Marker):
"""
Class for drawing circle overlays on a map.
It's an approximation and starts to diverge from a real circle closer to
the poles (due to projection distortion).
See :func:`folium.vector_layers.path_options` for the `Path` options.
Parameters
----------
location: tuple[float, float]
Latitude and Longitude pair (Northing, Easting)
popup: string or folium.Popup, default None
Input text or visualization for object displayed when clicking.
tooltip: str or folium.Tooltip, default None
Display a text when hovering over the object.
radius: float
Radius of the circle, in meters.
**kwargs
Other valid (possibly inherited) options. See:
https://leafletjs.com/reference.html#circle
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = L.circle(
{{ this.location|tojson }},
{{ this.options|tojson }}
).addTo({{ this._parent.get_name() }});
{% endmacro %}
"""
)
def __init__(
self,
location: Optional[Sequence[float]] = None,
radius: float = 50,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "circle"
self.options = path_options(line=False, radius=radius, **kwargs)
class CircleMarker(Marker):
"""
A circle of a fixed size with radius specified in pixels.
See :func:`folium.vector_layers.path_options` for the `Path` options.
Parameters
----------
location: tuple[float, float]
Latitude and Longitude pair (Northing, Easting)
popup: string or folium.Popup, default None
Input text or visualization for object displayed when clicking.
tooltip: str or folium.Tooltip, default None
Display a text when hovering over the object.
radius: float, default 10
Radius of the circle marker, in pixels.
**kwargs
Other valid (possibly inherited) options. See:
https://leafletjs.com/reference.html#circlemarker
"""
_template = Template(
"""
{% macro script(this, kwargs) %}
var {{ this.get_name() }} = L.circleMarker(
{{ this.location|tojson }},
{{ this.options|tojson }}
).addTo({{ this._parent.get_name() }});
{% endmacro %}
"""
)
def __init__(
self,
location: Optional[Sequence[float]] = None,
radius: float = 10,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "CircleMarker"
self.options = path_options(line=False, radius=radius, **kwargs)