Skip to content

Commit 7642504

Browse files
committed
Moved marker iteration to decorator for more flexibility
1 parent cddaab3 commit 7642504

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

folium/folium.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@
1111
from __future__ import division
1212
import codecs
1313
import json
14+
import functools
1415
from jinja2 import Environment, PackageLoader
1516
from pkg_resources import resource_string, resource_filename
1617
import utilities
1718

1819

20+
def iter_obj(type):
21+
'''Decorator to keep count of different map object types in self.mk_cnt'''
22+
def decorator(func):
23+
@functools.wraps(func)
24+
def wrapper(self, *args, **kwargs):
25+
self.mark_cnt[type] = self.mark_cnt.get(type, 0) + 1
26+
func_result = func(self, *args, **kwargs)
27+
return func_result
28+
return wrapper
29+
return decorator
30+
31+
1932
class Map(object):
2033
'''Create a Map with Folium'''
2134

@@ -129,6 +142,7 @@ def __init__(self, location=None, width=960, height=500,
129142
self.template_vars['attr'] = unicode(attr, 'utf8')
130143
self.tile_types.update({'Custom': {'template': tiles, 'attr': attr}})
131144

145+
@iter_obj('simple')
132146
def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
133147
'''Create a simple stock Leaflet marker on the map, with optional
134148
popup text or Vincent visualization.
@@ -153,7 +167,7 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
153167
>>>map.simple_marker(location=[45.5, -122.3], popup=(vis, 'vis.json'))
154168
155169
'''
156-
self.mark_cnt['simple'] = count = self.mark_cnt.get('simple', 0) + 1
170+
count = self.mark_cnt['simple']
157171

158172
mark_temp = self.env.get_template('simple_marker.js')
159173

@@ -171,6 +185,7 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
171185
popup_out,
172186
add_mark))
173187

188+
@iter_obj('circle')
174189
def circle_marker(self, location=None, radius=500, popup='Pop Text',
175190
popup_on=True, line_color='black', fill_color='black',
176191
fill_opacity=0.6):
@@ -207,7 +222,7 @@ def circle_marker(self, location=None, radius=500, popup='Pop Text',
207222
radius=1000, popup=(bar_chart, 'bar_data.json'))
208223
209224
'''
210-
self.mark_cnt['circle'] = count = self.mark_cnt.get('circle', 0) + 1
225+
count = self.mark_cnt['circle']
211226

212227
circle_temp = self.env.get_template('circle_marker.js')
213228

@@ -228,6 +243,7 @@ def circle_marker(self, location=None, radius=500, popup='Pop Text',
228243
popup_out,
229244
add_mark))
230245

246+
@iter_obj('polygon')
231247
def polygon_marker(self, location=None, line_color='black', line_opacity=1,
232248
line_weight=2, fill_color='blue', fill_opacity=1,
233249
num_sides=4, rotation=0, radius=15, popup='Pop Text',
@@ -267,7 +283,7 @@ def polygon_marker(self, location=None, line_color='black', line_opacity=1,
267283
268284
'''
269285

270-
self.mark_cnt['polygon'] = count = self.mark_cnt.get('polygon', 0) + 1
286+
count = self.mark_cnt['polygon']
271287

272288
poly_temp = self.env.get_template('poly_marker.js')
273289

@@ -379,6 +395,7 @@ def _popup_render(self, popup=None, mk_name=None, count=None,
379395
'json_out': json_out,
380396
'vega_id': vega_id})
381397

398+
@iter_obj('geojson')
382399
def geo_json(self, geo_path=None, data_out='data.json', data=None,
383400
columns=None, key_on=None, threshold_scale=None,
384401
fill_color='blue', fill_opacity=0.6, line_color='black',
@@ -467,7 +484,7 @@ def geo_json(self, geo_path=None, data_out='data.json', data=None,
467484
'gjson_layers', 'map_legends', 'topo_convert']
468485
for var in reset_vars:
469486
self.template_vars.update({var: []})
470-
self.mark_cnt['geojson'] = 0
487+
self.mark_cnt['geojson'] = 1
471488

472489
def json_style(style_cnt, line_color, line_weight, line_opacity,
473490
fill_color, fill_opacity, quant_fill):
@@ -485,9 +502,6 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,
485502
#Set map type to geojson
486503
self.map_type = 'geojson'
487504

488-
#Set counter for GeoJSON and set iterations
489-
self.mark_cnt['geojson'] = self.mark_cnt.get('geojson', 0) + 1
490-
491505
#Get JSON map layer template pieces, convert TopoJSON if necessary
492506
geo_path = ".defer(d3.json, '{0}')".format(geo_path)
493507
if topojson is None:

0 commit comments

Comments
 (0)