Skip to content

Added ability to use remote geo_path #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Jinja2
branca
six
requests
17 changes: 17 additions & 0 deletions tests/test_folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one 👍


def setup_data():
"""Import economic data for testing."""
Expand Down Expand Up @@ -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