Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/test_vector_layers.py
1593 views
1
""" "
2
Test Vector Layers
3
------------------
4
5
"""
6
7
import json
8
9
from folium import Map
10
from folium.utilities import get_bounds, normalize
11
from folium.vector_layers import (
12
Circle,
13
CircleMarker,
14
Polygon,
15
PolyLine,
16
Rectangle,
17
path_options,
18
)
19
20
21
def test_circle():
22
m = Map()
23
radius = 10000
24
popup = f"I am {radius} meters"
25
location = [-27.551667, -48.478889]
26
27
circle = Circle(
28
location=location,
29
radius=radius,
30
color="black",
31
weight=2,
32
fill_opacity=0.6,
33
opacity=1,
34
fill=True,
35
popup=popup,
36
)
37
circle.add_to(m)
38
39
expected_options = {
40
"bubblingMouseEvents": True,
41
"color": "black",
42
"dashArray": None,
43
"dashOffset": None,
44
"fill": True,
45
"fillColor": "black",
46
"fillOpacity": 0.6,
47
"fillRule": "evenodd",
48
"lineCap": "round",
49
"lineJoin": "round",
50
"opacity": 1,
51
"radius": radius,
52
"stroke": True,
53
"weight": 2,
54
}
55
56
m._repr_html_()
57
expected_rendered = f"""
58
var {circle.get_name()} = L.circle(
59
{location},
60
{{
61
"bubblingMouseEvents": true,
62
"color": "black",
63
"dashArray": null,
64
"dashOffset": null,
65
"fill": true,
66
"fillColor": "black",
67
"fillOpacity": 0.6,
68
"fillRule": "evenodd",
69
"lineCap": "round",
70
"lineJoin": "round",
71
"opacity": 1,
72
"radius": {radius},
73
"stroke": true,
74
"weight": 2
75
}}
76
)
77
.addTo({m.get_name()});
78
""" # noqa
79
80
rendered = circle._template.module.script(circle)
81
assert normalize(rendered) == normalize(expected_rendered)
82
assert circle.get_bounds() == [location, location]
83
assert json.dumps(circle.to_dict()) == circle.to_json()
84
assert circle.location == [-27.551667, -48.478889]
85
assert circle.options == expected_options
86
87
88
def test_circle_marker():
89
m = Map()
90
radius = 50
91
popup = f"I am {radius} pixels"
92
location = [-27.55, -48.8]
93
94
circle_marker = CircleMarker(
95
location=location,
96
radius=radius,
97
color="black",
98
weight=2,
99
fill_opacity=0.6,
100
opacity=1,
101
fill=True,
102
popup=popup,
103
)
104
circle_marker.add_to(m)
105
106
options = {
107
"bubblingMouseEvents": True,
108
"color": "black",
109
"dashArray": None,
110
"dashOffset": None,
111
"fill": True,
112
"fillColor": "black",
113
"fillOpacity": 0.6,
114
"fillRule": "evenodd",
115
"lineCap": "round",
116
"lineJoin": "round",
117
"opacity": 1,
118
"radius": radius,
119
"stroke": True,
120
"weight": 2,
121
}
122
123
m._repr_html_()
124
expected_bounds = [location, location]
125
expected_rendered = f"""
126
var {circle_marker.get_name()} = L.circleMarker(
127
{location},
128
{{
129
"bubblingMouseEvents": true,
130
"color": "black",
131
"dashArray": null,
132
"dashOffset": null,
133
"fill": true,
134
"fillColor": "black",
135
"fillOpacity": 0.6,
136
"fillRule": "evenodd",
137
"lineCap": "round",
138
"lineJoin": "round",
139
"opacity": 1,
140
"radius": {radius},
141
"stroke": true,
142
"weight": 2
143
}}
144
)
145
.addTo({m.get_name()});
146
""" # noqa
147
148
rendered = circle_marker._template.module.script(circle_marker)
149
assert normalize(rendered) == normalize(expected_rendered)
150
assert circle_marker.get_bounds() == expected_bounds
151
assert json.dumps(circle_marker.to_dict()) == circle_marker.to_json()
152
assert circle_marker.location == location
153
assert circle_marker.options == options
154
155
156
def test_rectangle():
157
m = Map()
158
159
location = [[45.6, -122.8], [45.61, -122.7]]
160
rectangle = Rectangle(
161
bounds=location,
162
popup="I am a rectangle",
163
color="black",
164
weight=2,
165
fill_opacity=0.6,
166
opacity=1,
167
fill=True,
168
)
169
rectangle.add_to(m)
170
171
expected_options = {
172
"bubblingMouseEvents": True,
173
"color": "black",
174
"dashArray": None,
175
"dashOffset": None,
176
"fill": True,
177
"fillColor": "black",
178
"fillOpacity": 0.6,
179
"fillRule": "evenodd",
180
"lineCap": "round",
181
"lineJoin": "round",
182
"noClip": False,
183
"opacity": 1,
184
"smoothFactor": 1.0,
185
"stroke": True,
186
"weight": 2,
187
}
188
189
m._repr_html_()
190
expected_rendered = f"""
191
var {rectangle.get_name()} = L.rectangle(
192
{location},
193
{{
194
"bubblingMouseEvents": true,
195
"color": "black",
196
"dashArray": null,
197
"dashOffset": null,
198
"fill": true,
199
"fillColor": "black",
200
"fillOpacity": 0.6,
201
"fillRule": "evenodd",
202
"lineCap": "round",
203
"lineJoin": "round",
204
"noClip": false,
205
"opacity": 1,
206
"smoothFactor": 1.0,
207
"stroke": true,
208
"weight": 2
209
}}
210
)
211
.addTo({m.get_name()});
212
"""
213
214
rendered = rectangle._template.module.script(rectangle)
215
assert normalize(rendered) == normalize(expected_rendered)
216
assert rectangle.get_bounds() == location
217
assert json.dumps(rectangle.to_dict()) == rectangle.to_json()
218
assert rectangle.options == expected_options
219
220
221
def test_polygon_marker():
222
m = Map()
223
locations = [
224
[35.6636, 139.7634],
225
[35.6629, 139.7664],
226
[35.6663, 139.7706],
227
[35.6725, 139.7632],
228
[35.6728, 139.7627],
229
[35.6720, 139.7606],
230
[35.6682, 139.7588],
231
[35.6663, 139.7627],
232
]
233
polygon = Polygon(locations=locations, popup="I am a polygon")
234
polygon.add_to(m)
235
236
expected_options = {
237
"bubblingMouseEvents": True,
238
"color": "#3388ff",
239
"dashArray": None,
240
"dashOffset": None,
241
"fill": False,
242
"fillColor": "#3388ff",
243
"fillOpacity": 0.2,
244
"fillRule": "evenodd",
245
"lineCap": "round",
246
"lineJoin": "round",
247
"noClip": False,
248
"opacity": 1.0,
249
"smoothFactor": 1.0,
250
"stroke": True,
251
"weight": 3,
252
}
253
254
m._repr_html_()
255
expected_rendered = f"""
256
var {polygon.get_name()} = L.polygon(
257
{locations},
258
{{
259
"bubblingMouseEvents": true,
260
"color": "#3388ff",
261
"dashArray": null,
262
"dashOffset": null,
263
"fill": false,
264
"fillColor": "#3388ff",
265
"fillOpacity": 0.2,
266
"fillRule": "evenodd",
267
"lineCap": "round",
268
"lineJoin": "round",
269
"noClip": false,
270
"opacity": 1.0,
271
"smoothFactor": 1.0,
272
"stroke": true,
273
"weight": 3
274
}}
275
)
276
.addTo({m.get_name()});
277
"""
278
279
rendered = polygon._template.module.script(polygon)
280
assert normalize(rendered) == normalize(expected_rendered)
281
assert polygon.get_bounds() == get_bounds(locations)
282
assert json.dumps(polygon.to_dict()) == polygon.to_json()
283
assert polygon.options == expected_options
284
285
286
def test_polyline():
287
m = Map()
288
locations = [[40.0, -80.0], [45.0, -80.0]]
289
polyline = PolyLine(locations=locations, popup="I am PolyLine")
290
polyline.add_to(m)
291
292
expected_options = {
293
"smoothFactor": 1.0,
294
"noClip": False,
295
"bubblingMouseEvents": True,
296
"color": "#3388ff",
297
"dashArray": None,
298
"dashOffset": None,
299
"fill": False,
300
"fillColor": "#3388ff",
301
"fillOpacity": 0.2,
302
"fillRule": "evenodd",
303
"lineCap": "round",
304
"lineJoin": "round",
305
"opacity": 1.0,
306
"stroke": True,
307
"weight": 3,
308
}
309
310
m._repr_html_()
311
expected_rendered = f"""
312
var {polyline.get_name()} = L.polyline(
313
{locations},
314
{{
315
"bubblingMouseEvents": true,
316
"color": "#3388ff",
317
"dashArray": null,
318
"dashOffset": null,
319
"fill": false,
320
"fillColor": "#3388ff",
321
"fillOpacity": 0.2,
322
"fillRule": "evenodd",
323
"lineCap": "round",
324
"lineJoin": "round",
325
"noClip": false,
326
"opacity": 1.0,
327
"smoothFactor": 1.0,
328
"stroke": true,
329
"weight": 3
330
}}
331
)
332
.addTo({m.get_name()});
333
"""
334
335
rendered = polyline._template.module.script(polyline)
336
assert normalize(rendered) == normalize(expected_rendered)
337
assert polyline.get_bounds() == get_bounds(locations)
338
assert json.dumps(polyline.to_dict()) == polyline.to_json()
339
assert polyline.options == expected_options
340
341
342
def test_mulyipolyline():
343
m = Map()
344
345
locations = [
346
[[45.51, -122.68], [37.77, -122.43], [34.04, -118.2]],
347
[[40.78, -73.91], [41.83, -87.62], [32.76, -96.72]],
348
]
349
350
multipolyline = PolyLine(locations=locations, popup="MultiPolyLine")
351
multipolyline.add_to(m)
352
353
expected_options = {
354
"smoothFactor": 1.0,
355
"noClip": False,
356
"bubblingMouseEvents": True,
357
"color": "#3388ff",
358
"dashArray": None,
359
"dashOffset": None,
360
"fill": False,
361
"fillColor": "#3388ff",
362
"fillOpacity": 0.2,
363
"fillRule": "evenodd",
364
"lineCap": "round",
365
"lineJoin": "round",
366
"opacity": 1.0,
367
"stroke": True,
368
"weight": 3,
369
}
370
371
m._repr_html_()
372
expected_rendered = f"""
373
var {multipolyline.get_name()} = L.polyline(
374
{locations},
375
{{
376
"bubblingMouseEvents": true,
377
"color": "#3388ff",
378
"dashArray": null,
379
"dashOffset": null,
380
"fill": false,
381
"fillColor": "#3388ff",
382
"fillOpacity": 0.2,
383
"fillRule": "evenodd",
384
"lineCap": "round",
385
"lineJoin": "round",
386
"noClip": false,
387
"opacity": 1.0,
388
"smoothFactor": 1.0,
389
"stroke": true,
390
"weight": 3
391
}}
392
)
393
.addTo({m.get_name()});
394
"""
395
396
rendered = multipolyline._template.module.script(multipolyline)
397
assert normalize(rendered) == normalize(expected_rendered)
398
assert multipolyline.get_bounds() == get_bounds(locations)
399
assert json.dumps(multipolyline.to_dict()) == multipolyline.to_json()
400
assert multipolyline.options == expected_options
401
402
403
def test_path_options_lower_camel_case():
404
options = path_options(fill_color="red", fillOpacity=0.3)
405
assert options["fillColor"] == "red"
406
assert options["fillOpacity"] == 0.3
407
408