I am trying to calculate the driving distance between a location and a train station. I have four lists of data containing the location latitude, the location longitude, the station latitude and station longitude.
I'd like to take a value from each of these and pass it through an API that calculates driving distance.
I have made the following function, which gives the distance in miles:
def get_driving_distance(lat1,lon1,lat2,lon2):
r = requests.get(f"http://router.project-osrm.org/route/v1/car/{lon1},{lat1};{lon2},{lat2}?overview=false""")
routes = json.loads(r.content)
route_1 = routes.get("routes")[0]
driving_distance = route_1['distance']
return driving_distance/1609.34
Ultimately I'd like a dataframe which shows these four lists (one per column), with a fifth column showing the driving distance.
How do I do this? Everything I've tried keeps failing.
It would be useful to put a timer delay in the request to the API, so it only makes 500 calls before pausing for 5 seconds
(Sorry appreciate this is probably super basic, but I'm new to Python and self-taught)
You can use a simple contextmanager to check a counter and sleep if the counter is too high.
If you make a lot of requests. It is generally a good idea to check the http status code before parsing the response. One easy way provided by the requests library is
Response.raise_for_status
. This will raise an exception if an error occurred. Another way is checking theResponse.status_code
directly, in that case you probably want to use thehttp.HTTPStatus
provided in the standard library. to help your code solve the network issue correctly.