11
11
from __future__ import division
12
12
import codecs
13
13
import json
14
+ import functools
14
15
from jinja2 import Environment , PackageLoader
15
16
from pkg_resources import resource_string , resource_filename
16
17
import utilities
17
18
18
19
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
+
19
32
class Map (object ):
20
33
'''Create a Map with Folium'''
21
34
@@ -129,6 +142,7 @@ def __init__(self, location=None, width=960, height=500,
129
142
self .template_vars ['attr' ] = unicode (attr , 'utf8' )
130
143
self .tile_types .update ({'Custom' : {'template' : tiles , 'attr' : attr }})
131
144
145
+ @iter_obj ('simple' )
132
146
def simple_marker (self , location = None , popup = 'Pop Text' , popup_on = True ):
133
147
'''Create a simple stock Leaflet marker on the map, with optional
134
148
popup text or Vincent visualization.
@@ -153,7 +167,7 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
153
167
>>>map.simple_marker(location=[45.5, -122.3], popup=(vis, 'vis.json'))
154
168
155
169
'''
156
- self . mark_cnt [ 'simple' ] = count = self .mark_cnt . get ( 'simple' , 0 ) + 1
170
+ count = self .mark_cnt [ 'simple' ]
157
171
158
172
mark_temp = self .env .get_template ('simple_marker.js' )
159
173
@@ -171,6 +185,7 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
171
185
popup_out ,
172
186
add_mark ))
173
187
188
+ @iter_obj ('circle' )
174
189
def circle_marker (self , location = None , radius = 500 , popup = 'Pop Text' ,
175
190
popup_on = True , line_color = 'black' , fill_color = 'black' ,
176
191
fill_opacity = 0.6 ):
@@ -207,7 +222,7 @@ def circle_marker(self, location=None, radius=500, popup='Pop Text',
207
222
radius=1000, popup=(bar_chart, 'bar_data.json'))
208
223
209
224
'''
210
- self . mark_cnt [ 'circle' ] = count = self .mark_cnt . get ( 'circle' , 0 ) + 1
225
+ count = self .mark_cnt [ 'circle' ]
211
226
212
227
circle_temp = self .env .get_template ('circle_marker.js' )
213
228
@@ -228,6 +243,7 @@ def circle_marker(self, location=None, radius=500, popup='Pop Text',
228
243
popup_out ,
229
244
add_mark ))
230
245
246
+ @iter_obj ('polygon' )
231
247
def polygon_marker (self , location = None , line_color = 'black' , line_opacity = 1 ,
232
248
line_weight = 2 , fill_color = 'blue' , fill_opacity = 1 ,
233
249
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,
267
283
268
284
'''
269
285
270
- self . mark_cnt [ 'polygon' ] = count = self .mark_cnt . get ( 'polygon' , 0 ) + 1
286
+ count = self .mark_cnt [ 'polygon' ]
271
287
272
288
poly_temp = self .env .get_template ('poly_marker.js' )
273
289
@@ -379,6 +395,7 @@ def _popup_render(self, popup=None, mk_name=None, count=None,
379
395
'json_out' : json_out ,
380
396
'vega_id' : vega_id })
381
397
398
+ @iter_obj ('geojson' )
382
399
def geo_json (self , geo_path = None , data_out = 'data.json' , data = None ,
383
400
columns = None , key_on = None , threshold_scale = None ,
384
401
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,
467
484
'gjson_layers' , 'map_legends' , 'topo_convert' ]
468
485
for var in reset_vars :
469
486
self .template_vars .update ({var : []})
470
- self .mark_cnt ['geojson' ] = 0
487
+ self .mark_cnt ['geojson' ] = 1
471
488
472
489
def json_style (style_cnt , line_color , line_weight , line_opacity ,
473
490
fill_color , fill_opacity , quant_fill ):
@@ -485,9 +502,6 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,
485
502
#Set map type to geojson
486
503
self .map_type = 'geojson'
487
504
488
- #Set counter for GeoJSON and set iterations
489
- self .mark_cnt ['geojson' ] = self .mark_cnt .get ('geojson' , 0 ) + 1
490
-
491
505
#Get JSON map layer template pieces, convert TopoJSON if necessary
492
506
geo_path = ".defer(d3.json, '{0}')" .format (geo_path )
493
507
if topojson is None :
0 commit comments