1616from .map import Map as _Map
1717from .map import Icon , Marker , Popup , FitBounds
1818from .features import (WmsTileLayer , RegularPolygonMarker , Vega , GeoJson ,
19- GeoJsonStyle , CircleMarker , LatLngPopup ,
19+ CircleMarker , LatLngPopup ,
2020 ClickForMarker , ColorScale , TopoJson , PolyLine ,
2121 MultiPolyLine , ImageOverlay )
2222from .utilities import color_brewer , write_png
@@ -442,10 +442,17 @@ def add_plugin(self, plugin):
442442#
443443# self.template_vars.update({'fit_bounds': fit_bounds_str.strip()})
444444
445- def geo_json (self , geo_path = None , geo_str = None , data_out = 'data.json' ,
445+ def geo_json (self , * args , ** kwargs ):
446+ """This method is deprecated and will be removed in v0.2.1. See
447+ `Map.choropleth` instead.
448+ """
449+ warnings .warn ('This method is deprecated. Please use Map.choropleth instead.' )
450+ return self .choropleth (* args , ** kwargs )
451+
452+ def choropleth (self , geo_path = None , geo_str = None , data_out = 'data.json' ,
446453 data = None , columns = None , key_on = None , threshold_scale = None ,
447454 fill_color = 'blue' , fill_opacity = 0.6 , line_color = 'black' ,
448- line_weight = 1 , line_opacity = 1 , legend_name = None ,
455+ line_weight = 1 , line_opacity = 1 , legend_name = "" ,
449456 topojson = None , reset = False ):
450457 """Apply a GeoJSON overlay to the map.
451458
@@ -506,8 +513,8 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
506513 GeoJSON geopath line weight.
507514 line_opacity: float, default 1
508515 GeoJSON geopath line opacity, range 0-1.
509- legend_name: string, default None
510- Title for data legend. If not passed, defaults to columns[1].
516+ legend_name: string, default empty string
517+ Title for data legend.
511518 topojson: string, default None
512519 If using a TopoJSON, passing "objects.yourfeature" to the topojson
513520 keyword argument will enable conversion to GeoJSON.
@@ -520,17 +527,14 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
520527
521528 Example
522529 -------
523- >>> m.geo_json (geo_path='us-states.json', line_color='blue',
530+ >>> m.choropleth (geo_path='us-states.json', line_color='blue',
524531 line_weight=3)
525- >>> m.geo_json (geo_path='geo.json', data=df,
532+ >>> m.choropleth (geo_path='geo.json', data=df,
526533 columns=['Data 1', 'Data 2'],
527534 key_on='feature.properties.myvalue', fill_color='PuBu',
528535 threshold_scale=[0, 20, 30, 40, 50, 60])
529- >>> m.geo_json (geo_path='countries.json', topojson='objects.countries')
536+ >>> m.choropleth (geo_path='countries.json', topojson='objects.countries')
530537 """
531- warnings .warn ("%s is deprecated. Use %s instead" %
532- ("geo_json" , "add_children(GeoJson)" ),
533- FutureWarning , stacklevel = 2 )
534538
535539 if threshold_scale and len (threshold_scale ) > 6 :
536540 raise ValueError
@@ -546,11 +550,6 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
546550 else :
547551 geo_data = {}
548552
549- if topojson :
550- geo_json = TopoJson (geo_data , topojson )
551- else :
552- geo_json = GeoJson (geo_data )
553-
554553 # Create color_data dict
555554 if hasattr (data , 'set_index' ):
556555 # This is a pd.DataFrame
@@ -587,20 +586,38 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
587586 color_domain = [data_min + i * (data_max - data_min )* 1. / nb_class
588587 for i in range (1 + nb_class )]
589588 else :
590- color_domain = [- 1 , 1 ]
589+ color_domain = None
590+
591+ if color_domain and key_on :
592+ key_on = key_on [8 :] if key_on .startswith ('feature.' ) else key_on
593+ color_range = color_brewer (fill_color , n = len (color_domain ))
594+ get_by_key = lambda obj ,key : (obj .get (key ,None ) if len (key .split ('.' ))<= 1
595+ else get_by_key (obj .get (key .split ('.' )[0 ],None ),
596+ '.' .join (key .split ('.' )[1 :])))
597+ color_scale_fun = lambda x : color_range [len ([u for u in color_domain
598+ if u <= color_data [get_by_key (x ,key_on )]])]
599+ else :
600+ color_scale_fun = lambda x : fill_color
591601
592- # Create GeoJsonStyle.
593- geo_json_style = GeoJsonStyle (
594- color_domain , fill_color , color_data = color_data , key_on = key_on ,
595- weight = line_weight , opacity = line_opacity , color = line_color ,
596- fill_opacity = fill_opacity )
602+ style_function = lambda x : {
603+ "weight" : line_weight ,
604+ "opactiy" : line_opacity ,
605+ "color" : line_color ,
606+ "fillOpacity" : fill_opacity ,
607+ "fillColor" : color_scale_fun (x )
608+ }
597609
598- # Create ColorScale
599- color_scale = ColorScale (color_domain , fill_color , caption = legend_name )
610+ if topojson :
611+ geo_json = TopoJson (geo_data , topojson )
612+ else :
613+ geo_json = GeoJson (geo_data , style_function = style_function )
600614
601- geo_json .add_children (geo_json_style )
602615 self .add_children (geo_json )
603- self .add_children (color_scale )
616+
617+ # Create ColorScale.
618+ if color_domain :
619+ color_scale = ColorScale (color_domain , fill_color , caption = legend_name )
620+ self .add_children (color_scale )
604621
605622 def image_overlay (self , data , opacity = 0.25 , min_lat = - 90.0 , max_lat = 90.0 ,
606623 min_lon = - 180.0 , max_lon = 180.0 , origin = 'upper' ,
0 commit comments