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