17
17
from .element import Element , Figure , JavascriptLink , CssLink , MacroElement
18
18
from .map import Layer , Icon , Marker , Popup
19
19
20
+
20
21
class WmsTileLayer (Layer ):
21
22
def __init__ (self , url , name = None ,
22
23
format = None , layers = None , transparent = True ,
@@ -49,6 +50,7 @@ def __init__(self, url, name=None,
49
50
{% endmacro %}
50
51
""" ) # noqa
51
52
53
+
52
54
class RegularPolygonMarker (Marker ):
53
55
def __init__ (self , location , color = 'black' , opacity = 1 , weight = 2 ,
54
56
fill_color = 'blue' , fill_opacity = 1 ,
@@ -128,6 +130,7 @@ def render(self, **kwargs):
128
130
JavascriptLink ("https://cdnjs.cloudflare.com/ajax/libs/leaflet-dvf/0.2/leaflet-dvf.markers.min.js" ), # noqa
129
131
name = 'dvf_js' )
130
132
133
+
131
134
class Vega (Element ):
132
135
def __init__ (self , data , width = None , height = None ,
133
136
left = "0%" , top = "0%" , position = 'relative' ):
@@ -137,13 +140,13 @@ def __init__(self, data, width=None, height=None,
137
140
"""
138
141
super (Vega , self ).__init__ ()
139
142
self ._name = 'Vega'
140
- self .data = data .to_json () if hasattr (data ,'to_json' ) else data
141
- if isinstance (self .data ,text_type ) or isinstance (data ,binary_type ):
143
+ self .data = data .to_json () if hasattr (data , 'to_json' ) else data
144
+ if isinstance (self .data , text_type ) or isinstance (data , binary_type ):
142
145
self .data = json .loads (self .data )
143
146
144
147
# Size Parameters.
145
- self .width = _parse_size (self .data .get ('width' ,'100%' ) if width is None else width )
146
- self .height = _parse_size (self .data .get ('height' ,'100%' ) if height is None else height )
148
+ self .width = _parse_size (self .data .get ('width' , '100%' ) if width is None else width ) # noqa
149
+ self .height = _parse_size (self .data .get ('height' , '100%' ) if height is None else height ) # noqa
147
150
self .left = _parse_size (left )
148
151
self .top = _parse_size (top )
149
152
self .position = position
@@ -191,6 +194,7 @@ def render(self, **kwargs):
191
194
vg.parse.spec(spec, function(chart) { chart({el:div}).update(); });}""" ), # noqa
192
195
name = 'vega_parse' )
193
196
197
+
194
198
class GeoJson (MacroElement ):
195
199
def __init__ (self , data , style_function = None ):
196
200
"""
@@ -220,20 +224,23 @@ def __init__(self, data, style_function=None):
220
224
>>> # Providing string.
221
225
>>> GeoJson(open('foo.json').read())
222
226
223
- >>> # Providing a style_function that put all states in green, but Alabama in blue.
224
- >>> style_function=lambda x: {'fillColor': '#0000ff' if x['properties']['name']=='Alabama' else '#00ff00'}
227
+ >>> # Provide a style_function that color all states green but Alabama.
228
+ >>> gree, blue = '#0000ff', '#00ff00'
229
+ >>> style_function = lambda x: {'fillColor': greem if
230
+ ... x['properties']['name']=='Alabama' else
231
+ ... blue}
225
232
>>> GeoJson(geojson, style_function=style_function)
226
233
"""
227
234
super (GeoJson , self ).__init__ ()
228
235
self ._name = 'GeoJson'
229
- if hasattr (data ,'read' ):
236
+ if hasattr (data , 'read' ):
230
237
self .embed = True
231
238
self .data = json .load (data )
232
- elif isinstance (data ,dict ):
239
+ elif isinstance (data , dict ):
233
240
self .embed = True
234
241
self .data = data
235
242
elif isinstance (data , text_type ) or isinstance (data , binary_type ):
236
- if data .lstrip ()[0 ] in '[{' : # This is a GeoJSON inline string
243
+ if data .lstrip ()[0 ] in '[{' : # This is a GeoJSON inline string.
237
244
self .embed = True
238
245
self .data = json .loads (data )
239
246
else : # This is a filename
@@ -242,18 +249,19 @@ def __init__(self, data, style_function=None):
242
249
elif data .__class__ .__name__ in ['GeoDataFrame' , 'GeoSeries' ]:
243
250
self .embed = True
244
251
if hasattr (data , '__geo_interface__' ):
245
- # We have a GeoPandas 0.2 object
246
- self .data = json .loads (json .dumps (data .to_crs (epsg = '4326' ).__geo_interface__ ))
252
+ # We have a GeoPandas 0.2 object.
253
+ self .data = json .loads (json .dumps (data .to_crs (epsg = '4326' ).__geo_interface__ )) # noqa
247
254
elif hasattr (data , 'columns' ):
248
255
# We have a GeoDataFrame 0.1
249
256
self .data = json .loads (data .to_crs (epsg = '4326' ).to_json ())
250
257
else :
251
- raise ValueError ('Unable to transform this object to a GeoJSON.' )
258
+ msg = 'Unable to transform this object to a GeoJSON.'
259
+ raise ValueError (msg )
252
260
else :
253
261
raise ValueError ('Unhandled object {!r}.' .format (data ))
254
262
255
263
if style_function is None :
256
- style_function = lambda x : {}
264
+ def style_function ( x ): return {}
257
265
self .style_function = style_function
258
266
259
267
self ._template = Template (u"""
@@ -268,14 +276,13 @@ def __init__(self, data, style_function=None):
268
276
def style_data (self ):
269
277
if 'features' not in self .data .keys ():
270
278
# Catch case when GeoJSON is just a single Feature or a geometry.
271
- if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()):
279
+ if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()): # noqa
272
280
# Catch case when GeoJSON is just a geometry.
273
- self .data = {'type' : 'Feature' , 'geometry' : self .data }
274
- self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
281
+ self .data = {'type' : 'Feature' , 'geometry' : self .data }
282
+ self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
275
283
276
284
for feature in self .data ['features' ]:
277
- feature .setdefault ('properties' ,{}).setdefault ('style' ,{}).update (
278
- self .style_function (feature ))
285
+ feature .setdefault ('properties' , {}).setdefault ('style' , {}).update (self .style_function (feature )) # noqa
279
286
return json .dumps (self .data )
280
287
281
288
def _get_self_bounds (self ):
@@ -287,14 +294,14 @@ def _get_self_bounds(self):
287
294
288
295
if 'features' not in self .data .keys ():
289
296
# Catch case when GeoJSON is just a single Feature or a geometry.
290
- if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()):
297
+ if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()): # noqa
291
298
# Catch case when GeoJSON is just a geometry.
292
- self .data = {'type' : 'Feature' , 'geometry' : self .data }
293
- self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
299
+ self .data = {'type' : 'Feature' , 'geometry' : self .data }
300
+ self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
294
301
295
- bounds = [[None ,None ],[None ,None ]]
302
+ bounds = [[None , None ], [None , None ]]
296
303
for feature in self .data ['features' ]:
297
- for point in iter_points (feature .get ('geometry' ,{}).get ('coordinates' ,{})):
304
+ for point in iter_points (feature .get ('geometry' , {}).get ('coordinates' , {})): # noqa
298
305
bounds = [
299
306
[
300
307
none_min (bounds [0 ][0 ], point [1 ]),
@@ -307,6 +314,7 @@ def _get_self_bounds(self):
307
314
]
308
315
return bounds
309
316
317
+
310
318
class TopoJson (MacroElement ):
311
319
def __init__ (self , data , object_path ):
312
320
"""
@@ -357,10 +365,10 @@ def _get_self_bounds(self):
357
365
358
366
data = json .loads (self .data )
359
367
360
- xmin ,xmax ,ymin ,ymax = None , None , None , None
368
+ xmin , xmax , ymin , ymax = None , None , None , None
361
369
362
370
for arc in data ['arcs' ]:
363
- x ,y = 0 ,0
371
+ x , y = 0 , 0
364
372
for dx , dy in arc :
365
373
x += dx
366
374
y += dy
@@ -370,16 +378,17 @@ def _get_self_bounds(self):
370
378
ymax = none_max (y , ymax )
371
379
return [
372
380
[
373
- data ['transform' ]['translate' ][0 ] + data ['transform' ]['scale' ][0 ] * xmin ,
374
- data ['transform' ]['translate' ][1 ] + data ['transform' ]['scale' ][1 ] * ymin ,
381
+ data ['transform' ]['translate' ][0 ] + data ['transform' ]['scale' ][0 ] * xmin , # noqa
382
+ data ['transform' ]['translate' ][1 ] + data ['transform' ]['scale' ][1 ] * ymin , # noqa
375
383
],
376
384
[
377
- data ['transform' ]['translate' ][0 ] + data ['transform' ]['scale' ][0 ] * xmax ,
378
- data ['transform' ]['translate' ][1 ] + data ['transform' ]['scale' ][1 ] * ymax ,
385
+ data ['transform' ]['translate' ][0 ] + data ['transform' ]['scale' ][0 ] * xmax , # noqa
386
+ data ['transform' ]['translate' ][1 ] + data ['transform' ]['scale' ][1 ] * ymax , # noqa
379
387
]
380
388
381
389
]
382
390
391
+
383
392
class ColorScale (MacroElement ):
384
393
def __init__ (self , color_domain , color_code , caption = "" ):
385
394
"""
@@ -409,6 +418,7 @@ def render(self, **kwargs):
409
418
JavascriptLink ("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" ), # noqa
410
419
name = 'd3' )
411
420
421
+
412
422
class MarkerCluster (Layer ):
413
423
"""Adds a MarkerCluster layer on the map."""
414
424
def __init__ (self , overlay = True , control = True ):
@@ -533,6 +543,7 @@ def __init__(self, location, radius=500, color='black',
533
543
{% endmacro %}
534
544
""" )
535
545
546
+
536
547
class LatLngPopup (MacroElement ):
537
548
def __init__ (self ):
538
549
"""
@@ -651,6 +662,7 @@ def _get_self_bounds(self):
651
662
]
652
663
return bounds
653
664
665
+
654
666
class MultiPolyLine (MacroElement ):
655
667
def __init__ (self , locations , color = None , weight = None ,
656
668
opacity = None , latlon = True , popup = None ):
@@ -697,7 +709,9 @@ def __init__(self, locations, color=None, weight=None,
697
709
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
698
710
{% endmacro %}
699
711
""" ) # noqa
700
- def _get_self_bounds (self ):
712
+
713
+
714
+ def _get_self_bounds (self ):
701
715
"""Computes the bounds of the object itself (not including it's children)
702
716
in the form [[lat_min, lon_min], [lat_max, lon_max]]
703
717
"""
@@ -715,6 +729,7 @@ def _get_self_bounds(self):
715
729
]
716
730
return bounds
717
731
732
+
718
733
class CustomIcon (Icon ):
719
734
def __init__ (self , icon_image , icon_size = None , icon_anchor = None ,
720
735
shadow_image = None , shadow_size = None , shadow_anchor = None ,
0 commit comments