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 stations(city): designations = { 'Toronto': 'toronto', 'Montréal': 'montreal', 'Ottawa': 'capitale' } designation = designations[city] url = 'https://{}.bixi.com/data/bikeStations.xml'.format(designation) data = tools.query_API(url) stations = tools.load_xml(data) return normalize(stations)
def stations(city): # The city parameter is necessary so that everything works key = tools.read_json('config/keys.json')['lacub'] url = 'http://data.lacub.fr/wfs?key={}' \ '&SERVICE=WFS&VERSION=1.1.0&' \ 'REQUEST=GetFeature' \ '&TYPENAME=CI_VCUB_P&SRSNAME=EPSG:4326' print(url) data = tools.query_API(url) stations = tools.load_xml(data) print(stations) 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 get_route(url): ''' Specific function to perform caching. ''' data = tools.query_API(url, repeat=True) return data
def stations(city): # The city parameter is necessary so that everything works url = 'https://secure.niceridemn.org/data2/bikeStations.xml' data = tools.query_API(url) stations = tools.load_xml(data) return normalize(stations)
def stations(city): # The city parameter is necessary so that everything works url = 'https://tfl.gov.uk/tfl/syndication/feeds/cycle-hire/livecyclehireupdates.xml' data = tools.query_API(url) stations = tools.load_xml(data) return normalize(stations)
def stations(city): # The city parameter is necessary so that everything works url = 'https://www.capitalbikeshare.com/data/stations/bikeStations.xml' data = tools.query_API(url) stations = tools.load_xml(data) return normalize(stations)
def stations(city): # The city parameter is necessary so that everything works url = 'http://minaport.ubweb.jp/stations.php' data = tools.query_API(url) stations = tools.load_xml(data) return normalize(stations)
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)