From 0a7dafb07844048bc345ee0655df43a4ca99b032 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Mon, 6 Mar 2017 21:05:59 +0000 Subject: [PATCH 1/9] Added ability to use remote URL for GeoJSON --- folium/folium.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/folium/folium.py b/folium/folium.py index c0ad0a8cf6..45382b1edc 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,11 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json', # Create GeoJson object if geo_path: - geo_data = open(geo_path) + print("Using new folium") + 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: From 5ec18b1c200500c098d5ebb9dd6fce35f91afeab Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Mon, 6 Mar 2017 21:07:41 +0000 Subject: [PATCH 2/9] Ooops, forgot to remove debugging message --- folium/folium.py | 1 - 1 file changed, 1 deletion(-) diff --git a/folium/folium.py b/folium/folium.py index 45382b1edc..2c8b4167d4 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -252,7 +252,6 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json', # Create GeoJson object if geo_path: - print("Using new folium") if geo_path.lower().startswith(('http','ftp','https')): geo_data = requests.get(geo_path).json() else: From c5c9dc4e93ea66b419ee0417d33d59b8d4c156b0 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Mon, 6 Mar 2017 21:14:15 +0000 Subject: [PATCH 3/9] Fixing stickler stuff --- folium/folium.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/folium/folium.py b/folium/folium.py index 2c8b4167d4..3210d261e3 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -252,9 +252,9 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json', # Create GeoJson object if geo_path: - if geo_path.lower().startswith(('http','ftp','https')): + if geo_path.lower().startswith(('http', 'ftp', 'https')): geo_data = requests.get(geo_path).json() - else: + else: geo_data = open(geo_path) elif geo_str: geo_data = geo_str From b37df58ec6e910faa878c1c0700d382acc5131d9 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 11:13:01 +0000 Subject: [PATCH 4/9] Adding details to CHANGES and requirements. Tweaked pattern for startswith on URL checking. --- CHANGES.txt | 1 + folium/folium.py | 2 +- requirements.txt | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index d9421c35c6..43a50e4dcd 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 Bug Fixes diff --git a/folium/folium.py b/folium/folium.py index 3210d261e3..8dcb05ad7a 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -252,7 +252,7 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json', # Create GeoJson object if geo_path: - if geo_path.lower().startswith(('http', 'ftp', 'https')): + if geo_path.lower().startswith(('http:', 'ftp':, 'https:')): geo_data = requests.get(geo_path).json() else: geo_data = open(geo_path) 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 From 3a8b75f761fa0f942023feba601fde22398386b4 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 11:15:47 +0000 Subject: [PATCH 5/9] Grrrr, typo. --- folium/folium.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/folium/folium.py b/folium/folium.py index 8dcb05ad7a..587b330374 100644 --- a/folium/folium.py +++ b/folium/folium.py @@ -252,7 +252,7 @@ def choropleth(self, geo_path=None, geo_str=None, data_out='data.json', # Create GeoJson object if geo_path: - if geo_path.lower().startswith(('http:', 'ftp':, 'https:')): + if geo_path.lower().startswith(('http:', 'ftp:', 'https:')): geo_data = requests.get(geo_path).json() else: geo_data = open(geo_path) From d6fc34ae2ba210cb7ea42bb30646b88435b229bc Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 14:30:15 +0000 Subject: [PATCH 6/9] Added test for remote JSON. --- tests/test_folium.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_folium.py b/tests/test_folium.py index 99798ba5d9..a1ac04c554 100644 --- a/tests/test_folium.py +++ b/tests/test_folium.py @@ -21,6 +21,8 @@ from six import PY3 import branca.element +import requests + import folium from folium.map import Popup, Marker, FitBounds, FeatureGroup from folium.features import TopoJson, RectangleMarker, PolygonMarker @@ -448,3 +450,16 @@ 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. + path = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json' + self.map.choropleth(geo_path=path, + smooth_factor=0.5) + + self.map._parent.render() + bounds = self.map.get_bounds() + assert bounds == [[18.948267, -178.123152], [71.351633, 173.304726]], bounds From 7d7c19af3873394c578d7ce6b7041de65f5cfda1 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 14:35:28 +0000 Subject: [PATCH 7/9] Responding to stickler feedback --- tests/test_folium.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_folium.py b/tests/test_folium.py index a1ac04c554..d4b33839c2 100644 --- a/tests/test_folium.py +++ b/tests/test_folium.py @@ -21,14 +21,17 @@ from six import PY3 import branca.element -import requests - import folium from folium.map import Popup, Marker, FitBounds, FeatureGroup from folium.features import TopoJson, RectangleMarker, PolygonMarker 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.""" @@ -456,8 +459,7 @@ def test_json_request(self): self.map = folium.Map(zoom_start=4) # Adding remote GeoJSON as additional layer. - path = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json' - self.map.choropleth(geo_path=path, + self.map.choropleth(geo_path=remote_url, smooth_factor=0.5) self.map._parent.render() From a151002006e041ff58e69e6d43b6768c904dece9 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 14:37:10 +0000 Subject: [PATCH 8/9] Responding to stickler feedback --- tests/test_folium.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_folium.py b/tests/test_folium.py index d4b33839c2..696003c627 100644 --- a/tests/test_folium.py +++ b/tests/test_folium.py @@ -29,9 +29,9 @@ # For testing remote requests remote_url = '/'.join([ - 'https://raw.githubusercontent.com', - 'python-visualization/folium/master', - 'examples/data/us-states.json']) + 'https://raw.githubusercontent.com', + 'python-visualization/folium/master', + 'examples/data/us-states.json']) def setup_data(): """Import economic data for testing.""" From 7b5ce5b2ed40e01835512ee8bc52dff935025e97 Mon Sep 17 00:00:00 2001 From: Jon Reades Date: Thu, 9 Mar 2017 14:46:19 +0000 Subject: [PATCH 9/9] Added handle and PR number. --- CHANGES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 43a50e4dcd..2102dfb68b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,7 +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 +- Added requests support to enable http(s) and ftp for geo_path parameter (jreades #602) Bug Fixes