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

Conversation

jreades
Copy link
Contributor

@jreades jreades commented Mar 6, 2017

Adds a dependency (requests) but enables ability to use http/https/ftp for loading geo_paths.

I'm really rusty on unit tests (last did them in Java 1.2 or thereabouts) so I've not attempted to implement these. This is the first time I've done this, so please review carefully before accepting the PR.

folium/folium.py Outdated
@@ -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')):

Choose a reason for hiding this comment

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

E231 missing whitespace after ','
W291 trailing whitespace

folium/folium.py Outdated
geo_data = open(geo_path)
if geo_path.lower().startswith(('http','ftp','https')):
geo_data = requests.get(geo_path).json()
else:

Choose a reason for hiding this comment

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

W291 trailing whitespace

folium/folium.py Outdated
@@ -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')):
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably should include the colon, too; someone could have a file that starts with any of these sequences.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it is an overkill but how about something like:

def scheme_validator(url):
    parsed = urlparse(url)
    if parsed.scheme in ['http', 'ftp', 'https']:
        return True
    return False

to validade the scheme?

@ocefpaf ocefpaf mentioned this pull request Mar 7, 2017
@ocefpaf
Copy link
Member

ocefpaf commented Mar 7, 2017

Adds a dependency (requests) but enables ability to use http/https/ftp for loading geo_paths.

I am fine with the extra dependency. We could use a try/except to catch it when unavailable and issue a proper error message, but I don't think it is worth in this case.

I'm really rusty on unit tests (last did them in Java 1.2 or thereabouts) so I've not attempted to implement these. This is the first time I've done this, so please review carefully before accepting the PR.

The PR is fine. You can add a simple test that just tries to use this. See https://github.com/python-visualization/folium/blob/master/tests/test_folium.py#L282 for an example (but yours will probably be much simpler than that.)

Please add:

  • requests to the requirements.txt file
  • a changelog entry in the CHANGES.txt file
  • the test 😉

and this should be good to go.

@jreades
Copy link
Contributor Author

jreades commented Mar 7, 2017

Cool, I will try to get the test written in the next few days.

I will modify the startswith to check for a colon since that is both trivial and sensible. I originally wrote this using urlparse (it's why the check didn't already have a :) but ended up thinking it was overkill.

Will modify docs and reqs accordingly.

folium/folium.py Outdated
@@ -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:')):

Choose a reason for hiding this comment

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

E231 missing whitespace after ':'
E999 SyntaxError: invalid syntax

@@ -21,6 +21,8 @@
from six import PY3
import branca.element

import requests

Choose a reason for hiding this comment

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

F401 'requests' imported but unused

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'

Choose a reason for hiding this comment

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

E501 line too long (114 > 100 characters)

@@ -27,6 +27,11 @@

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

# For testing remote requests
remote_url = '/'.join([
'https://raw.githubusercontent.com',

Choose a reason for hiding this comment

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

E101 indentation contains mixed spaces and tabs
W191 indentation contains tabs

# For testing remote requests
remote_url = '/'.join([
'https://raw.githubusercontent.com',
'python-visualization/folium/master',

Choose a reason for hiding this comment

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

W191 indentation contains tabs

remote_url = '/'.join([
'https://raw.githubusercontent.com',
'python-visualization/folium/master',
'examples/data/us-states.json'])

Choose a reason for hiding this comment

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

W191 indentation contains tabs

@jreades
Copy link
Contributor Author

jreades commented Mar 9, 2017

Right, think I've managed to tidy everything up -- that stickler really is a stickler (but I can appreciate that). Have added a simple test that tries to remotely access one of the example JSON files packaged with Folium from GitHub and checks that the map extent matches its expectations.

@jreades
Copy link
Contributor Author

jreades commented Mar 9, 2017

Please do check that edits to CHANGES.txt and such are appropriate and reasonable.

CHANGES.txt Outdated
@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

If you want feel free to add you github handle and the PR number here too.

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 👍

@ocefpaf
Copy link
Member

ocefpaf commented Mar 9, 2017

Right, think I've managed to tidy everything up

Thanks! Looks good to go.

that stickler really is a stickler (but I can appreciate that).

And it saves the devs time while reviewing PRs 😉

Have added a simple test that tries to remotely access one of the example JSON files packaged with Folium from GitHub and checks that the map extent matches its expectations.

That is perfect. Let's wait on Travis-CI and merge this.

@jreades
Copy link
Contributor Author

jreades commented Mar 9, 2017 via email

@ocefpaf ocefpaf merged commit 6753324 into python-visualization:master Mar 9, 2017
@ocefpaf
Copy link
Member

ocefpaf commented Mar 9, 2017

Thanks for the awesome contribution @jreades!

sanga pushed a commit to sanga/folium that referenced this pull request Oct 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants