-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefer.py
More file actions
56 lines (46 loc) · 2.1 KB
/
Copy pathRefer.py
File metadata and controls
56 lines (46 loc) · 2.1 KB
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
from flask import Flask, request, jsonify
import pandas as pd
import pgeocode
# Load data
df = pd.read_csv('WaitingTimes_2.csv', sep=',', encoding='utf-8')
df['Postcode_kort'] = df['Postcode_kort'].astype(str).str[:4] # Make sure it's string
app = Flask(__name__)
geo = pgeocode.GeoDistance('NL')
@app.route('/best_match', methods=['GET'])
def best_match():
# Get postcode from query parameter
postcode = request.args.get('postcode')
if not postcode:
return jsonify({'error': 'No postcode provided'}), 400
try:
postcode_short = int(str(postcode)[:4])
except ValueError:
return jsonify({'error': 'Invalid postcode'}), 400
# Compute distances to all locations
distances = df['Postcode_kort'].apply(lambda x: geo.query_postal_code(str(postcode_short), str(x)))
distanceval = ((distances - distances.min()) / (distances.max() - distances.min()))
waittime = df['WaitingTime'] * 1.5
waittime = (waittime - waittime.min()) / (waittime.max() - waittime.min())
score1 = -((distanceval + 1)**2)
score2 = (df['IndicatorWaarde']/10)**2
score3 = -((waittime + 1)**2)
total_score = score1 + score2 + score3
total_score = (total_score - total_score.min()) / (total_score.max() - total_score.min())
top5_indices = total_score.nlargest(5).index
matches = []
for rank, i in enumerate(top5_indices, start=1):
match = {
'% match': float(total_score[i]*100),
'Rank': int(rank),
'OrganisatieNaam': str(df.iloc[i]['LocatieNaam']),
'Postcode': str(df.iloc[i]['LocatiePostcode']),
'Plaats': str(df.iloc[i]['LocatiePlaats']),
'Kwaliteit': float(df.iloc[i]['IndicatorWaarde']),
'WaitingTime [Days]': float(df.iloc[i]['WaitingTime']),
'Distance [KM]': float(round(distances[i], 2)),
'GoogleMapsLink': f"https://www.google.com/maps/dir/?api=1&origin={postcode}&destination={df.iloc[i]['LocatiePostcode']}"
}
matches.append(match)
return jsonify(matches), 200
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=8080)