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
Room for improvement on directions interface #65
Comments
For anyone interested in tackling adding parameters, here is how to get started. To add the ability to set the travel mode, for instance:
class Directions(widgets.Widget):
...
travel_mode = Enum(
values=["walking", "driving"], default_value="driving"
).tag(sync=True)
This allows you to write:
```python
d = Directions(data=[...], travel_mode="walking")
var DirectionsLayerView = GMapsLayerView.extend({
render: function() {
...
var travel_mode = this.get_travel_mode();
var request = {
origin: orig,
destination: dest,
waypoints: wps,
travelMode: travel_mode
};
...
},
...
get_travel_mode: function() {
var model_travel_mode = this.model.get("travel_mode");
var mapping = {
"walking": google.maps.DirectionsTravelMode.WALKING,
"driving": google.maps.DirectionsTravelMode.DRIVING
}
return mapping[model_travel_mode];
}
}); This tells the view to read the Adding things like departure time etc. should be similarly easy. |
Adding parameters addresses half of the problem. The other half -- automatic geocoding to translate from string addresses to latitude / longitude pairs -- is a little different. I think a set of translation functions to inter-operate with the |
Note to myself: a reasonable interface to start working on would be gmaps.add_layer(Directions(data=[client.geocode("Toulouse"), client.geocode("Paris")])) |
I think I disagree with this interface somewhat. I would rather keep the data array as latitude / longitude pairs, since that's the lowest common denominator. We could then provide helper functions that help with the geocoding. The interface could look like: import gmaps
gmaps.configure(API KEY)
journey = ["Toulouse", "Paris"]
data = [gmaps.geocode(location) for location in journey]
# data is then an array like [[43.6, 1.2], [48.0, 2.3]]
d = Directions(data=data, travel_mode="driving") ... where This lets people do their own geocoding if they want. Any thoughts? If there is something to be gained by passing the entire |
Oh actually, the result of >>> client.geocode("Paris")
[{'address_components': [{'long_name': 'Paris',
'short_name': 'Paris',
'types': ['locality', 'political']},
{'long_name': 'Paris',
'short_name': 'Paris',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'Île-de-France',
'short_name': 'Île-de-France',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'France',
'short_name': 'FR',
'types': ['country', 'political']}],
'formatted_address': 'Paris, France',
'geometry': {'bounds': {'northeast': {'lat': 48.9021449, 'lng': 2.4699209},
'southwest': {'lat': 48.815573, 'lng': 2.225193}},
'location': {'lat': 48.856614, 'lng': 2.3522219},
'location_type': 'APPROXIMATE',
'viewport': {'northeast': {'lat': 48.9021449, 'lng': 2.4699209},
'southwest': {'lat': 48.815573, 'lng': 2.225193}}},
'place_id': 'ChIJD7fiBh9u5kcRYJSMaMOCCwQ',
'types': ['locality', 'political']},
{'address_components': [{'long_name': 'Paris',
'short_name': 'Paris',
'types': ['locality', 'political']},
{'long_name': 'Lamar County',
'short_name': 'Lamar County',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'Texas',
'short_name': 'TX',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'United States',
'short_name': 'US',
'types': ['country', 'political']}],
'formatted_address': 'Paris, TX, USA',
'geometry': {'bounds': {'northeast': {'lat': 33.7383781, 'lng': -95.435455},
'southwest': {'lat': 33.6118529, 'lng': -95.627928}},
'location': {'lat': 33.6609389, 'lng': -95.55551299999999},
'location_type': 'APPROXIMATE',
'viewport': {'northeast': {'lat': 33.7383781, 'lng': -95.435455},
'southwest': {'lat': 33.6118529, 'lng': -95.627928}}},
'place_id': 'ChIJmysnFgZYSoYRSfPTL2YJuck',
'types': ['locality', 'political']},
# truncated
] |
The results of def geocode(location):
"""
Return a (latitude, longitude) pair for a location.
>>> geocode("paris")
(48.856614, 2.4699209)
"""
googlemaps_geocode = client.geocode(location) # client is a googlemaps.Client instance
googlemaps_location = googlemaps_geocode[0]["geometry"]["location"]
latitude = googlemaps_location["latitude"]
longitude = googlemaps_location["longitude"]
return (latitude, longitude) (I haven't tested this method) The results returned by our |
Looks fair. 😉 |
I have mixed feelings on the lat-lon pairs, as structured data is probably going to give it to you in two arrays.
Will allow for more seamless use with numpy.array as well. |
I don't have any strong opinions on that matter but as a matter of fact, it is common for me to handle a list of structured data (dict or object with accessors to lat/lon) indexed by time. Well, in the end it is always not free to project the data into the proper interface... |
Yeah, but the objective is to remain as close to the most common presentation of the data as possible so that the amount of conversions is reduced. I guess I'm making the case that databases, csv files, and other structured data is going to (generally) give you back everything in vectorized arrays. Maybe you can make the case that the geocoders always think in the other representation (dicts, tuples, ect), but I don't have the experience to comment on that. |
This is a great discussion! The original motivation for gmaps was to help explore geographical data. For this use case, most users will have data in arrays that are built from databases or CSV files, as @reyale said. However, I'm not convinced this makes an interface like import pandas as pd
df = pd.read_csv(...)
Heatmap(data=df[["latitude", "longitude"]]) ... is just as obvious. Clearly latitude and longitude belong together and passing in one without the other doesn't make much sense. Note that this doesn't work at the moment, but fixing it is easy and doesn't change the interface (see issue #66). I can see the case for having split |
I think we're missing the plot a bit here. the So if you like the data= I suggest the interface/logic to be something like:
This allows behavior like:
to just work. Sorry for all the armchair commentary. I'll roll up my sleeves and contribute soon! |
Contributions are definitely welcome! Since the shape of the data array is only loosely related to the directions interface, I've opened issue #68 and we should move the discussion there. |
(from #60)
Some room for improvements, here are some reflections:
data
). More generally, it would be great to have a close interface togooglemaps
and write:For future references, there is a good overview of the interface here.
The text was updated successfully, but these errors were encountered: