25
25
image_to_url ,
26
26
none_max ,
27
27
none_min ,
28
- get_obj_in_upper_tree
28
+ get_obj_in_upper_tree ,
29
+ parse_options ,
29
30
)
30
- from folium .vector_layers import PolyLine
31
+ from folium .vector_layers import PolyLine , path_options
31
32
32
33
from jinja2 import Template
33
34
@@ -46,65 +47,44 @@ class RegularPolygonMarker(Marker):
46
47
----------
47
48
location: tuple or list, default None
48
49
Latitude and Longitude of Marker (Northing, Easting)
49
- color: string, default 'black'
50
- Marker line color
51
- opacity: float, default 1
52
- Line opacity, scale 0-1
53
- weight: int, default 2
54
- Stroke weight in pixels
55
- fill_color: string, default 'blue'
56
- Marker fill color
57
- fill_opacity: float, default 1
58
- Marker fill opacity
59
50
number_of_sides: int, default 4
60
51
Number of polygon sides
61
52
rotation: int, default 0
62
53
Rotation angle in degrees
63
54
radius: int, default 15
64
55
Marker radius, in pixels
65
- popup: string or folium. Popup, default None
56
+ popup: string or Popup, optional
66
57
Input text or visualization for object displayed when clicking.
67
- tooltip: str or folium.Tooltip, default None
58
+ tooltip: str or folium.Tooltip, optional
68
59
Display a text when hovering over the object.
60
+ **kwargs:
61
+ See vector layers path_options for additional arguments.
69
62
70
63
https://humangeo.github.io/leaflet-dvf/
71
64
72
65
"""
73
66
_template = Template (u"""
74
- {% macro script(this, kwargs) %}
75
- var {{this.get_name()}} = new L.RegularPolygonMarker(
76
- new L.LatLng({{this.location[0]}},{{this.location[1]}}),
77
- {
78
- icon : new L.Icon.Default(),
79
- color: '{{this.color}}',
80
- opacity: {{this.opacity}},
81
- weight: {{this.weight}},
82
- fillColor: '{{this.fill_color}}',
83
- fillOpacity: {{this.fill_opacity}},
84
- numberOfSides: {{this.number_of_sides}},
85
- rotation: {{this.rotation}},
86
- radius: {{this.radius}}
87
- }
88
- ).addTo({{this._parent.get_name()}});
89
- {% endmacro %}
90
- """ )
67
+ {% macro script(this, kwargs) %}
68
+ var {{ this.get_name() }} = new L.RegularPolygonMarker(
69
+ {{ this.location|tojson }},
70
+ {{ this.options|tojson }}
71
+ ).addTo({{ this._parent.get_name() }});
72
+ {% endmacro %}
73
+ """ )
91
74
92
- def __init__ (self , location , color = 'black' , opacity = 1 , weight = 2 ,
93
- fill_color = 'blue' , fill_opacity = 1 , number_of_sides = 4 ,
94
- rotation = 0 , radius = 15 , popup = None , tooltip = None ):
75
+ def __init__ (self , location , number_of_sides = 4 , rotation = 0 , radius = 15 ,
76
+ popup = None , tooltip = None , ** kwargs ):
95
77
super (RegularPolygonMarker , self ).__init__ (
96
78
_iter_tolist (location ),
97
79
popup = popup , tooltip = tooltip
98
80
)
99
81
self ._name = 'RegularPolygonMarker'
100
- self .color = color
101
- self .opacity = opacity
102
- self .weight = weight
103
- self .fill_color = fill_color
104
- self .fill_opacity = fill_opacity
105
- self .number_of_sides = number_of_sides
106
- self .rotation = rotation
107
- self .radius = radius
82
+ self .options = path_options (** kwargs )
83
+ self .options .update (parse_options (
84
+ number_of_sides = number_of_sides ,
85
+ rotation = rotation ,
86
+ radius = radius ,
87
+ ))
108
88
109
89
def render (self , ** kwargs ):
110
90
"""Renders the HTML representation of the element."""
@@ -402,7 +382,7 @@ class GeoJson(Layer):
402
382
function {{ this.get_name() }}_styler(feature) {
403
383
switch({{ this.feature_identifier }}) {
404
384
{%- for style, ids_list in this.style_map.items() if not style == 'default' %}
405
- {% for id_val in ids_list %}case " {{ id_val }}" : {% endfor %}
385
+ {% for id_val in ids_list %}case {{ id_val|tojson }}: {% endfor %}
406
386
return {{ style }};
407
387
{%- endfor %}
408
388
default:
@@ -414,7 +394,7 @@ class GeoJson(Layer):
414
394
function {{ this.get_name() }}_highlighter(feature) {
415
395
switch({{ this.feature_identifier }}) {
416
396
{%- for style, ids_list in this.highlight_map.items() if not style == 'default' %}
417
- {% for id_val in ids_list %}case " {{ id_val }}" : {% endfor %}
397
+ {% for id_val in ids_list %}case {{ id_val|tojson }}: {% endfor %}
418
398
return {{ style }};
419
399
{%- endfor %}
420
400
default:
@@ -439,17 +419,17 @@ class GeoJson(Layer):
439
419
};
440
420
var {{ this.get_name() }} = L.geoJson(null, {
441
421
{%- if this.smooth_factor is not none %}
442
- smoothFactor: {{ this.smooth_factor }},
422
+ smoothFactor: {{ this.smooth_factor|tojson }},
443
423
{%- endif %}
444
424
onEachFeature: {{ this.get_name() }}_onEachFeature,
445
425
{% if this.style %}
446
426
style: {{ this.get_name() }}_styler,
447
427
{%- endif %}
448
428
}).addTo({{ this._parent.get_name() }});
449
429
{%- if this.embed %}
450
- {{ this.get_name() }}.addData({{ this.json }});
430
+ {{ this.get_name() }}.addData({{ this.data|tojson }});
451
431
{%- else %}
452
- $.ajax({url: " {{ this.embed_link }}" , dataType: 'json', async: true,
432
+ $.ajax({url: {{ this.embed_link|tojson }}, dataType: 'json', async: true,
453
433
success: function(data) {
454
434
{{ this.get_name() }}.addData(data);
455
435
}});
@@ -582,8 +562,6 @@ def render(self, **kwargs):
582
562
if self .highlight :
583
563
self .highlight_map = mapper .get_highlight_map (
584
564
self .highlight_function )
585
- if self .embed :
586
- self .json = json .dumps (self .data , sort_keys = True )
587
565
super (GeoJson , self ).render ()
588
566
589
567
@@ -699,15 +677,21 @@ class TopoJson(Layer):
699
677
"""
700
678
_template = Template (u"""
701
679
{% macro script(this, kwargs) %}
702
- var {{this.get_name()}}_data = {{this.style_data()}};
703
- var {{this.get_name()}} = L.geoJson(topojson.feature(
704
- {{this.get_name()}}_data,
705
- {{this.get_name()}}_data.{{this.object_path}})
706
- {% if this.smooth_factor is not none %}
707
- , {smoothFactor: {{this.smooth_factor}}}
708
- {% endif %}
709
- ).addTo({{this._parent.get_name()}});
710
- {{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
680
+ var {{ this.get_name() }}_data = {{ this.data|tojson }};
681
+ var {{ this.get_name() }} = L.geoJson(
682
+ topojson.feature(
683
+ {{ this.get_name() }}_data,
684
+ {{ this.get_name() }}_data.{{ this.object_path }}
685
+ ),
686
+ {
687
+ {%- if this.smooth_factor is not none %}
688
+ smoothFactor: {{ this.smooth_factor|tojson }},
689
+ {%- endif %}
690
+ }
691
+ ).addTo({{ this._parent.get_name() }});
692
+ {{ this.get_name() }}.setStyle(function(feature) {
693
+ return feature.properties.style;
694
+ });
711
695
{% endmacro %}
712
696
""" ) # noqa
713
697
@@ -743,11 +727,7 @@ def style_function(x):
743
727
self .add_child (Tooltip (tooltip ))
744
728
745
729
def style_data (self ):
746
- """
747
- Applies self.style_function to each feature of self.data and returns
748
- a corresponding JSON output.
749
-
750
- """
730
+ """Applies self.style_function to each feature of self.data."""
751
731
752
732
def recursive_get (data , keys ):
753
733
if len (keys ):
@@ -758,10 +738,10 @@ def recursive_get(data, keys):
758
738
geometries = recursive_get (self .data , self .object_path .split ('.' ))['geometries' ] # noqa
759
739
for feature in geometries :
760
740
feature .setdefault ('properties' , {}).setdefault ('style' , {}).update (self .style_function (feature )) # noqa
761
- return json .dumps (self .data , sort_keys = True )
762
741
763
742
def render (self , ** kwargs ):
764
743
"""Renders the HTML representation of the element."""
744
+ self .style_data ()
765
745
super (TopoJson , self ).render (** kwargs )
766
746
767
747
figure = self .get_root ()
@@ -822,7 +802,7 @@ class GeoJsonTooltip(Tooltip):
822
802
This will use JavaScript's .toLocaleString() to format 'clean' values
823
803
as strings for the user's location; i.e. 1,000,000.00 comma separators,
824
804
float truncation, etc.
825
- \* Available for most of JavaScript's primitive types (any data you'll
805
+ Available for most of JavaScript's primitive types (any data you'll
826
806
serve into the template).
827
807
style: str, default None.
828
808
HTML inline style properties like font and colors. Will be applied to
@@ -852,11 +832,11 @@ class GeoJsonTooltip(Tooltip):
852
832
function(layer){
853
833
// Convert non-primitive to String.
854
834
let handleObject = (feature)=>typeof(feature)=='object' ? JSON.stringify(feature) : feature;
855
- let fields = {{ this.fields }};
856
- {% if this.aliases %}
857
- let aliases = {{ this.aliases }};
858
- {% endif %}
859
- return '<table{% if this.style %} style="{{ this.style}}" {% endif%}>' +
835
+ let fields = {{ this.fields|tojson }};
836
+ {%- if this.aliases %}
837
+ let aliases = {{ this.aliases|tojson }};
838
+ {%- endif %}
839
+ return '<table{% if this.style %} style={{ this.style|tojson }} {% endif%}>' +
860
840
String(
861
841
fields.map(
862
842
columnname=>
@@ -872,7 +852,7 @@ class GeoJsonTooltip(Tooltip):
872
852
{% if this.localize %}.toLocaleString(){% endif %}}</td></tr>`
873
853
).join(''))
874
854
+'</table>'
875
- }, {{ this.options }});
855
+ }, {{ this.options|tojson }});
876
856
{% endmacro %}
877
857
""" )
878
858
@@ -1207,28 +1187,23 @@ class DivIcon(MacroElement):
1207
1187
"""
1208
1188
1209
1189
_template = Template (u"""
1210
- {% macro script(this, kwargs) %}
1211
-
1212
- var {{this.get_name()}} = L.divIcon({
1213
- {% if this.icon_size %}iconSize: [{{this.icon_size[0]}},{{this.icon_size[1]}}],{% endif %}
1214
- {% if this.icon_anchor %}iconAnchor: [{{this.icon_anchor[0]}},{{this.icon_anchor[1]}}],{% endif %}
1215
- {% if this.popup_anchor %}popupAnchor: [{{this.popup_anchor[0]}},{{this.popup_anchor[1]}}],{% endif %}
1216
- {% if this.className %}className: '{{this.className}}',{% endif %}
1217
- {% if this.html %}html: `{{this.html}}`,{% endif %}
1218
- });
1219
- {{this._parent.get_name()}}.setIcon({{this.get_name()}});
1220
- {% endmacro %}
1221
- """ ) # noqa
1190
+ {% macro script(this, kwargs) %}
1191
+ var {{ this.get_name() }} = L.divIcon({{ this.options|tojson }});
1192
+ {{this._parent.get_name()}}.setIcon({{this.get_name()}});
1193
+ {% endmacro %}
1194
+ """ ) # noqa
1222
1195
1223
1196
def __init__ (self , html = None , icon_size = None , icon_anchor = None ,
1224
1197
popup_anchor = None , class_name = 'empty' ):
1225
1198
super (DivIcon , self ).__init__ ()
1226
1199
self ._name = 'DivIcon'
1227
- self .icon_size = icon_size
1228
- self .icon_anchor = icon_anchor
1229
- self .popup_anchor = popup_anchor
1230
- self .html = html
1231
- self .className = class_name
1200
+ self .options = parse_options (
1201
+ html = html ,
1202
+ icon_size = icon_size ,
1203
+ icon_ancher = icon_anchor ,
1204
+ popup_anchor = popup_anchor ,
1205
+ class_name = class_name ,
1206
+ )
1232
1207
1233
1208
1234
1209
class LatLngPopup (MacroElement ):
@@ -1305,58 +1280,47 @@ class CustomIcon(Icon):
1305
1280
output file.
1306
1281
* If array-like, it will be converted to PNG base64 string
1307
1282
and embedded in the output.
1308
- icon_size : tuple of 2 int
1283
+ icon_size : tuple of 2 int, optional
1309
1284
Size of the icon image in pixels.
1310
- icon_anchor : tuple of 2 int
1285
+ icon_anchor : tuple of 2 int, optional
1311
1286
The coordinates of the "tip" of the icon
1312
1287
(relative to its top left corner).
1313
1288
The icon will be aligned so that this point is at the
1314
1289
marker's geographical location.
1315
- shadow_image : string, file or array-like object
1290
+ shadow_image : string, file or array-like object, optional
1316
1291
The data for the shadow image. If not specified,
1317
1292
no shadow image will be created.
1318
- shadow_size : tuple of 2 int
1293
+ shadow_size : tuple of 2 int, optional
1319
1294
Size of the shadow image in pixels.
1320
- shadow_anchor : tuple of 2 int
1295
+ shadow_anchor : tuple of 2 int, optional
1321
1296
The coordinates of the "tip" of the shadow relative to its
1322
1297
top left corner (the same as icon_anchor if not specified).
1323
- popup_anchor : tuple of 2 int
1298
+ popup_anchor : tuple of 2 int, optional
1324
1299
The coordinates of the point from which popups will "open",
1325
1300
relative to the icon anchor.
1326
1301
1327
1302
"""
1328
1303
_template = Template (u"""
1329
- {% macro script(this, kwargs) %}
1330
-
1331
- var {{this.get_name()}} = L.icon({
1332
- iconUrl: '{{this.icon_url}}',
1333
- {% if this.icon_size %}iconSize: [{{this.icon_size[0]}},{{this.icon_size[1]}}],{% endif %}
1334
- {% if this.icon_anchor %}iconAnchor: [{{this.icon_anchor[0]}},{{this.icon_anchor[1]}}],{% endif %}
1335
-
1336
- {% if this.shadow_url %}shadowUrl: '{{this.shadow_url}}',{% endif %}
1337
- {% if this.shadow_size %}shadowSize: [{{this.shadow_size[0]}},{{this.shadow_size[1]}}],{% endif %}
1338
- {% if this.shadow_anchor %}shadowAnchor: [{{this.shadow_anchor[0]}},{{this.shadow_anchor[1]}}],{% endif %}
1339
-
1340
- {% if this.popup_anchor %}popupAnchor: [{{this.popup_anchor[0]}},{{this.popup_anchor[1]}}],{% endif %}
1341
- });
1342
- {{this._parent.get_name()}}.setIcon({{this.get_name()}});
1343
- {% endmacro %}
1344
- """ ) # noqa
1304
+ {% macro script(this, kwargs) %}
1305
+ var {{ this.get_name() }} = L.icon({{ this.options|tojson }});
1306
+ {{ this._parent.get_name() }}.setIcon({{ this.get_name() }});
1307
+ {% endmacro %}
1308
+ """ ) # noqa
1345
1309
1346
1310
def __init__ (self , icon_image , icon_size = None , icon_anchor = None ,
1347
1311
shadow_image = None , shadow_size = None , shadow_anchor = None ,
1348
1312
popup_anchor = None ):
1349
1313
super (Icon , self ).__init__ ()
1350
1314
self ._name = 'CustomIcon'
1351
- self .icon_url = image_to_url ( icon_image )
1352
- self . icon_size = icon_size
1353
- self . icon_anchor = icon_anchor
1354
-
1355
- self . shadow_url = ( image_to_url (shadow_image )
1356
- if shadow_image is not None else None )
1357
- self . shadow_size = shadow_size
1358
- self . shadow_anchor = shadow_anchor
1359
- self . popup_anchor = popup_anchor
1315
+ self .options = parse_options (
1316
+ icon_url = image_to_url ( icon_image ),
1317
+ icon_size = icon_size ,
1318
+ icon_anchor = icon_anchor ,
1319
+ shadow_url = shadow_image and image_to_url (shadow_image ),
1320
+ shadow_size = shadow_size ,
1321
+ shadow_anchor = shadow_anchor ,
1322
+ popup_anchor = popup_anchor ,
1323
+ )
1360
1324
1361
1325
1362
1326
class ColorLine (FeatureGroup ):
0 commit comments