-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
200 lines (154 loc) · 5.96 KB
/
app.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from flask import Flask, request, send_file
import json
from flask_cors import CORS
import gpxpy
import gpxpy.gpx
import os
from geopy.distance import geodesic
from werkzeug.utils import secure_filename
import server_support_get_trails
import gpx_analysis
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET'])
def get():
return "It works, buddy :)"
@app.route('/get-trails', methods=['POST'])
def get_trails():
distance_minimum = int(request.json['distance_minimum'])
distance_maximum = int(request.json['distance_maximum'])
elevation_gain_minimum = int(request.json['elevation_gain_minimum'])
elevation_gain_maximum = int(request.json['elevation_gain_maximum'])
elevation_loss_minimum = int(request.json['elevation_loss_minimum'])
elevation_loss_maximum = int(request.json['elevation_loss_maximum'])
print(
distance_minimum,
distance_maximum,
elevation_gain_minimum,
elevation_gain_maximum,
elevation_loss_minimum,
elevation_loss_maximum
)
results = server_support_get_trails.get_trails(
distance_minimum*1000, #converting to meters
distance_maximum*1000,
elevation_gain_minimum,
elevation_gain_maximum,
elevation_loss_minimum,
elevation_loss_maximum
)
# with open("trail_search_results.json") as fp:
# trail_search_results = json.load(fp)
return json.dumps(results['data'])
@app.route('/get-gpx', methods=['POST'])
def get_gpx():
coordinates = request.json['coordinates']
gpx = gpxpy.gpx.GPX()
# # Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# # Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
# # Create points:
for point in coordinates:
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(point[1], point[0], elevation=0))
# Generate GPX file content
gpx_data = gpx.to_xml()
# Write to a temporary file
file_name = 'route_gpx_file.gpx'
with open(file_name, 'w') as f:
f.write(gpx_data)
# Return the file as a response
response = send_file(file_name, as_attachment=True)
# Optionally, delete the file after sending it
os.remove(file_name)
return response
@app.route('/get-elevation-profile', methods=['POST'])
def get_elevation_profile():
file = None
race = None
# Check if the post request has the file part
if 'file' in request.files:
file = request.files['file']
else:
race = request.json['race']
if file:
# Parse GPX file
gpx = gpxpy.parse(file)
# new_file_path = os.path.join('./uploads/', new_filename)
with open('./user_files/{}'.format(file.filename), 'w') as new_gpx_file:
new_gpx_file.write(gpx.to_xml())
parsed_gpx = []
total_distance = 0
for track in gpx.tracks:
for segment in track.segments:
for i in range(1, len(segment.points)):
point1 = segment.points[i - 1]
point2 = segment.points[i]
distance = geodesic((point1.latitude, point1.longitude), (point2.latitude, point2.longitude)).meters
total_distance += distance
parsed_gpx.append({"distance": total_distance, "elevation": point2.elevation})
# Return the parsed GPX data
return {"data": parsed_gpx}
if race:
gpx_file = open('./official_files/{}.gpx'.format(race), 'r')
gpx = gpxpy.parse(gpx_file)
parsed_gpx = []
total_distance = 0
for track in gpx.tracks:
for segment in track.segments:
for i in range(1, len(segment.points)):
point1 = segment.points[i - 1]
point2 = segment.points[i]
distance = geodesic((point1.latitude, point1.longitude), (point2.latitude, point2.longitude)).meters
total_distance += distance
parsed_gpx.append({"distance": total_distance, "elevation": point2.elevation})
# Return the parsed GPX data
return {"data": parsed_gpx}
return {"error": "File upload failed"}, 500
@app.route('/analyse-gpx', methods=['POST'])
def analyse_gpx():
type_val = request.json['type']
if(type_val == 'race'):
filename = request.json['race']
segments = gpx_analysis.analyse('./official_files/{}.gpx'.format(filename))
if(type_val == 'file'):
filename = request.json['filename']
segments = gpx_analysis.analyse('./user_files/{}'.format(filename))
results = []
count = 0
for each in segments:
if(each['type'] == 'climb'):
elevation_gain_minimum = each['elevation_change'] - 50
elevation_gain_maximum = each['elevation_change'] + 50
elevation_loss_minimum = -99999
elevation_loss_maximum = 999999
if(each['type'] == 'descent'):
elevation_loss_minimum = each['elevation_change'] - 50
elevation_loss_maximum = each['elevation_change'] + 50
elevation_gain_minimum = -99999
elevation_gain_maximum = 999999
if(each['type'] == 'flat'):
elevation_gain_minimum = -99999
elevation_gain_maximum = 99999
elevation_loss_minimum = -99999
elevation_loss_maximum = 999999
print("**************")
print(each)
trails = server_support_get_trails.get_trails(
each['distance']-100, #adding a little range buffer
each['distance']+100,
elevation_gain_minimum,
elevation_gain_maximum,
elevation_loss_minimum,
elevation_loss_maximum
)['data']
each['trails'] = trails
count +=1
each['id'] = count
results.append(each)
print("**************")
return json.dumps(results)
if __name__ == '__main__':
app.run(debug=True)