Skip to content
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

Explicit coordinate validation #1090

Merged
merged 2 commits into from
Mar 19, 2019

Conversation

Conengmo
Copy link
Member

@Conengmo Conengmo commented Mar 10, 2019

Problem
We have multiple conversion/validation functions in utilities. It's hard to decide which to use, and they are not thorough.

Solution
Have a few validation/conversion functions that are clear and explicit. They raise meaningful errors. They have extensive tests.

So that's what I tried in this PR. I put some things together into two functions: one for single coordinate pairs and one for lists (of lists) of coordinate pairs. Both try to raise meaningful exceptions and convert to a single common format.

I applied the validation functions throughout the code base when possible. In some cases (HeatMap, FastMarkerCluster) I made custom ones because they have special data formats.

For the Marker class and vector classes I added a new BaseMarker class to make the distinction between single and multiple locations explicit. Before the vector classes just passed a list of coordinate pairs to Marker, while Marker is documented to only accept a single coordinate pair.

Possible improvements:

  • Use tuple data type for coordinate pairs. That's something for another PR I think, if we want that at all.

@ocefpaf @jtbaker want to review this one?

Closes #1089

folium/map.py Outdated Show resolved Hide resolved
folium/utilities.py Outdated Show resolved Hide resolved
folium/map.py Outdated Show resolved Hide resolved
from six.moves.urllib.parse import urlparse, uses_netloc, uses_params, uses_relative


_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
_VALID_URLS.discard('')


def _is_sized_iterable(arg):
"""Validates the arg is Sized & Iterable"""
return isinstance(arg, abc.Sized) & isinstance(arg, abc.Iterable)
Copy link
Member

Choose a reason for hiding this comment

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

Don't you think this was simpler with the abc? I'm fine with this approach but I could not see why the change at a first pass.

Copy link
Member Author

@Conengmo Conengmo Mar 10, 2019

Choose a reason for hiding this comment

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

Hmm I tried to change it into abc.Sequence, because it should also have __getitem__, but then it won't work for Numpy arrays. So my problem is that there may be other classes that don't inherit from abc types but have those methods implemented? I guess I prefer the try/except version because it's more intuitive and handles duck types better than abc.

folium/map.py Show resolved Hide resolved
folium/plugins/boat_marker.py Outdated Show resolved Hide resolved
folium/vector_layers.py Show resolved Hide resolved
folium/vector_layers.py Show resolved Hide resolved
folium/vector_layers.py Show resolved Hide resolved
folium/vector_layers.py Show resolved Hide resolved
Rewrite the validation functions in utilities that check single and multiple coordinate pairs. Make them more strict and raise meaningful exceptions.

Apply the validation functions throughout the code base wherever possible.

For the Marker class and vector classes a new BaseMarker class is added to make the distinction between single and multiple locations explicit.

For the HeatMap and FastMarkerCluster classes I made custom validation in the classes themselves. Their use cases are different, so they cannot use the standard validation functions.
It contained an outdated use of the `MarkerCluster` class.
@Conengmo
Copy link
Member Author

Hi @ocefpaf, I think this is pretty much ready. Could you check it out if you have a moment?

@ocefpaf ocefpaf merged commit 6e87811 into python-visualization:master Mar 19, 2019
@ocefpaf
Copy link
Member

ocefpaf commented Mar 19, 2019

Awesome changes @Conengmo! Thanks for fixing this!!

@Conengmo
Copy link
Member Author

Thanks for the review!

@Conengmo Conengmo removed the waiting for review PR is waiting to be reviewed label Mar 20, 2019
@Conengmo Conengmo deleted the explicit-coordinates branch March 20, 2019 06:47
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.

3 participants