Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Improved speed by route api
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterofSoftware committed Oct 29, 2023
1 parent a6613f6 commit dc68828
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions backend/getRoutes.py
@@ -1,8 +1,9 @@
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")
Expand Down Expand Up @@ -39,8 +40,15 @@ def getAllRoutes(routingProfile, coopLocations,originCoordinates):

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 = 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")
Expand All @@ -57,5 +65,5 @@ def getAllRoutes(routingProfile, coopLocations,originCoordinates):
print("Wrong Routing Profile", routingProfile)

if __name__ == '__main__':
originCoordinates, coopLocations = getCoopLocations("Basel", 10, time_filter=False)
originCoordinates, coopLocations = getCoopLocations("Basel", time_filter=False)
ans = getAllRoutes("driving-car", coopLocations, originCoordinates)

0 comments on commit dc68828

Please sign in to comment.