-
Notifications
You must be signed in to change notification settings - Fork 0
/
genRoute.py
286 lines (251 loc) · 8.37 KB
/
genRoute.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
############################################################
# Program Name: genRoute.py
# Version: 2.8
# Author: L0rd DarkF0ce
# GitHub: https://github.com/l0rddarkf0rce/genRoute
############################################################
# 20210206
# Original program created
#
# 20210209
# Added code to create different files for each of the
# research tasks.
#
# 20210210
# Added code to calculate and print distance and cool
# down times.
#
# 20210213
# Added code to combine the quests into one single file
# and format the file to paste into Reddit.
#
# 20210225
# Added CD distances > 30 Km. Too keep it simple I am
# using ranges for ant distance above 30 Km.
#
# 20210310
# Added an option to keep the GPX files if we want to
# feed the GPX to our spoofing tool for auto walk.
# Also added a sleep for 2 seconds, because I was
# running into an issue every now and then where the
# script is looking for a file that is not fully
# created.
############################################################
import os, sys, getopt, time
from math import sin, cos, sqrt, atan2, radians
inFile = ''
outFile = ''
keepGPX = False
ERROR1 = 'Invalid number of parameters'
ERROR2 = 'Invalid parameter provided'
ERROR3 = 'ERROR: In file ({}) does not exists.'
ERROR4 = 'ERROR: Out file ({}) already exists.'
def combine(iFile, oFile):
with open(iFile, 'r') as f:
# do something here
lines = f.readlines()
title = '**'+iFile[:-4].replace('_', ' ').replace('Gible', 'G.i.b.l.e.')+'**'
with open(oFile, 'a') as of:
print(title, file=of)
for line in lines:
print('* {}'.format(line.replace('[', '\[').replace(']', '\]')), file=of)
def coolDown(file):
coords = []
cd = {0: '15 sec',
1: '30 sec',
2: '1 min',
3: '1.5 min',
4: '1.5 min',
5: '2 min',
6: '3 min',
7: '5 min',
8: '6 min',
9: '6 min',
10: '7 min',
11: '7 min',
12: '8 min',
13: '8 min',
14: '8 min',
15: '9 min',
16: '9 min',
17: '9 min',
18: '10 min',
19: '10 min',
20: '11 min',
21: '11 min',
22: '12 min',
23: '12 min',
24: '13 min',
25: '14 min',
26: '15 min',
27: '15 min',
28: '15 min',
29: '16 min',
30: '16 min',
65: '22 min',
81: '25 min',
100: '35 min',
250: '45 min',
500: '1 hr',
750: '1 hr 20 min',
1000: '1 hr 30 min',
1500: '2 hrs'}
with open(file, 'r') as f:
for line in f.readlines():
f_list = [float(i) for i in line.split(',') if i.strip()]
coords.append(f_list)
with open(file, 'w') as f:
# write back to the file
print(coords[0], file=f)
for x in range(1, len(coords)):
dist = distance(coords[x-1],coords[x])
rDist = round(dist, 0)
if rDist in range(31, 66): rDist = 65
elif rDist in range(66, 82): rDist = 81
elif rDist in range(82, 101): rDist = 100
elif rDist in range(101, 251): rDist = 250
elif rDist in range(251, 501): rDist = 500
elif rDist in range(501, 751): rDist = 750
elif rDist in range(751, 1001): rDist = 1000
elif rDist > 1000: rDist = 1500
print('{} distance {} km - cooldown {}'.format(coords[x], round(dist, 2), cd[rDist]), file=f)
def usage(name, msg):
# Parameters:
# name: string containing the name of the program itself
# msg: string of the error message that we want to print
# Description:
# Print usage message for our program
print('ERROR: {}\n Usage: {} -i <INPUTFILE> -o <OUTFILE>\n'.format(msg, name))
def trunc(fn):
# Parameters:
# fn: filename that we want to truncate the last line for
# Description:
# Removes the NL character from the last line of the file
lines = open(fn, 'r').readlines()
lastLine = (lines[-1].rstrip())
lines[-1] = lastLine
open(fn, 'w').writelines(lines)
return 0
def find_nth(myString, mySubString, n):
# Parameters
# myString: string that we will look in
# mySubString: string that we will look for inside of myString
# n: integer represent the # of the occurence of mySubString insode of myString
# Return:
# If found, return the location of the nth occurence of mySubString inside of myString
if (n == 1):
return myString.find(mySubString)
else:
return myString.find(mySubString, find_nth(myString, mySubString, n -1) + 1)
def findQuest(quest, questList):
# Parameters:
# quest: string representing the quest that we are looking for
# questList: Array of all of the available quests
# Return:
# If found, the possition in the array
n = -1
for x in range(len(questList)):
key = questList[x][0]
if quest == key:
return x
return n
def distance(c1, c2):
# Parameters:
# c1: tuple of float (lat, lon)
# c2: tuple of float (lat, lon)
# Return:
# Distance in km between c1 and c2: float
# Earth's radius at the equator in KM
R = 6378.0
lat1, lon1 = c1
lat2, lon2 = c2
dlat = radians(lat2 - lat1)
dlon = radians(lon2 - lon1)
a = ((sin(dlat / 2)**2) + ((cos(radians(lat1)) * cos(radians(lat2))) * (sin(dlon / 2)**2)))
c = 2 * atan2(sqrt(a), sqrt(1 - a))
d = R * c
return(d)
def main():
tmpFile = 'foobar'
Quests = []
# Read quests.txt file and only keep the coordinates
with open(inFile, 'r') as f:
line = f.readline()
while line:
n = find_nth(line, ' ', 2) + 1
coords = line[line.find(' ') + 1:n - 1]
qName = line[n:].rstrip().replace(':', ' -').replace(' ', '_')
questNumber = findQuest(qName, Quests)
if questNumber == -1:
Quests.append(['',''])
Quests[len(Quests) - 1][0] = qName
Quests[len(Quests) - 1][1] = coords
else:
Quests[questNumber][1] += ';' + coords
line = f.readline()
for quest in Quests:
with open(quest[0]+'.txt', 'w') as f:
qCoords = quest[1].split(';')
for x in range(len(qCoords)):
print(qCoords[x], file=f)
# Generate optimized GPX file
# For some un-Godly reason I have to remove the last new line from the file. This seems to be a
# bug with the library used to generate the optimized gpx route. I contacted the developer but
# have not received any answers. At some point I may look at fixing it myself, but for now this hack
# will do the trick.
#
# The library can be found at https://gitlab.com/3nvy/gpx-route-generator-console
ret_code = trunc(quest[0]+'.txt')
ret_code = os.system('node generate in='+quest[0]+'.txt out='+quest[0]+' type=2OPT count=50')
# Process GPX file
with open(quest[0]+'.gpx') as f:
lines = [line.strip() for line in f if line.startswith('<wpt')]
with open(quest[0]+'.out', 'w') as f:
for line in lines:
lat = line.split(' ')[1].split('"')[1]
lon = line.split(' ')[2].split('"')[1]
print("{},{}".format(lat,lon), file=f)
# Calculate distance
time.sleep(2)
coolDown(quest[0]+'.out')
combine(quest[0]+'.out', outFile)
if keepGPX:
ret_code = os.system('del '+quest[0]+'.txt')
else:
ret_code = os.system('del '+quest[0]+'.txt '+quest[0]+'.gpx')
print('{} generated'.format(quest[0]+'.out'))
def filesOK():
code = True
if not os.path.isfile(inFile):
print(ERROR3.format(inFile))
code = False
if os.path.isfile(outFile):
print(ERROR4.format(outFile))
code = False
return code
if __name__ == '__main__':
if (len(sys.argv) < 5):
usage(sys.argv[0], ERROR1)
sys.exit(1)
fCmd = sys.argv
argList = fCmd[1:]
sOptions = 'i:o:'
try:
args, vals = getopt.getopt(argList, sOptions)
except getopt.error as err:
usage(fCmd[0], err)
sys.exit(1)
for cArg, cVal in args:
if cArg in ('-i'):
inFile = cVal
elif cArg in ('-o'):
outFile = cVal
elif cArg in ('-g'):
keepGPX = True
else:
usage(fCmd[0], ERROR2)
if filesOK():
main()
sys.exit(0)
else:
sys.exit(1)