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 435000a + cb4b52e commit ef23199
Show file tree
Hide file tree
Showing 5 changed files with 14,948 additions and 37 deletions.
37 changes: 29 additions & 8 deletions backend/app.py
@@ -1,5 +1,5 @@
#!/bin/python3
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from coop_locations import getCoopLocations
from getRoutes import getAllRoutes
from flask_cors import CORS
Expand All @@ -11,13 +11,34 @@
@app.route('/locations')

def locations():
coopData = getCoopLocations("Basel", 10)
return jsonify(coopData)
coopData = getCoopLocations("Basel", 10)
return jsonify(coopData)


# @app.route('/route/<variable1>/<variable2>')
# def route(variable1, variable2):
# # variable1 und variable2 sind die empfangenen URL-Parameter
# return f'Variable 1: {variable1}, Variable 2: {variable2}'

@app.route('/calculations')

# @app.route('/search/<searchstring>', methods=['GET'])

# def serach(searchstring):
# originCoordinates, coopLocations = getCoopLocations(searchstring, 10, time_filter=False)
# calculationDatas = getAllRoutes("driving-car", coopLocations)
# return jsonify(calculationDatas)

@app.route('/calculations', methods=['GET'])
def calculations():
originCoordinates, coopLocations = getCoopLocations("Basel", 10, time_filter=False)
calculationDatas = getAllRoutes("driving-car", coopLocations)
return jsonify(calculationDatas)
location = request.args.get('location')
filter_value = request.args.get('filter')
originCoordinates, coopLocations = getCoopLocations(location, 10, time_filter=False)
calculationDatas = getAllRoutes("driving-car", coopLocations, originCoordinates)
return jsonify(calculationDatas)

# location = request.args.get('location')
# filter_value = request.args.get('filter')

# return f'Location: {location}, Filter: {filter_value}'

if __name__ == '__main__':
app.run()
44 changes: 42 additions & 2 deletions backend/coop_locations.py
Expand Up @@ -36,10 +36,50 @@ def get_unique_format_ids(lat, lng, numCoops):
# # Usage:
# lat = 47.543427 # Replace with the desired latitude
# lng = 7.598133599999983 # Replace with the desired longitude
# numCoops = 2233 # Replace with the desired number of Coop locations
# numCoops = 2500 # Replace with the desired number of Coop locations

# 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.
overpass_query = """
[out:json];
area["ISO3166-1"="CH"][admin_level=2];
(node["place"~"city|town|village"](area);
way["place"~"city|town|village"](area);
relation["place"~"city|town|village"](area);
);
out center;
"""

response = requests.get(overpass_url, params={'data': overpass_query})
data = response.json()

cities = []
for element in data['elements']:
if 'tags' in element:
name = element['tags'].get('name', 'Unknown')
population = int(element['tags'].get('population', 0)) # Convert population to integer
cities.append((name, population))

# 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:
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.")



Expand All @@ -49,7 +89,7 @@ def getCoopLocations(locationName, search_radius=10, time_filter=True):
def getOriginCoordinates(locationName):
"""Get the latitude and longitude of the given location name."""
geolocator = Nominatim(user_agent="CoopFinder")
location = geolocator.geocode(f'{locationName}, Switzerland')
location = geolocator.geocode(f'{locationName}', country_codes="CH") # Limit to Switzerland with country_codes="CH"
if location is None:
print(f'No location found for {locationName}, Switzerland')
return None, None
Expand Down
40 changes: 20 additions & 20 deletions backend/filtered_coopLocations.json
Expand Up @@ -11,8 +11,8 @@
"StreetName": "Gerbergasse",
"HouseNumber": "4",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "17:45"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop City Basel Am Marktplatz",
Expand All @@ -26,8 +26,8 @@
"StreetName": "Gerbergasse",
"HouseNumber": "4",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Supermarkt City Basel Marktplatz Food",
Expand All @@ -41,8 +41,8 @@
"StreetName": "Hutgasse",
"HouseNumber": "19",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Supermarkt Basel Fischmärt",
Expand All @@ -56,8 +56,8 @@
"StreetName": "Marktgasse",
"HouseNumber": "8",
"Open": false,
"OpeningTime": "06:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Supermarkt City Basel Pfauen Food",
Expand All @@ -71,8 +71,8 @@
"StreetName": "Freie Strasse",
"HouseNumber": "75",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop City Basel Pfauen",
Expand All @@ -86,8 +86,8 @@
"StreetName": "Freie Strasse",
"HouseNumber": "75",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Restaurant Basel Pfauen",
Expand All @@ -101,8 +101,8 @@
"StreetName": "Freie Strasse",
"HouseNumber": "75",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Take it Basel City Pfauen",
Expand All @@ -116,8 +116,8 @@
"StreetName": "Freie Strasse",
"HouseNumber": "75",
"Open": false,
"OpeningTime": "08:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
},
{
"Name": "Coop Pronto Shop Basel Barfüsserplatz",
Expand All @@ -131,8 +131,8 @@
"StreetName": "Streitgasse",
"HouseNumber": "20",
"Open": false,
"OpeningTime": "06:30",
"ClosingTime": "22:00"
"OpeningTime": "09:00",
"ClosingTime": "20:00"
},
{
"Name": "Coop Supermarkt Basel Greifengasse",
Expand All @@ -146,7 +146,7 @@
"StreetName": "Greifengasse",
"HouseNumber": "3",
"Open": false,
"OpeningTime": "07:30",
"ClosingTime": "18:00"
"OpeningTime": null,
"ClosingTime": null
}
]
14 changes: 7 additions & 7 deletions backend/getRoutes.py
Expand Up @@ -3,12 +3,11 @@
import xml.etree.ElementTree as ET# Constructing the XML elements and attributes
from coop_locations import getCoopLocations

originCoordinates, coopLocations = getCoopLocations("Basel", 10, time_filter=False)

def getRoute(coopLocations, routingProfile):
def getRoute(coopLocations, routingProfile, originCoordinates):
longitude = coopLocations.get("Longitude")
latitude = coopLocations.get("Latitude")
body = {"coordinates": [[8.681495, 49.41461], [longitude, 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',
Expand All @@ -26,21 +25,21 @@ def getRoute(coopLocations, routingProfile):
route = json.loads(call.text) # Parse the JSON response
print("Route calculated")

coop = {**route, **coopLocations}
coop = {**body, **coopLocations, **route}

return coop
else:
print("Request was not successful. Status code:", call.status_code)

def getAllRoutes(routingProfile, coopLocations):
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), coopLocations))
routes = list(map(lambda x: getRoute(x, routingProfile,originCoordinates), coopLocations))

with open('routes.json', 'w') as json_file:
json.dump(routes, json_file, indent=4) # Save the JSON to a file with indentation
Expand All @@ -58,4 +57,5 @@ def getAllRoutes(routingProfile, coopLocations):
print("Wrong Routing Profile", routingProfile)

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

0 comments on commit ef23199

Please sign in to comment.