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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
PID012 committed Oct 29, 2023
2 parents c18a48c + 90cb317 commit 80f48bc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -158,3 +158,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

#Jason Debug Files
**/backend/**/*.json
22 changes: 21 additions & 1 deletion README.md
@@ -1,5 +1,25 @@
# EndlessHunger
HackBasel2023

## Description
There are times in your life, when you are hungry.
Unfortunately, you are not able to cook something, because your fridge is empty.

Time to head to the nearest grocery store...

![Hungry](./hangry.jpeg)


## Technical Details (Management Summary)
- Frontend in plain old HTML and jQuery
- Backend in Python with Flask

## How to run
We have Github actions deploying our code to DigitalOcean.
You can access the project at:

[YOUR TRIP TO THE NEXT COOP](https://basel.do.oliverwisler.ch)

This code was created during HackBasel 2023

## Team Members
- Daniel Dienemann
Expand Down
47 changes: 32 additions & 15 deletions backend/coop_locations.py
Expand Up @@ -40,51 +40,68 @@ 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)

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."""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)



Expand Down
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(0.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)
Binary file added hangry.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 80f48bc

Please sign in to comment.