I have the following dataset (this is just 5 observations out of 270K):
ride_id_hash start_lat start_lon end_lat end_lon
0 3913897986482697189 52.514532 13.389043 52.513640 13.380389
1 -742299897365889600 52.515073 13.394545 52.518483 13.407965
2 -319522908151650562 52.528291 13.409570 52.526965 13.415183
3 -3247332391891858034 52.514276 13.453460 52.503649 13.448538
4 5368542961279891088 52.504448 13.468405 52.511891 13.474998
I would like to make requests to an API and retrieve the estimated distance and time based on longitude-latitude origin-destination from Mapbox. As I have to make it with 270 rows, the process is veeeery slow. I coded the following:
acces_token = 'pk.eyXXXX'
def create_route_json(row):
"""Get route JSON."""
base_url = 'https://api.mapbox.com/directions/v5/mapbox/cycling/'
url = base_url + str(row['Start Lon']) + \
',' + str(row['Start Lat']) + \
';' + str(row['End Lon']) + \
',' + str(row['End Lat'])
print('start lon: ' + str(row['Start Lon']))
print('start lat: ' + str(row['Start Lat']))
print('end lon: ' + str(row['End Lon']))
print('end lat: ' + str(row['End Lat']))
params = {
'geometries': 'geojson',
'access_token': acces_token
}
req = requests.get(url, params=params)
route_json = req.json()['routes'][0]
print('Duration: ' + str(route_json['duration']))
print('Distance: ' + str(route_json['distance']))
print('^'*40)
row['Estimated_duration'] = route_json['duration']
row['Estimated_distance'] = route_json['distance']
#mystr= str(route_json['duration']) + ';'+ str(route_json['distance'])
return row
start = time.time()
df_process.apply(create_route_json, axis=1)
I am wondering if there is a way to make 270K calls in a fast way. (you need to have your own access token from Mapbox Directions API