Skip to content

Commit 5e96485

Browse files
authored
Merge pull request #703 from ocefpaf/choropleth_take_gdf
Choropleth take gdf
2 parents 9d851f5 + 1f6b977 commit 5e96485

File tree

7 files changed

+174
-172
lines changed

7 files changed

+174
-172
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.4.0
22
~~~~~
3+
- Optional `iconCreateFunction` for `MarkerCluster` to customize the icons (odovad #701)
34
- Added `HeatMapWithTime` (Padarn #567)
45
- Added `MeasureControl` (ocefpaf #669)
56
- Added `VideoOverlay` plugin (ocefpaf #665)
@@ -14,6 +15,9 @@
1415

1516
API changes
1617

18+
- `choropleth` now takes a single `geo_data` instad of `geo_path`/`geo_str`
19+
leaving the parsing to `GeoJSON`, remove the unused `data_out` option,
20+
add geopandas support (ocefpaf #702)
1721
- All popups are considered HTML text by default (ocefpaf #689)
1822
If a popup requires rendering use the `kwarg` `parse_html=True`.
1923
- `PolyLine`, `Circle` and `CircleMarker` are set to leaflet's defaults and

examples/CheckZorder.ipynb

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

examples/GeoJSON_and_choropleth.ipynb

Lines changed: 108 additions & 90 deletions
Large diffs are not rendered by default.

examples/Quickstart.ipynb

Lines changed: 40 additions & 40 deletions
Large diffs are not rendered by default.

folium/features.py

Lines changed: 9 additions & 8 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

@@ -466,19 +468,18 @@ def __init__(self, data, style_function=None, name=None,
466468
super(GeoJson, self).__init__(name=name, overlay=overlay,
467469
control=control)
468470
self._name = 'GeoJson'
469-
if hasattr(data, 'read'):
470-
self.embed = True
471-
self.data = json.load(data)
472-
elif isinstance(data, dict):
471+
if isinstance(data, dict):
473472
self.embed = True
474473
self.data = data
475474
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
475+
self.embed = True
476+
if data.lower().startswith(('http:', 'ftp:', 'https:')):
477+
self.data = requests.get(data).json()
478+
elif data.lstrip()[0] in '[{': # This is a GeoJSON inline string
478479
self.data = json.loads(data)
479480
else: # This is a filename
480-
self.embed = False
481-
self.data = data
481+
with open(data) as f:
482+
self.data = json.loads(f.read())
482483
elif data.__class__.__name__ in ['GeoDataFrame', 'GeoSeries']:
483484
self.embed = True
484485
if hasattr(data, '__geo_interface__'):

folium/folium.py

Lines changed: 6 additions & 24 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,11 +141,10 @@ 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',
147-
data=None, columns=None, key_on=None, threshold_scale=None,
148-
fill_color='blue', fill_opacity=0.6, line_color='black',
149-
line_weight=1, line_opacity=1, legend_name='',
150-
topojson=None, reset=False, smooth_factor=None,
144+
def choropleth(self, geo_data, data=None, columns=None, key_on=None,
145+
threshold_scale=None, fill_color='blue', fill_opacity=0.6,
146+
line_color='black', line_weight=1, line_opacity=1,
147+
legend_name='', topojson=None, reset=False, smooth_factor=None,
151148
highlight=None):
152149
"""
153150
Apply a GeoJSON overlay to the map.
@@ -177,12 +174,8 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
177174
178175
Parameters
179176
----------
180-
geo_path: string, default None
181-
URL or File path to your GeoJSON data
182-
geo_str: string, default None
183-
String of GeoJSON, alternative to geo_path
184-
data_out: string, default 'data.json'
185-
Path to write Pandas DataFrame/Series to JSON if binding data
177+
geo_path: string/object
178+
URL, file path, or data (json, dict, geopandas, etc) to your GeoJSON geometries
186179
data: Pandas DataFrame or Series, default None
187180
Data to bind to the GeoJSON.
188181
columns: dict or tuple, default None
@@ -252,17 +245,6 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
252245
raise ValueError('Please pass a valid color brewer code to '
253246
'fill_local. See docstring for valid codes.')
254247

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-
266248
# Create color_data dict
267249
if hasattr(data, 'set_index'):
268250
# 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)