Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created October 29, 2019 13:08
Show Gist options
  • Select an option

  • Save saswata-dutta/cb9051a4188f088a91c6053255a8b3a8 to your computer and use it in GitHub Desktop.

Select an option

Save saswata-dutta/cb9051a4188f088a91c6053255a8b3a8 to your computer and use it in GitHub Desktop.
read waypoints from file and invoke bing maps route api
#!/usr/bin/env python3
import pandas as pd
import requests
import json
import sys
base_url='https://dev.virtualearth.net'
api='REST/v1/Routes/Truck'
key='???'
url =f'{base_url}/{api}?key={key}'
# opts = {'distanceUnit' : 'km', 'routeAttributes' : 'routePath', 'avoid' : 'ferry', 'tolerances' : [0.0001] }
opts = {'distanceUnit' : 'km', 'avoid' : 'ferry'}
def main(infile):
# latitude,longitude
# 13.130705,77.414315
df = pd.read_csv(infile)
waypoints = df.apply(lambda row: {'latitude': row['latitude'], 'longitude' : row['longitude']}, axis = 1)
payload={'waypoints' : waypoints.to_numpy().tolist(), **opts}
response = requests.post(url, json=payload)
json_response = response.json()
print(f"Distance = {json_response['resourceSets'][0]['resources'][0]['travelDistance']}")
print(f"Dutation = {json_response['resourceSets'][0]['resources'][0]['travelDuration']}")
print(f"Dutation Traffic = {json_response['resourceSets'][0]['resources'][0]['travelDurationTraffic']}")
outfile = infile[:-4] + "_response.json"
with open(outfile, 'w') as fp:
json.dump(json_response, fp)
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment