Skip to content

Commit 9cf5de6

Browse files
committed
choropleth can take gdf
1 parent 9d851f5 commit 9cf5de6

3 files changed

Lines changed: 11 additions & 22 deletions

File tree

folium/features.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from jinja2 import Template
2323

24+
import requests
25+
2426
from six import binary_type, text_type
2527

2628

@@ -473,8 +475,11 @@ def __init__(self, data, style_function=None, name=None,
473475
self.embed = True
474476
self.data = data
475477
elif isinstance(data, text_type) or isinstance(data, binary_type):
476-
if data.lstrip()[0] in '[{': # This is a GeoJSON inline string
477-
self.embed = True
478+
self.embed = True
479+
if data.lower().startswith(('http:', 'ftp:', 'https:')):
480+
self.data = requests.get(data).json()
481+
print(data)
482+
elif data.lstrip()[0] in '[{': # This is a GeoJSON inline string
478483
self.data = json.loads(data)
479484
else: # This is a filename
480485
self.embed = False

folium/folium.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from folium.features import GeoJson, TopoJson
1717
from folium.map import FitBounds, LegacyMap
1818

19-
import requests
20-
2119

2220
class Map(LegacyMap):
2321
"""Create a Map with Folium and Leaflet.js
@@ -143,7 +141,7 @@ def fit_bounds(self, bounds, padding_top_left=None,
143141
)
144142
)
145143

146-
def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
144+
def choropleth(self, geo_data, data_out='data.json',
147145
data=None, columns=None, key_on=None, threshold_scale=None,
148146
fill_color='blue', fill_opacity=0.6, line_color='black',
149147
line_weight=1, line_opacity=1, legend_name='',
@@ -252,17 +250,6 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
252250
raise ValueError('Please pass a valid color brewer code to '
253251
'fill_local. See docstring for valid codes.')
254252

255-
# Create GeoJson object
256-
if geo_path:
257-
if geo_path.lower().startswith(('http:', 'ftp:', 'https:')):
258-
geo_data = requests.get(geo_path).json()
259-
else:
260-
geo_data = open(geo_path)
261-
elif geo_str:
262-
geo_data = geo_str
263-
else:
264-
geo_data = {}
265-
266253
# Create color_data dict
267254
if hasattr(data, 'set_index'):
268255
# This is a pd.DataFrame

tests/test_folium.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ def test_topo_json_smooth_factor(self):
291291
self.m = folium.Map([43, -100], zoom_start=4)
292292

293293
# Adding TopoJSON as additional layer.
294-
path = os.path.join(rootpath, 'or_counties_topo.json')
295-
self.m.choropleth(geo_path=path,
296-
topojson='objects.or_counties_geo',
297-
smooth_factor=0.5)
294+
with open(os.path.join(rootpath, 'or_counties_topo.json')) as f:
295+
self.m.choropleth(f, topojson='objects.or_counties_geo', smooth_factor=0.5)
298296

299297
out = self.m._parent.render()
300298

@@ -462,8 +460,7 @@ def test_json_request(self):
462460
self.m = folium.Map(zoom_start=4)
463461

464462
# Adding remote GeoJSON as additional layer.
465-
self.m.choropleth(geo_path=remote_url,
466-
smooth_factor=0.5)
463+
self.m.choropleth(remote_url, smooth_factor=0.5)
467464

468465
self.m._parent.render()
469466
bounds = self.m.get_bounds()

0 commit comments

Comments
 (0)