diff --git a/CHANGES.txt b/CHANGES.txt index d9421c35c6..2102dfb68b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,6 +13,7 @@ - Added `smooth_factor `option to `GeoJSON`, `TopoJSON` and `Choropleth` (JamesGardiner #428) - `Map` object now accepts Leaflet global switches (sgvandijk #424) - Added weight option to CircleMarker (palewire #581) +- Added requests support to enable http(s) and ftp for geo_path parameter (jreades #602) Bug Fixes diff --git a/folium/folium.py b/folium/folium.py index c0ad0a8cf6..587b330374 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -15,6 +15,7 @@ from .map import LegacyMap, FitBounds from .features import GeoJson, TopoJson +import requests class Map(LegacyMap): """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', # Create GeoJson object if geo_path: - geo_data = open(geo_path) + if geo_path.lower().startswith(('http:', 'ftp:', 'https:')): + geo_data = requests.get(geo_path).json() + else: + geo_data = open(geo_path) elif geo_str: geo_data = geo_str else: diff --git a/requirements.txt b/requirements.txt index 7789bb60f6..affaaf5d65 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Jinja2 branca six +requests diff --git a/tests/test_folium.py b/tests/test_folium.py index 99798ba5d9..696003c627 100644 --- a/tests/test_folium.py +++ b/tests/test_folium.py @@ -27,6 +27,11 @@ rootpath = os.path.abspath(os.path.dirname(__file__)) +# For testing remote requests +remote_url = '/'.join([ + 'https://raw.githubusercontent.com', + 'python-visualization/folium/master', + 'examples/data/us-states.json']) def setup_data(): """Import economic data for testing.""" @@ -448,3 +453,15 @@ def test_global_switches(self): assert (mapd.global_switches.prefer_canvas is True and mapd.global_switches.no_touch is True and mapd.global_switches.disable_3d is True) + + def test_json_request(self): + """Test requests for remote GeoJSON files.""" + self.map = folium.Map(zoom_start=4) + + # Adding remote GeoJSON as additional layer. + self.map.choropleth(geo_path=remote_url, + smooth_factor=0.5) + + self.map._parent.render() + bounds = self.map.get_bounds() + assert bounds == [[18.948267, -178.123152], [71.351633, 173.304726]], bounds