def generate_route(trip): ''' Build a path using the Mapzen's Valhalla API. ''' mode = trip['mode'] points = trip['points'] base = 'http://valhalla.mapzen.com/' key = tools.read_json('config/keys.json')['valhalla'] request = json.dumps({ 'locations': points, # 'costing': mode, # For some reasons the biking costing is not close to reality 'costing': 'pedestrian', 'directions_options': { 'units': 'kilometers' } }) url = '{0}route?json={1}&api_key={2}'.format(base, request, key) # No whitespace allowed url = url.replace(' ', '') data = get_route(url) path = tools.load_json(data) return { 'mode': mode, 'polyline': path['trip']['legs'][0]['shape'], 'maneuvers': path['trip']['legs'][0]['maneuvers'], 'distance': path['trip']['legs'][0]['summary']['length'] }
def add_altitudes(stations, size=50): ''' Use the Google Maps Elevation API. ''' base = 'https://maps.googleapis.com/maps/api/elevation/json?' key = tools.read_json('config/keys.json')['google-elevation'] locationsList = '' packages = [] counter = 1 for station in stations: locationsList += '{lat},{lon}|'.format(lat=station['lat'], lon=station['lon']) # The API only allows a certain amount of locations per request counter += 1 if counter > size: locationsList += ';' counter = 1 for locations in locationsList.split(';'): locations = locations[:-1] url = base + 'locations={0}&key={1}'.format(locations, key) request = tools.query_API(url) data = tools.load_json(request) packages.append(data['results']) # Melt the packages into one list altitudes = [] for package in packages: altitudes.extend(package) # Tidy everything up for database insertion data = [] for station in zip(stations, altitudes): data.append({ 'name': station[0]['name'], 'lat': station[0]['lat'], 'lon': station[0]['lon'], 'alt': station[1]['elevation'] }) return data
def stations(city): base = 'https://api.jcdecaux.com/vls/v1/' key = tools.read_json('config/keys.json')['jcdecaux'] url = '{0}stations/?contract={1}&apiKey={2}'.format(base, city, key) data = tools.query_API(url) stations = tools.load_json(data) return normalize(stations)
def stations(city): # The city parameter is necessary so that everything works base = 'http://data.keolis-rennes.com/json/?version=1.0&' key = tools.read_json('config/keys.json')['keolis'] url = '{0}key={1}&cmd=getstation'.format(base, key) data = tools.query_API(url) stations = tools.load_json(data) return normalize(stations)
def geocode(address): ''' Return the latitude and longitude of an address thanks to the Nominatim API. ''' base = 'http://nominatim.openstreetmap.org/search?' \ 'format=json&polygon_geojson=1&q=' text = tools.remove_special_characters(address) keywords = '+'.join(text.split()) url = ''.join((base, keywords)) data = tools.query_API(url) address = tools.load_json(data)[0] latitude = float(address['lat']) longitude = float(address['lon']) return (latitude, longitude)
def compute_distances(departure, stations, mode): ''' Using the Mapbox Distance API. ''' # Interrogate the API to get the distance to each station base = 'https://api.mapbox.com/distances/v1/mapbox/' key = tools.read_json('config/keys.json')['mapbox-distance'] coordinates = { 'coordinates': [departure] + [station['p'] for station in stations] } print(coordinates) url = '{0}{1}?access_token={2}'.format(base, mode, key) data = tools.query_API(url) distances = tools.load_json(data)['rows'][0]['elements'] candidates = [] for station in zip(stations, distances): candidate = {} for information in station: candidate.update(information) candidates.append(candidate) return candidates
def compute_distances(departure, stations, mode): ''' Using the Mapbox Distance API. ''' # Interrogate the API to get the distance to each station base = 'https://api.mapbox.com/distances/v1/mapbox/' key = tools.read_json('config/keys.json')['mapbox-distance'] coordinates = { 'coordinates': [ departure ] + [station['p'] for station in stations] } print(coordinates) url = '{0}{1}?access_token={2}'.format(base, mode, key) data = tools.query_API(url) distances = tools.load_json(data)['rows'][0]['elements'] candidates = [] for station in zip(stations, distances): candidate = {} for information in station: candidate.update(information) candidates.append(candidate) return candidates
def generate_route(trip): ''' Build a path using the Mapzen's Valhalla API. ''' mode = trip['mode'] points = trip['points'] base = 'http://valhalla.mapzen.com/' key = tools.read_json('config/keys.json')['valhalla'] request = json.dumps({ 'locations': points, # 'costing': mode, # For some reasons the biking costing is not close to reality 'costing': 'pedestrian', 'directions_options': { 'units': 'kilometers' } }) url = '{0}route?json={1}&api_key={2}'.format(base, request, key) # No whitespace allowed url = url.replace(' ', '') data = get_route(url) path = tools.load_json(data) return {'mode': mode, 'polyline': path['trip']['legs'][0]['shape'], 'maneuvers': path['trip']['legs'][0]['maneuvers'], 'distance': path['trip']['legs'][0]['summary']['length']}
def stations(city): # The city parameter is necessary so that everything works url = 'http://www.bikechattanooga.com/stations/json' data = tools.query_API(url) stations = tools.load_json(data) return normalize(stations)