# gpst.py

Sep 9th, 2015
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #qpy:2
2. #qpy:console
3.
4. #import androidhelper as android
5. import time, urllib2, math
6.
7. def glt():
8. return time.localtime()
9.
10. def dtr(d):
11. return d*3.1415926535/180
12.
13. def hmstr(t): # secs to "9 mins" or "50 secs"
14. if t>=3600 : return str(t/3600)+" hrs"
15. if t>=600 : return str(t/60)+ " mins"
16. if t>=60 : return ' '+str(t/60)+" mins"
17. if t>=10 : return str(t) + " secs"
18. if t>=0 : return ' '+str(t) +" secs"
19.
20. def dist(la, lo, lat, lon):
21. r=6371000
22. dla = dtr(lat-la)
23. dlo = dtr(lon-lo)
24.
25. a = math.sin(dla/2)**2 + math.cos(dtr(la))*math.cos(dtr(lat)) * math.sin(dlo/2)**2
26.
27. c = 2 * math.atan2(a**0.5, (1-a)**0.5)
28. return r * c
29.
30. def secs(hhmmss):
31. return 3600*int(hhmmss[0:2])+60*int(hhmmss[3:5])+int(hhmmss[6:8])
32.
33. def hms(secs):
34. h, m, s = secs/3600, (secs%3600)/60, secs%60
35. return str(h/10)+str(h%10)+':'+str(m/10)+str(m%10)+':'+str(s/10)+str(s%10)
36.
37. def isv(serv):
38. lt = glt()
39. return isAv(serv, str(lt[0])+str(lt[1]/10)+str(lt[1]%10)+str(lt[2]/10)+str(lt[2]%10), lt[6]);
40.
41. def isAv(serv, date, dow): # date is 'YYYYMMAA' # dow is from Mon 0
42. s=sum([s[2] for s in cd if s[0]==serv and s[1]==date])
43. if s>0: return s==1
44. date = int(date)
45. return sum([int(s[dow+1]) for s in cl if s[0]==serv and date>=int(s[8]) and date<=int(s[9])])>0
46.
47.
50. droot = 'Documents/Tan/'
51. st = [s.split(',') for s in open(droot+'stops.txt').readlines()[1:]]
52. ts = [s.split(',') for s in open(droot+'stop_times.txt').readlines()[1:]]
53. tr = [s.split(',') for s in open(droot+'trips.txt').readlines()[1:]]
54. rt = [s.split(',') for s in open(droot+'routes.txt').readlines()[1:]]
55. cl = [s.split(',') for s in open(droot+'calendar.txt').readlines()[1:]]
56. cd = [s.split(',') for s in open(droot+'calendar_dates.txt').readlines()[1:]]
57.
58. pt = [s for s in st if s[7]=='0']
59. st = [s for s in st if s[7]=='1']
60. print 'Setup complete!'
61. # stops, times, trips, routes, points
62. gps = False
63. home= True
64. loop= True
65.
66. while loop:
67. found = False
68. if gps:
69. time.sleep(2)
70. droid=android.Android()
71. droid.startLocating()
72. event=droid.eventWaitFor('location', 10000).result
73. try:
74. lng = -1.55716847 if home else -1.51530761
75. lat = 47.25072151 if home else 47.28189980
76. if gps:
77. lng = event['data']['gps']['longitude']
78. lat = event['data']['gps']['latitude']
79. print "Lng: %s Lat: %s" %(lng,lat)
80. found = True
81. except:
82. print "Network Coordinates"
83. if found:
84. print "Calculating..."
85. sttime = time.time();
86. prox = []
87. for s in st[1:]:
88. la = float(s[3])
89. lo = float(s[4])
90. nm = s[1]
91. ds = dist(la, lo, lat, lng)
92. prox.append((ds, nm[1:-1], s[0]))
93. prox.sort()
94. lt = glt()
95. dow = lt[6] if lt[3]>3 else (lt[6]+6)%7
96. now = 3600*lt[3]+60*lt[4]+lt[5]+(84600 if lt[3]<4 else 0)
97. ids = [s[2] for s in prox]
98. stt = [s for s in pt if s[8] in ids]
99. for i in range(8):
100. stop=prox[i]
101. print stop[1]+' ('+str(int(stop[0]))+'m)'
102. if i<4:
103. sts = [s[0] for s in stt if s[8]==stop[2]]
104. pxr = [(secs(s[2])-now, s) for s in ts if s[3] in sts and secs(s[2])>now and secs(s[2])<=now+1800]
105. pxr = [(p[0], p[1], [s for s in tr if s[2]==p[1][0]][0]) for p in pxr]
106. prx = [(p[0], p[1], p[2], [r for r in rt if r[0]==p[2][0]][0]) for p in pxr if isv(p[2][1]) ]
107. prx.sort()
108. for p in prx:
109. print ' '+hmstr(p[0])+' - '+p[3][2]+' vers '+p[2][3][1:-1]
110. print 'Time : '+str(int(1000*(time.time()-sttime)))+' ms'
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. if not gps:
139. time.sleep(3)
140. loop=False
RAW Paste Data