-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
223 lines (204 loc) · 8.44 KB
/
models.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#All the models/ difficult algorithms
from findFolder import *
from changeDB import add_gpxFile
from select import *
from math import sin, cos, sqrt, atan, radians, atan2
import math
import gpxpy
import copy
def retrieveFiles(data):
(folders, files) = findGPX(data.path)
data.totalPages = (len(files) + len(folders)) // data.pageLength
start = data.currentPage * data.pageLength
if start > len(files) - 1:
fileStart = start - len(files)
return folders[fileStart:fileStart+data.pageLength]
else:
if len(files) - start >= data.pageLength:
return files[start:start+data.pageLength]
else:
end = len(files) - start
return files[start:start+end] + folders[:(start+data.pageLength-end)]
def retrieveUsers(data):
data.totalPages = len(data.users) // data.pageLength
start = data.currentPage * data.pageLength
return data.users[start:start+data.pageLength]
def retrieveRides(data):
data.totalRides = select_gpx(data.conn, data.id)
data.totalPages = len(data.totalRides) // data.pageLength
start = data.currentPage * data.pageLength
return data.totalRides[start:start+data.pageLength]
def addGPX(data, selected):
name = data.files[selected]
if data.path == ".":
gpxFile = data.files[selected]
else:
gpxFile = data.path + data.files[selected]
gpx = open(gpxFile, 'r')
add_gpxFile(data.conn, name, gpx, data.id)
def retrieveGPXMiles(data):
files = select_gpx(data.conn, data.id)
data.totalRides = files
data.totalMiles = 0
for gpx in files:
data.totalMiles += gpx[3]
data.totalMiles = int(data.totalMiles)
def reccommendTrail(data):
pass
#Find distance from lattitude longitude https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude
def findDistance(lat1, lon1, lat2, lon2):
R = 6373.0
lat1 = radians(lat1)
lon1 = radians(lon1)
lat2 = radians(lat2)
lon2 = radians(lon2)
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return distance
def findSections(full):
(_,_,gpxFile,_, min_lat, min_lon, max_lat, max_lon,_) = full
gpx = gpxpy.parse(gpxFile)
i=0
plotNext = False
last = []
section = []
sections = []
inBetween = {}
betweenTracker = []
looping = False
loopDist = 0
tolerance = 0.0008
initPoint = None
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
posX = point.longitude
posY = point.latitude
#posX, posY = float("%.3f" % (2 * posX)), float("%.3f" % (2 * posY))
if i != 0:
plotNext = True
#checks through all current points to see if a new one can be added or not
for (lastX, lastY, elev) in last:
if abs(posX - lastX) < tolerance and abs(posY - lastY) < tolerance:
plotNext = False
curPoint = (lastX, lastY, elev)
break
if i == 0:
last.append((posX, posY, point.elevation))
section.append((posX, posY, point.elevation))
initPoint = (posX, posY, point.elevation)
elif plotNext == True:
last.append((posX, posY, point.elevation))
if looping == True:
loop = findFloats(section, section[-1])[0]
assert(section[loop] == section[-1])
loopDist = len(section) - loop
#in order not to have short loops
if len(section[-loopDist:-1]) > 10:
sections.append(section[-loopDist:])
assert(section[-loopDist:][0] == section[-loopDist:][-1])
section = section[:-loopDist+1]
looping = False
inBetween[(initPoint, last[-1])] = copy.copy(betweenTracker)
betweenTracker = []
initPoint = (posX, posY, point.elevation)
section.append((posX, posY, point.elevation))
plotNext = False
else:
(segEndX, segEndY, _) = section[-1]
if abs(posX - segEndX) < tolerance and abs(posY - segEndY) < tolerance:
betweenTracker.append((posX, posY, point.elevation))
else:
looping = True
section.append(curPoint)
inBetween[(initPoint, curPoint)] = copy.copy(betweenTracker)
betweenTracker = []
initPoint = curPoint
i += 1
sections.append(section)
first = set()
for i in range(len(sections)):
length = len(sections[i])
j = 0
offset = 0
while j < length - 1:
dist = len(inBetween[(sections[i][j+offset],sections[i][j+1+offset])])
if sections[i][j+offset] not in first and sections[i][j+1+offset] not in first:
sections[i] = sections[i][:offset+j+1] + inBetween[(sections[i][j+offset],sections[i][j+1+offset])] + sections[i][j+1+offset:]
offset += dist
first.add(sections[i][j+1+offset])
first.add(sections[i][j+offset])
else:
sections[i] = sections[i][:offset+j] + inBetween[(sections[i][j+offset],sections[i][j+1+offset])] + sections[i][j+1+offset:]
offset += dist - 1
j += 1
first = set()
return sections
#https://stackoverflow.com/questions/24935938/how-to-find-a-float-number-in-a-list , modified to use tuples
def findFloats(listOfFloats, value):
return [i for i, tup in enumerate(listOfFloats)
if abs(tup[0]-value[0]) < 0.00001 and abs(tup[1]-value[1]) < 0.00001]
def analyzeSections(sections):
i = 0
size = []
while len(size) != len(sections):
for section in sections:
difficulty = assignDifficulty(section)
if i == 0:
size.append((difficulty, i))
else:
for j in range(len(size)):
if size[j][0] < difficulty:
size.insert(j, (difficulty, i))
break
elif j+1 == len(size):
size.insert(j+1, (difficulty, i))
i += 1
return size
def assignDifficulty(section):
i = 0
avgSteep = 0
for point in section:
lon, lat, elev = point
if i == 0:
lastElev = elev
lastLat = lat
lastLon = lon
else:
elevationChange = elev - lastElev
distance = findDistance(lastLat, lastLon, lat, lon)*1000
ratio = abs(atan(elevationChange/distance))
lastLat = lat
lastLon = lon
lastElev = elev
avgSteep += ratio
i += 1
avgSteep /= i
return avgSteep
#Requests from https://developers.google.com/maps/documentation/maps-static/dev-guide
"""def retrieveMap(minLat, maxLat, minLon, maxLon, data, path):
#"&path=weight:3|color:orange"\
apiKey = getApi()
base = "https://maps.googleapis.com/maps/api/staticmap?"
size = str(data.width//2) +"x" + str(data.height//2)
centerX = minLon + (maxLon-minLon)/2
centerY = minLat + (maxLat-minLat)/2
weight = 5
#meters_per_pixel = 156543.03392 * math.cos(centerY * math.pi / 180) / 2**zoom
request = base +"center="+ str(centerY)+"," + str(centerX) + "&size="+ size + path + "&zoom=11&format=gif&scale=2&key=" + apiKey
print(request)
return request"""
def encodePath(gpxFile):
gpx = gpxpy.parse(gpxFile)
path = "&path=color:0x0000ff|weight:5|"
i = 0
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
if i%10 == 0:
path += str(point.latitude) + "," + str(point.longitude) + "|"
i+=1
return path[:-1]