Skip to content

Commit 6753324

Browse files
authored
Merge pull request #602 from jreades/master
Added ability to use remote geo_path
2 parents a9e97b0 + 7b5ce5b commit 6753324

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Added `smooth_factor `option to `GeoJSON`, `TopoJSON` and `Choropleth` (JamesGardiner #428)
1414
- `Map` object now accepts Leaflet global switches (sgvandijk #424)
1515
- Added weight option to CircleMarker (palewire #581)
16+
- Added requests support to enable http(s) and ftp for geo_path parameter (jreades #602)
1617

1718
Bug Fixes
1819

folium/folium.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .map import LegacyMap, FitBounds
1616
from .features import GeoJson, TopoJson
1717

18+
import requests
1819

1920
class Map(LegacyMap):
2021
"""Create a Map with Folium and Leaflet.js
@@ -251,7 +252,10 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json',
251252

252253
# Create GeoJson object
253254
if geo_path:
254-
geo_data = open(geo_path)
255+
if geo_path.lower().startswith(('http:', 'ftp:', 'https:')):
256+
geo_data = requests.get(geo_path).json()
257+
else:
258+
geo_data = open(geo_path)
255259
elif geo_str:
256260
geo_data = geo_str
257261
else:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Jinja2
22
branca
33
six
4+
requests

tests/test_folium.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
rootpath = os.path.abspath(os.path.dirname(__file__))
2929

30+
# For testing remote requests
31+
remote_url = '/'.join([
32+
'https://raw.githubusercontent.com',
33+
'python-visualization/folium/master',
34+
'examples/data/us-states.json'])
3035

3136
def setup_data():
3237
"""Import economic data for testing."""
@@ -448,3 +453,15 @@ def test_global_switches(self):
448453
assert (mapd.global_switches.prefer_canvas is True and
449454
mapd.global_switches.no_touch is True and
450455
mapd.global_switches.disable_3d is True)
456+
457+
def test_json_request(self):
458+
"""Test requests for remote GeoJSON files."""
459+
self.map = folium.Map(zoom_start=4)
460+
461+
# Adding remote GeoJSON as additional layer.
462+
self.map.choropleth(geo_path=remote_url,
463+
smooth_factor=0.5)
464+
465+
self.map._parent.render()
466+
bounds = self.map.get_bounds()
467+
assert bounds == [[18.948267, -178.123152], [71.351633, 173.304726]], bounds

0 commit comments

Comments
 (0)