Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/pattern.py
2531 views
1
from branca.element import MacroElement
2
3
from folium.elements import JSCSSMixin
4
from folium.folium import Map
5
from folium.template import Template
6
from folium.utilities import get_obj_in_upper_tree, remove_empty
7
8
9
class StripePattern(JSCSSMixin, MacroElement):
10
"""Fill Pattern for polygon composed of alternating lines.
11
12
Add these to the 'fillPattern' field in GeoJson style functions.
13
14
Parameters
15
----------
16
angle: float, default 0.5
17
Angle of the line pattern (degrees). Should be between -360 and 360.
18
weight: float, default 4
19
Width of the main lines (pixels).
20
space_weight: float
21
Width of the alternate lines (pixels).
22
color: string with hexadecimal, RGB, or named color, default "#000000"
23
Color of the main lines.
24
space_color: string with hexadecimal, RGB, or named color, default "#ffffff"
25
Color of the alternate lines.
26
opacity: float, default 0.75
27
Opacity of the main lines. Should be between 0 and 1.
28
space_opacity: float, default 0.0
29
Opacity of the alternate lines. Should be between 0 and 1.
30
31
See https://github.com/teastman/Leaflet.pattern for more information.
32
"""
33
34
_template = Template("""
35
{% macro script(this, kwargs) %}
36
var {{ this.get_name() }} = new L.StripePattern(
37
{{ this.options|tojavascript }}
38
);
39
{{ this.get_name() }}.addTo({{ this.parent_map.get_name() }});
40
{% endmacro %}
41
""")
42
43
default_js = [
44
("pattern", "https://teastman.github.io/Leaflet.pattern/leaflet.pattern.js")
45
]
46
47
def __init__(
48
self,
49
angle=0.5,
50
weight=4,
51
space_weight=4,
52
color="#000000",
53
space_color="#ffffff",
54
opacity=0.75,
55
space_opacity=0.0,
56
**kwargs
57
):
58
super().__init__()
59
self._name = "StripePattern"
60
self.options = remove_empty(
61
angle=angle,
62
weight=weight,
63
space_weight=space_weight,
64
color=color,
65
space_color=space_color,
66
opacity=opacity,
67
space_opacity=space_opacity,
68
**kwargs
69
)
70
self.parent_map = None
71
72
def render(self, **kwargs):
73
self.parent_map = get_obj_in_upper_tree(self, Map)
74
super().render(**kwargs)
75
76
77
class CirclePattern(JSCSSMixin, MacroElement):
78
"""Fill Pattern for polygon composed of repeating circles.
79
80
Add these to the 'fillPattern' field in GeoJson style functions.
81
82
Parameters
83
----------
84
width: int, default 20
85
Horizontal distance between circles (pixels).
86
height: int, default 20
87
Vertical distance between circles (pixels).
88
radius: int, default 12
89
Radius of each circle (pixels).
90
weight: float, default 2.0
91
Width of outline around each circle (pixels).
92
color: string with hexadecimal, RGB, or named color, default "#3388ff"
93
Color of the circle outline.
94
fill_color: string with hexadecimal, RGB, or named color, default "#3388ff"
95
Color of the circle interior.
96
opacity: float, default 0.75
97
Opacity of the circle outline. Should be between 0 and 1.
98
fill_opacity: float, default 0.5
99
Opacity of the circle interior. Should be between 0 and 1.
100
101
See https://github.com/teastman/Leaflet.pattern for more information.
102
"""
103
104
_template = Template("""
105
{% macro script(this, kwargs) %}
106
var {{ this.get_name() }}_shape = new L.PatternCircle(
107
{{ this.options_pattern_circle|tojavascript }}
108
);
109
var {{ this.get_name() }} = new L.Pattern(
110
{{ this.options_pattern|tojavascript }}
111
);
112
{{ this.get_name() }}.addShape({{ this.get_name() }}_shape);
113
{{ this.get_name() }}.addTo({{ this.parent_map }});
114
{% endmacro %}
115
""")
116
117
default_js = [
118
("pattern", "https://teastman.github.io/Leaflet.pattern/leaflet.pattern.js")
119
]
120
121
def __init__(
122
self,
123
width=20,
124
height=20,
125
radius=12,
126
weight=2.0,
127
color="#3388ff",
128
fill_color="#3388ff",
129
opacity=0.75,
130
fill_opacity=0.5,
131
):
132
super().__init__()
133
self._name = "CirclePattern"
134
self.options_pattern_circle = dict(
135
x=radius + 2 * weight,
136
y=radius + 2 * weight,
137
weight=weight,
138
radius=radius,
139
color=color,
140
fill_color=fill_color,
141
opacity=opacity,
142
fill_opacity=fill_opacity,
143
fill=True,
144
)
145
self.options_pattern = dict(
146
width=width,
147
height=height,
148
)
149
self.parent_map = None
150
151
def render(self, **kwargs):
152
self.parent_map = get_obj_in_upper_tree(self, Map).get_name()
153
super().render(**kwargs)
154
155