From dc6882887027269e00b8215d46dcfc660090b9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20V=C3=B6geli?= Date: Sun, 29 Oct 2023 10:27:03 +0100 Subject: [PATCH 01/15] Improved speed by route api --- backend/getRoutes.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/getRoutes.py b/backend/getRoutes.py index cb1744b..6f3193f 100644 --- a/backend/getRoutes.py +++ b/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") @@ -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") @@ -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) \ No newline at end of file From 76a6098680e78a7c2e8f87c7edf16bb0674c1558 Mon Sep 17 00:00:00 2001 From: Yotie2000 <124540833+Yotie2000@users.noreply.github.com> Date: Sun, 29 Oct 2023 10:50:45 +0100 Subject: [PATCH 02/15] only 5 coops --- backend/coop_locations.py | 47 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/backend/coop_locations.py b/backend/coop_locations.py index 2494568..e5b4512 100644 --- a/backend/coop_locations.py +++ b/backend/coop_locations.py @@ -40,32 +40,49 @@ def get_unique_format_ids(lat, lng, numCoops): # get_unique_format_ids(lat, lng, numCoops) -import requests -import json def fetch_swiss_cities(): overpass_url = "http://overpass-api.de/api/interpreter" - # This query fetches all populated places (cities, towns, villages, etc.) in Switzerland. + # This query fetches all populated places (cities, towns, villages, etc.) in Switzerland + # and their associated canton. overpass_query = """ [out:json]; area["ISO3166-1"="CH"][admin_level=2]; - (node["place"~"city|town|village"](area); + ( + node["place"~"city|town|village"](area); way["place"~"city|town|village"](area); relation["place"~"city|town|village"](area); ); out center; + foreach( + is_in->.a; + area.a["admin_level"="4"]; + out tags; + ); """ response = requests.get(overpass_url, params={'data': overpass_query}) data = response.json() cities = [] - for element in data['elements']: - if 'tags' in element: + idx = 0 + + while idx < len(data['elements']): + element = data['elements'][idx] + + if element['type'] in ['node', 'way', 'relation'] and 'tags' in element: name = element['tags'].get('name', 'Unknown') - population = int(element['tags'].get('population', 0)) # Convert population to integer - cities.append((name, population)) + population = int(element['tags'].get('population', 0)) + + # Increment index to capture the canton information + idx += 1 + canton_element = data['elements'][idx] + canton = canton_element['tags'].get('ref', 'Unknown') + + cities.append((f"{name}, {canton}", population)) + + idx += 1 # Sort cities by population in descending order cities.sort(key=lambda x: x[1], reverse=True) @@ -73,18 +90,18 @@ def fetch_swiss_cities(): return cities def save_to_file(cities): - with open('swiss_cities_by_population.json', 'w', encoding='utf-8') as file: + with open('swiss_cities_by_population_with_canton.json', 'w', encoding='utf-8') as file: json.dump(cities, file, ensure_ascii=False, indent=4) -# cities = fetch_swiss_cities() -# save_to_file(cities) -# print(f"Data saved to swiss_cities_by_population.json.") +cities = fetch_swiss_cities() +save_to_file(cities) +print(f"Data saved to swiss_cities_by_population_with_canton.json.") -def getCoopLocations(locationName, search_radius=10, time_filter=True): +def getCoopLocations(locationName, search_radius=100, time_filter=False): def getOriginCoordinates(locationName): """Get the latitude and longitude of the given location name.""" @@ -169,7 +186,7 @@ def filter_coops(coops, food_offering_formats, time_filter=True): if formatId in food_offering_formats and distance <= search_radius * 1000 and (not time_filter or openStatus): qualified_coops.append(create_json(coop, openingTime, closingTime, openStatus)) - if len(qualified_coops) >= 10 or distance > search_radius * 1000: + if len(qualified_coops) >= 5 or distance > search_radius * 1000: break return qualified_coops @@ -233,7 +250,7 @@ def create_json(coop, openingTime, closingTime, openStatus): qualified_coops ) -getCoopLocations("Basel", 10, time_filter=False) +# getCoopLocations("Basel", 10, time_filter=False) From 90cb317039e6fa5fe22408416b70d82cf579f4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20V=C3=B6geli?= Date: Sun, 29 Oct 2023 10:53:39 +0100 Subject: [PATCH 03/15] change max thread to 5 --- backend/getRoutes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/getRoutes.py b/backend/getRoutes.py index 6f3193f..0a82f1b 100644 --- a/backend/getRoutes.py +++ b/backend/getRoutes.py @@ -45,7 +45,7 @@ def getAllRoutes(routingProfile, coopLocations,originCoordinates): 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) + time.sleep(0.1) result = future.result() if result: routes.append(result) From c18a48c71d34a40bc6623f9c37c92cfd376da878 Mon Sep 17 00:00:00 2001 From: PID012 <85282994+PID012@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:01:08 +0100 Subject: [PATCH 04/15] added Markers clear --- html/index.html | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/html/index.html b/html/index.html index 578711d..b3ae3eb 100755 --- a/html/index.html +++ b/html/index.html @@ -83,32 +83,32 @@ - - - - - + +