This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getRoutes.py
69 lines (51 loc) · 2.77 KB
/
getRoutes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import json
import time
import xml.etree.ElementTree as ET# Constructing the XML elements and attributes
from coop_locations import getCoopLocations
from concurrent.futures import ThreadPoolExecutor
def getRoute(coopLocations, routingProfile, originCoordinates):
longitude = coopLocations.get("Longitude")
latitude = coopLocations.get("Latitude")
body = {"coordinates": [[originCoordinates.get("Longitude"),originCoordinates.get("Latitude")], [longitude, latitude]]}
headers = {
'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
'Authorization': '5b3ce3597851110001cf6248bf3d9d3adcf5443db7ee91adc2489f2d',
'Content-Type': 'application/json; charset=utf-8'
}
# Use the routing profile variable in the URL
url = f'http://api.openrouteservice.org/v2/directions/{routingProfile}/geojson'
call = requests.post(url, json=body, headers=headers)
if call.status_code == 200:
route = json.loads(call.text) # Parse the JSON response
print("Route calculated")
coop = {**body, **coopLocations, **route}
return coop
else:
print("Request was not successful. Status code:", call.status_code)
def getAllRoutes(routingProfile, coopLocations,originCoordinates):
if routingProfile == "publicTransport":
pass
elif routingProfile == "driving-car" or routingProfile == "cycling-regular" or routingProfile == "foot-walking" or routingProfile == "wheelchair":
# routes = list(map(lambda x: getRoute(x, routingProfile,originCoordinates), coopLocations))
routes = []
with ThreadPoolExecutor(max_workers=5) as executor: # Du kannst die Anzahl der parallelen Threads anpassen
futures = [executor.submit(getRoute, x, routingProfile, originCoordinates) for x in coopLocations]
for future in futures:
time.sleep(1)
result = future.result()
if result:
routes.append(result)
with open('routes.json', 'w') as json_file:
json.dump(routes, json_file, indent=4) # Save the JSON to a file with indentation
print ("Routes saved")
sortedRoutes = sorted(routes, key=lambda x: x['features'][0]['properties']['summary']['distance'])
with open('sortedRoutes.json', 'w') as json_file:
json.dump(sortedRoutes, json_file, indent=4) # Save the JSON to a file with indentation
print ("sortedRoutes saved")
return sortedRoutes
else:
print("Wrong Routing Profile", routingProfile)
if __name__ == '__main__':
originCoordinates, coopLocations = getCoopLocations("Basel", time_filter=False)
ans = getAllRoutes("driving-car", coopLocations, originCoordinates)