-
Notifications
You must be signed in to change notification settings - Fork 20
/
GeoAnimation.py
1316 lines (1141 loc) · 40.8 KB
/
GeoAnimation.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
import wx
from html import escape
from math import radians, degrees, sin, cos, asin, sqrt, atan2, modf, pi, floor
import random
import bisect
import datetime
import socket
from Version import AppVerName
from Animation import GetLapRatio
import Utils
from Utils import fld
from Utils import floatFormatLocale as ff
import xml.etree.ElementTree
import xml.etree.cElementTree
import xml.dom
import xml.dom.minidom
import collections
from getuser import lookup_username
import zipfile
from GpxParse import GpxParse
def LineNormal( x1, y1, x2, y2, normLen ):
''' Returns the coords of a normal line passing through x1, y1 of length normLen. '''
dx, dy = x2 - x1, y2 - y1
scale = (normLen / 2.0) / sqrt( dx**2 + dy**2 )
dx *= scale
dy *= scale
return x1 + dy, y1 - dx, x1 - dy, y1 + dx
def GreatCircleDistance( lat1, lon1, lat2, lon2 ):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees) in meters.
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, (lon1, lat1, lon2, lat2))
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2.0 * asin(sqrt(max(a, 0.0)))
m = 6371000.0 * c
return m
def GreatCircleDistance3D( lat1, lon1, ele1, lat2, lon2, ele2 ):
d = GreatCircleDistance( lat1, lon1, lat2, lon2 )
return sqrt( d ** 2 + (ele2 - ele1) ** 2 )
grad_speed = (
2.217527336052238,
2.202853110275395,
2.188053909555161,
2.1731282463704,
2.158074603708304,
2.142891434526821,
2.1275771612501315,
2.1121301753048813,
2.096548836705969,
2.0808314737019544,
2.06497638249156,
2.0489818270244,
2.032846038900783,
2.016567217387692,
2.000143529570188,
1.9835731106603496,
1.966854064488727,
1.9499844642068176,
1.93296235323291,
1.915785746478052,
1.8984526318939003,
1.880960972389858,
1.8633087081732884,
1.8454937595738268,
1.8275140304210298,
1.8093674120537766,
1.7910517880503405,
1.772565039779714,
1.7539050528880413,
1.7350697248488745,
1.7160569737225158,
1.6968647482884063,
1.6774910397351455,
1.6579338951157376,
1.6381914328010398,
1.6182618601922334,
1.598143493983527,
1.5778347832992043,
1.5573343360641365,
1.5366409490040733,
1.5157536417102468,
1.4946716952416133,
1.4733946957759005,
1.4519225838553647,
1.430255709802776,
1.4083948959035018,
1.3863415059564836,
1.3640975227843928,
1.3416656342536257,
1.3190493282790055,
1.2962529971645702,
1.2732820514472172,
1.250143043148757,
1.2268437979867222,
1.2033935556271051,
1.179803116464806,
1.1560849926753485,
1.132253560384181,
1.1083252087489648,
1.0843184805615627,
1.0602541976891038,
1.036155563355384,
1.0120482320197688,
0.9879603365842997,
0.9639224620353721,
0.9399675546183934,
0.9161307564793237,
0.8924491575934091,
0.868961459883586,
0.8457075527404815,
0.8227280045610927,
0.8000634810946754,
0.7777541077897695,
0.755838799278375,
0.7343545838357127,
0.7133359533865637,
0.6928142698501474,
0.6728172560960906,
0.6533685946811283,
0.6344876503870627,
0.6161893242166845,
0.598484037903228,
0.581377840100501,
0.5648726190130284,
0.548966401780926,
0.5336537186160659,
0.518926009368216,
0.5047720515425184,
0.49117839133466934,
0.4781297625120825,
0.4656094815064962,
0.45359981054187487,
0.44208228375211506,
0.43103799389939434,
0.42044783942572794,
0.410292733162104,
0.400553775128795,
0.3912123925580405,
0.3822504506358954,
0.37365033757169774,
0.3653950275316242,
0.35746812477661477,
0.34985389207223583,
0.34253726612546037,
0.3355038624776109,
0.3287399719624444,
0.3222325505357491,
0.31596920400500406,
0.3099381689382677,
0.30412829081150256,
0.29852900026236706,
0.2931302881543721,
0.2879226800158578,
0.28289721030091614,
0.27804539682146806,
0.27335921561865145,
0.2688310764750952,
0.2644537992153579,
0.26022059089788563,
0.2561250239665933,
0.25216101540213737,
0.24832280689087236,
0.2446049460123153,
0.24100226843277156,
0.2375098810828684,
0.23412314628945793
)
def GradeAdjustedDistance( lat1, lon1, ele1, lat2, lon2, ele2 ):
# More realistic effect of grade on speed based on power formula.
# Approximately, 25% grade hill is about 0.25 speed, 25% grade descent is about 2.0 speed.
distance = GreatCircleDistance( lat1, lon1, lat2, lon2 )
if not distance:
return 0.0
gradient = (ele2 - ele1) / distance
i = int( ((gradient + 0.25) / 0.5) * len(grad_speed) )
return distance / grad_speed[max( 0, min(i, len(grad_speed)-1) )]
LatLonEle = collections.namedtuple('LatLonEle', ['lat','lon','ele', 't'] )
GpsPoint = collections.namedtuple('GpsPoint', ['lat','lon','ele','x','y','d','dCum'] )
def triangle( t, a ):
a = float(a)
return (2 / a) * (t - a * int(t / a + 0.5)) * (-1 ** int(t / a + 0.5))
def CompassBearing(lat1, lon1, lat2, lon2):
"""
Calculates the bearing between two points.
"""
lat1 = radians(lat1)
lat2 = radians(lat2)
diffLong = radians(lon2 - lon1)
x = sin(diffLong) * cos(lat2)
y = cos(lat1) * sin(lat2) - (sin(lat1) * cos(lat2) * cos(diffLong))
initial_bearing = atan2(x, y)
# Now we have the initial bearing but math.atan2 return values
# from -180 to + 180 which is not what we want for a compass bearing
# The solution is to normalize the initial bearing as shown below
initial_bearing = degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
reGpxTime = re.compile( '[^0-9+]' )
def GpxHasTimes( fname ):
''' Check that the gpx file contains valid times. '''
points = GpxParse( fname )
tLast = None
for p in points:
try:
t = p['time']
except KeyError:
return False
if tLast is not None and tLast > t:
return False
tLast = t
return True
def LatLonElesToGpsPoints( latLonEles, useTimes = False, isPointToPoint = False ):
hasTimes = useTimes
latMin, lonMin = 1000.0, 1000.0
for latLonEle in latLonEles:
if latLonEle.t is None:
hasTimes = False
if latLonEle.lat < latMin:
latMin = latLonEle.lat
if latLonEle.lon < lonMin:
lonMin = latLonEle.lon
gpsPoints = []
dCum = 0.0
for i in range(len(latLonEles) - (1 if isPointToPoint else 0)):
p, pNext = latLonEles[i], latLonEles[(i+1) % len(latLonEles)]
if hasTimes:
if pNext.t > p.t:
gad = (pNext.t - p.t).total_seconds()
else:
# Estimate the last time difference based on the speed of the last segment.
pPrev = latLonEles[(i+len(latLonEles)-1)%len(latLonEles)]
d = GreatCircleDistance( pPrev.lat, pPrev.lon, p.lat, p.lon )
t = (p.t - pPrev.t).total_seconds()
if t > 0:
s = d / t
gad = GreatCircleDistance( p.lat, p.lon, pNext.lat, pNext.lon ) / s
else:
gad = 0.0
else:
gad = GradeAdjustedDistance( p.lat, p.lon, p.ele, pNext.lat, pNext.lon, pNext.ele )
x = GreatCircleDistance( latMin, lonMin, latMin, p.lon )
y = GreatCircleDistance( latMin, lonMin, p.lat, lonMin )
if gad > 0.0:
gpsPoints.append( GpsPoint(p.lat, p.lon, p.ele, x, y, gad, dCum) )
dCum += gad
return gpsPoints
def ParseGpxFile( fname, useTimes = False, isPointToPoint = False ):
points = GpxParse( fname )
latLonEles = []
for p in points:
lat, lon, ele, t = p['lat'], p['lon'], p.get('ele',0.0), p.get('time', None)
# Skip consecutive duplicate points.
try:
if latLonEles[-1].lat == lat and latLonEles[-1].lon == lon:
continue
except IndexError:
pass
latLonEles.append( LatLonEle(lat, lon, ele, t) )
return latLonEles
def createAppendChild( doc, parent, name, textAttr={} ):
child = doc.createElement( name )
parent.appendChild( child )
for k, v in textAttr.items():
attr = doc.createElement( k )
if isinstance(v, float) and modf(v)[0] == 0.0:
v = int(v)
attr.appendChild( doc.createTextNode( '{:.6f}'.format(v) if isinstance(v, float) else '{}'.format(v) ) )
child.appendChild( attr )
return child
def createAppendTextChild( doc, parent, text ):
child = doc.createTextNode( text )
parent.appendChild( child )
return child
def CreateGPX( courseName, gpsPoints ):
''' Create a GPX file from the gpsPoints list. '''
doc = xml.dom.minidom.Document()
gpx = createAppendChild( doc, doc, 'gpx' )
gpx.attributes['creator'] = AppVerName + ' http://sites.google.com/site/crossmgrsoftware/'
gpx.attributes['xmlns'] ="http://www.topografix.com/GPX/1/0"
gpx.attributes['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
gpx.attributes['xsi:schemaLocation'] = "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd"
gpx.appendChild( doc.createComment( '\n'.join( [
'',
'DO NOT EDIT!',
'',
'This file was created automatically by {}.'.format(AppVerName),
'',
'For more information, see http://sites.google.com/site/crossmgrsoftware',
'',
'Created: {}'.format(datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' )),
'User: {}'.format(escape(lookup_username())),
'Computer: {}'.format(escape(socket.gethostname())),
'', ] )
) )
trk = createAppendChild( doc, gpx, 'trk', {
'name': courseName,
} )
trkseg = createAppendChild( doc, trk, 'trkseg' )
def fmt( v ):
return '{:.7f}'.format(v).rstrip('0').rstrip('.')
for p in gpsPoints:
trkpt = createAppendChild( doc, trkseg, 'trkpt' )
trkpt.attributes['lat'] = fmt(p.lat)
trkpt.attributes['lon'] = fmt(p.lon)
if p.ele:
ele = createAppendChild( doc, trkpt, 'ele' )
createAppendTextChild( doc, ele, fmt(p.ele) )
return doc
class GeoTrack:
def __init__( self ):
self.gpsPoints = []
self.distanceTotal = 0.0
self.cumDistance = []
self.x = 0
self.xMax = self.yMax = 0.0
self.yBottom = 0
self.mult = 1.0
self.length = 0.0
self.totalElevationGain = 0.0
self.isPointToPoint = False
self.cache = {}
def computeSummary( self ):
lenGpsPoints = len(self.gpsPoints)
length = 0.0
totalElevationGain = 0.0
self.isPointToPoint = getattr( self, 'isPointToPoint', False )
for i in range(lenGpsPoints - (1 if self.isPointToPoint else 0)):
pCur, pNext = self.gpsPoints[i], self.gpsPoints[(i + 1) % lenGpsPoints]
length += GreatCircleDistance3D( pCur.lat, pCur.lon, pCur.ele, pNext.lat, pNext.lon, pNext.ele )
totalElevationGain += max(0.0, pNext.ele - pCur.ele)
self.length = length
self.totalElevationGain = totalElevationGain
def setPoints( self, gpsPoints, isPointToPoint = False ):
self.gpsPoints = gpsPoints
self.isPointToPoint = isPointToPoint
# Default max to 1 to avoid divisivion by zero errors.
self.xMax = max( (p.x for p in self.gpsPoints), default=1 )
self.yMax = max( (p.y for p in self.gpsPoints), default=1 )
dCum = 0.0
self.cumDistance = []
for p in self.gpsPoints:
self.cumDistance.append( dCum )
dCum += p.d
self.distanceTotal = dCum
self.computeSummary()
def read( self, fname, useTimes = False, isPointToPoint = False ):
self.isPointToPoint = isPointToPoint
latLonEles = ParseGpxFile( fname, useTimes=useTimes, isPointToPoint=isPointToPoint )
gpsPoints = LatLonElesToGpsPoints( latLonEles, useTimes=useTimes, isPointToPoint=isPointToPoint )
self.setPoints( gpsPoints, isPointToPoint=isPointToPoint )
def getGPX( self, courseName ):
return CreateGPX( courseName, self.gpsPoints )
def writeGPXFile( self, fname ):
with open( fname, 'w', encoding='utf8') as fp:
self.getGPX( os.path.splitext(os.path.basename(fname))[0] ).writexml(fp, indent="", addindent=" ", newl="\n", encoding='utf8')
def readElevation( self, fname ):
header = None
distance, elevation = [], []
iDistance, iElevation = None, None
with open(fname, 'r', encoding='utf8') as fp:
for line in fp:
fields = [f.strip() for f in line.split(',')]
if not header:
header = fields
for i, h in enumerate(header):
h = h.lower()
if h.startswith('distance'):
iDistance = i
elif h.startswith('elevation'):
iElevation = i
assert iDistance is not None and iElevation is not None, 'Invalid header in file.'
else:
distance.append( float(fields[iDistance]) )
elevation.append( float(fields[iElevation]) )
if len(elevation) < 2:
return
lenGpsPoints = len(self.gpsPoints)
length = 0.0
for i in range(lenGpsPoints-1):
pCur, pNext = self.gpsPoints[i], self.gpsPoints[(i + 1) % lenGpsPoints]
length += GreatCircleDistance( pCur.lat, pCur.lon, pNext.lat, pNext.lon )
distanceMult = distance[-1] / length
# Update the known GPS points with the proportional elevation.
length = 0.0
iSearch = 0
for i in range(lenGpsPoints):
pCur, pNext = self.gpsPoints[i], self.gpsPoints[(i + 1) % lenGpsPoints]
d = min( length * distanceMult, distance[-1] )
for iSearch in range(iSearch, len(elevation) - 2):
if distance[iSearch] <= d < distance[iSearch+1]:
break
deltaDistance = max( distance[iSearch+1] - distance[iSearch], 0.000001 )
ele = elevation[iSearch] + (elevation[iSearch+1] - elevation[iSearch]) * \
(d - distance[iSearch]) / deltaDistance
self.gpsPoints[i] = pCur._replace( ele = ele )
length += GreatCircleDistance( pCur.lat, pCur.lon, pNext.lat, pNext.lon )
self.computeSummary()
def getXYTrack( self ):
x, yBottom, mult = self.x, self.yBottom, self.mult
return [(p.x * mult + x, yBottom - p.y * mult) for p in self.gpsPoints]
def asExportJson( self ):
return [ [int(getattr(p, a)*10.0) for a in ('x', 'y', 'd')] for p in self.gpsPoints ]
def getAltigraph( self ):
if not self.gpsPoints or all( p.ele == 0.0 for p in self.gpsPoints ):
return []
altigraph = [(0.0, self.gpsPoints[0].ele)]
p = self.gpsPoints
for i in range(1, len(p)):
altigraph.append( (altigraph[-1][0] + GreatCircleDistance(p[i-1].lat, p[i-1].lon, p[i].lat, p[i].lon), p[i].ele) )
altigraph.append( (altigraph[-1][0] + GreatCircleDistance(p[-1].lat, p[-1].lon, p[0].lat, p[0].lon), p[0].ele) )
return altigraph
def isClockwise( self ):
if not self.gpsPoints:
return False
p = self.gpsPoints
return sum( (p[j].x - p[j-1].x) * (p[j].y + p[j-1].y) for j in range(len(self.gpsPoints)) ) > 0.0
def reverse( self ):
''' Reverse the points in the track. Make sure the distance to the next point and cumDistance is correct. '''
self.cumDistance = []
gpsPointsReversed = []
dCum = 0.0
for i in range(len(self.gpsPoints)-1, -1, -1):
p = self.gpsPoints[i]
pPrev = self.gpsPoints[i-1 if i > 0 else len(self.gpsPoints)-1]
gpsPointsReversed.append( GpsPoint(p.lat, p.lon, p.ele, p.x, p.y, pPrev.d, dCum) )
self.cumDistance.append( dCum )
dCum += pPrev.d
self.gpsPoints = gpsPointsReversed
def setClockwise( self, clockwise = True ):
if self.isClockwise() != clockwise:
self.reverse()
def asCoordinates( self ):
coordinates = []
for p in self.gpsPoints:
coordinates.append( p.lon )
coordinates.append( p.lat )
return coordinates
def asKmlTour( self, raceName=None, speedKMH=None ):
race = raceName or 'Race'
speed = (speedKMH or 35.0) * (1000.0 / (60.0*60.0))
doc = xml.dom.minidom.Document()
kml = createAppendChild( doc, doc, 'kml' )
kml.attributes['xmlns'] = "http://www.opengis.net/kml/2.2"
kml.attributes['xmlns:gx'] = "http://www.google.com/kml/ext/2.2"
kml.appendChild( doc.createComment( '\n'.join( [
'',
'DO NOT EDIT!',
'',
'This file was created automatically by CrossMgr.',
'',
'Is shows a fly-through of the actual bicycle race course.',
'For more information, see http://sites.google.com/site/crossmgrsoftware',
'',
'Created: {}'.format(datetime.datetime.now().strftime( '%Y/%m/%d %H:%M:%S' )),
'User: {}'.format(escape(lookup_username())),
'Computer: {}'.format(escape(socket.gethostname())),
'', ] )
) )
Document = createAppendChild( doc, kml, 'Document', {
'open': 1,
'name': raceName,
} )
# Define some styles.
Style = createAppendChild( doc, Document, 'Style' )
Style.attributes['id'] = 'thickBlueLine'
LineStyle = createAppendChild( doc, Style, 'LineStyle', {
'width': 5,
'color': '#7fff0000', # aabbggrr
} )
# Define an flying tour around the course.
Tour = createAppendChild( doc, Document, 'gx:Tour', {
'name': '{}: Tour'.format(race),
} )
Playlist = createAppendChild( doc, Tour, 'gx:Playlist' )
def fly( doc, PlayList, p, mode, duration, heading ):
FlyTo = createAppendChild( doc, Playlist, 'gx:FlyTo', {
'gx:duration': duration,
'gx:flyToMode': mode,
} )
Camera = createAppendChild( doc, FlyTo, 'Camera', {
'latitude': p.lat,
'longitude': p.lon,
'altitude': 2,
'altitudeMode': 'relativeToGround',
'heading': heading,
'tilt': 80,
} )
# Fly to the starting point.
p, pNext = self.gpsPoints[:2]
fly( doc, Playlist, p, 'bounce', 3, CompassBearing(p.lat, p.lon, pNext.lat, pNext.lon) )
# Follow the path through all the points.
lenGpsPoints = len(self.gpsPoints)
for i in range(1, lenGpsPoints + (0 if self.isPointToPoint else 1)):
pPrev, p = self.gpsPoints[i-1], self.gpsPoints[i%lenGpsPoints]
fly(doc, Playlist, p, 'smooth',
GreatCircleDistance(pPrev.lat, pPrev.lon, p.lat, p.lon) / speed,
CompassBearing(pPrev.lat, pPrev.lon, p.lat, p.lon)
)
if self.isPointToPoint:
# Marker for the start line.
Placemark = createAppendChild( doc, Document, 'Placemark', {'name': '{}: Start Line'.format(race)} )
createAppendChild( doc, Placemark, 'Point', {
'coordinates': '{},{}'.format(self.gpsPoints[0].lon, self.gpsPoints[0].lat)
} )
pFinish = self.gpsPoints[-1]
else:
pFinish = self.gpsPoints[0]
# Marker for the finish line.
Placemark = createAppendChild( doc, Document, 'Placemark', {'name': '{}: Finish Line'.format(race)} )
createAppendChild( doc, Placemark, 'Point', {'coordinates': '{},{}'.format(pFinish.lon, pFinish.lat)} )
# Path for the course.
Placemark = createAppendChild( doc, Document, 'Placemark', {
'name': '{}: Course'.format(race),
'styleUrl': '#thickBlueLine',
} )
coords = ['']
for i in range(lenGpsPoints + (0 if self.isPointToPoint else 1)):
p = self.gpsPoints[i % lenGpsPoints]
coords.append( '{},{}'.format(p.lon, p.lat) )
coords.append('')
LineString = createAppendChild( doc, Placemark, 'LineString', {
'tessellate': 1,
'altitudeMode': 'clampToGround',
'coordinates': '\n'.join(coords),
} )
ret = doc.toprettyxml( indent=' ' )
doc.unlink()
return ret
def getXY( self, lap, id = None ):
# Find the segment at this distance in the lap.
lap = modf(lap)[0] # Get fraction of lap.
lapDistance = lap * self.distanceTotal # Get distance traveled in the lap.
# Avoid the cost of the binary search by checking if the id request is still on the last segment.
lenGpsPoints = len(self.gpsPoints)
try:
i = self.cache[id]
pCur = self.gpsPoints[i]
if not (pCur.dCum <= lapDistance <= pCur.dCum + pCur.d):
i = (i + 1) % lenGpsPoints
pCur = self.gpsPoints[i]
if not (pCur.dCum <= lapDistance <= pCur.dCum + pCur.d):
i = None
except (IndexError, KeyError):
i = None
if i is None:
# Find the closest point LT the lap distance.
i = bisect.bisect_right( self.cumDistance, lapDistance )
i %= lenGpsPoints
if self.cumDistance[i] > lapDistance:
i -= 1
self.cache[id] = i
pCur, pNext = self.gpsPoints[i], self.gpsPoints[(i + 1) % lenGpsPoints]
segDistance = lapDistance - self.cumDistance[i]
segRatio = 0.0 if pCur.d <= 0.0 else segDistance / pCur.d
x, y = pCur.x + (pNext.x - pCur.x) * segRatio, pCur.y + (pNext.y - pCur.y) * segRatio
return x * self.mult + self.x, self.yBottom - y * self.mult
@property
def lengthKm( self ):
return self.length / 1000.0
@property
def lengthMiles( self ):
return self.length * 0.621371/1000.0
@property
def totalElevationGainM( self ):
try:
return self.totalElevationGain
except AttributeError:
self.totalElevationGain = sum( max(0.0, self.gpsPoints[i].ele - self.gpsPoints[i-1].ele) for i in range(len(self.gpsPoints)) )
return self.totalElevationGain
@property
def totalElevationGainFt( self ):
return self.totalElevationGainM * 3.28084
@property
def numPoints( self ):
return len(self.gpsPoints)
def setDisplayRect( self, x, y, width, height ):
if width <= 0 or height <= 0:
self.mult = 1.0
self.x = x
self.yBottom = y + height
return
mult = min( width / self.xMax, height / self.yMax )
w, h = self.xMax * mult, self.yMax * mult
xBorder = (width - w) / 2.0
#yBorder = (height - h) / 2.0
self.mult = mult
self.x = xBorder + x
self.yBottom = y + height
shapes = [ [(cos(a), -sin(a))
for a in (q*(2.0*pi/i)+pi/2.0+(2.0*pi/(i*2.0) if i % 2 == 0 else 0)
for q in range(i))] for i in range(3,9)]
def DrawShape( dc, num, x, y, radius ):
dc.DrawPolygon( [ wx.Point(int(p*radius+x), int(q*radius+y)) for p,q in shapes[num % len(shapes)] ] )
class GeoAnimation(wx.Control):
topFewCount = 5
infoLines = 1
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator,
name="GeoAnimation"):
super().__init__(parent, id, pos, size, style, validator, name)
self.SetBackgroundColour('white')
self.data = {}
self.categoryDetails = {}
self.t = 0
self.tMax = None
self.tDelta = 1
self.r = 100 # Radius of the turns of the fictional track.
self.laneMax = 8
self.geoTrack = None
self.compassLocation = ''
self.widthLast = -1
self.heightLast = -1
self.xBanner = 300
self.tBannerLast = None
self.course = 'geo'
self.units = 'km'
self.framesPerSecond = 32
self.lapCur = 0
self.iLapDistance = 0
self.tLast = datetime.datetime.now()
self.speedup = 1.0
self.suspendGeoAnimation = False
self.numsToWatch = set()
self.checkeredFlag = wx.Bitmap(os.path.join(Utils.getImageFolder(), 'CheckeredFlag.png'), wx.BITMAP_TYPE_PNG)
trackRGB = [int('7FE57F'[i:i+2],16) for i in range(0, 6, 2)]
self.trackColour = wx.Colour( *trackRGB )
self.colours = []
k = [0,32,64,128,128+32,128+64,255]
for r in k:
for g in k:
for b in k:
if sum( abs(c - t) for c, t in zip([r,g,b],trackRGB) ) > 80 and \
sum( c for c in [r,g,b] ) > 64:
self.colours.append( wx.Colour(r, g, b) )
random.seed( 1234 )
random.shuffle( self.colours )
self.topFewColours = [
wx.Colour(255,215,0),
wx.Colour(230,230,230),
wx.Colour(205,133,63)
]
while len(self.topFewColours) < self.topFewCount:
self.topFewColours.append( wx.Colour(200,200,200) )
self.trackColour = wx.Colour( *[int('7FE57F'[i:i+2],16) for i in range(0, 6, 2)] )
# Cache the fonts if the size does not change.
self.numberFont = None
self.timeFont = None
self.highlightFont = None
self.rLast = -1
self.timer = wx.Timer( self, id=wx.ID_ANY )
self.Bind( wx.EVT_TIMER, self.NextFrame, self.timer )
# Bind the events related to our control: first of all, we use a
# combination of wx.BufferedPaintDC and an empty handler for
# wx.EVT_ERASE_BACKGROUND (see later) to reduce flicker
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
def SetGeoTrack( self, geoTrack ):
if self.geoTrack != geoTrack:
self.geoTrack = geoTrack
def SetOptions( self, *argc, **kwargs ):
pass
def DoGetBestSize(self):
return wx.Size(400, 200)
def _initGeoAnimation( self ):
self.tLast = datetime.datetime.now()
self.suspendGeoAnimation = False
def Animate( self, tRunning, tMax = None, tCur = 0.001 ):
self.StopAnimate()
self._initGeoAnimation()
self.t = tCur
if not self.data:
return
if tMax is None:
tMax = 0
for num, info in self.data.items():
try:
tMax = max(tMax, info['raceTimes'][-1])
except IndexError:
pass
self.speedup = float(tMax) / float(tRunning)
self.tMax = tMax
self.timer.Start( int(1000.0/self.framesPerSecond), False )
def StartAnimateRealtime( self ):
self.StopAnimate()
self._initGeoAnimation()
self.speedup = 1.0
self.tMax = 999999
self.timer.Start( int(1000.0/self.framesPerSecond), False )
def StopAnimate( self ):
if self.timer.IsRunning():
self.timer.Stop()
self.tBannerLast = None
def SetNumsToWatch( self, numsToWatch ):
self.numsToWatch = numsToWatch
self.Refresh()
def SuspendAnimate( self ):
self.suspendGeoAnimation = True
def IsAnimating( self ):
return not self.suspendGeoAnimation and self.timer.IsRunning()
def SetTime( self, t ):
self.t = t
self.Refresh()
def NextFrame( self, event ):
if event.GetId() == self.timer.GetId():
tNow = datetime.datetime.now()
tDelta = tNow - self.tLast
self.tLast = tNow
secsDelta = tDelta.seconds + tDelta.microseconds / 1000000.0
self.SetTime( self.t + secsDelta * self.speedup )
if self.suspendGeoAnimation or self.t >= self.tMax:
self.StopAnimate()
def SetForegroundColour(self, colour):
wx.Control.SetForegroundColour(self, colour)
self.Refresh()
def SetBackgroundColour(self, colour):
wx.Control.SetBackgroundColour(self, colour)
self.Refresh()
def GetDefaultAttributes(self):
"""
Overridden base class virtual. By default we should use
the same font/colour attributes as the native wx.StaticText.
"""
return wx.StaticText.GetClassDefaultAttributes()
def ShouldInheritColours(self):
"""
Overridden base class virtual. If the parent has non-default
colours then we want this control to inherit them.
"""
return True
def SetData( self, data, tCur = None, categoryDetails = None ):
"""
* data is a rider information indexed by number. Info includes lap times and lastTime times.
* lap times should include the start offset.
Example:
data = { 101: { raceTimes: [xx, yy, zz], lastTime: None }, 102 { raceTimes: [aa, bb], lastTime: cc} }
"""
self.data = data if data else {}
self.categoryDetails = categoryDetails if categoryDetails else {}
for num, info in self.data.items():
info['iLast'] = 1
if info['status'] == 'Finisher' and info['raceTimes']:
info['finishTime'] = info['raceTimes'][-1]
else:
info['finishTime'] = info['lastTime']
# Get the units.
for num, info in self.data.items():
if info['status'] == 'Finisher':
try:
self.units = 'miles' if 'mph' in info['speed'] else 'km'
except KeyError:
self.units = 'km'
break
if tCur is not None:
self.t = tCur
self.tBannerLast = None
self.Refresh()
def getShortName( self, num ):
try:
info = self.data[num]
except KeyError:
return ''
lastName = info.get('LastName','')
firstName = info.get('FirstName','')
if lastName:
if firstName:
return '%s, %s.' % (lastName, firstName[:1])
else:
return lastName
return firstName
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
def OnSize(self, event):
self.Refresh()
event.Skip()
def getRiderPositionTime( self, num ):
""" Returns the fraction of the lap covered by the rider and the time. """
if num not in self.data:
return (None, None)
info = self.data[num]
raceTimes = info['raceTimes']
if not raceTimes or self.t < raceTimes[0] or len(raceTimes) < 2:
return (None, None)
tSearch = self.t
finishTime = info['finishTime']
if finishTime is not None and finishTime < self.t:
if finishTime == raceTimes[-1]:
return (len(raceTimes), finishTime)
tSearch = finishTime
if tSearch >= raceTimes[-1]:
p = len(raceTimes) + float(tSearch - raceTimes[-1]) / float(raceTimes[-1] - raceTimes[-2])
else:
i = info['iLast']
if not (raceTimes[i-1] < tSearch <= raceTimes[i]):
i += 1
if not (raceTimes[i-1] < tSearch <= raceTimes[i]):
i = bisect.bisect_left( raceTimes, tSearch )
info['iLast'] = i
if i == 1:
firstLapRatio = info['flr']
p = float(tSearch - raceTimes[i-1]) / float(raceTimes[i] - raceTimes[i-1])
p = 1.0 - firstLapRatio + p * firstLapRatio
p -= floor(p) - 1.0
else:
p = i + float(tSearch - raceTimes[i-1]) / float(raceTimes[i] - raceTimes[i-1])
return (p, tSearch)
def getRiderXYPT( self, num ):
positionTime = self.getRiderPositionTime( num )
if positionTime[0] is None:
return None, None, None, None
if self.data[num]['finishTime'] is not None and self.t >= self.data[num]['finishTime']:
self.lapCur = max(self.lapCur, len(self.data[num]['raceTimes']))
return (None, None, positionTime[0], positionTime[1])
self.lapCur = max(self.lapCur, int(positionTime[0]))
xy = self.geoTrack.getXY( positionTime[0], num )
return xy[0], xy[1], positionTime[0], positionTime[1]
def drawBanner( self, dc, width, height, tHeight, bannerItems ):
blue = wx.Colour(0, 0, 200)
dc.SetPen( wx.Pen(blue) )
dc.SetBrush( wx.Brush(blue, wx.SOLID) )
dc.DrawRectangle( 0, 0, int(width), int(tHeight*1.1) )
if not bannerItems:
return
y = tHeight * 0.1
tHeight *= 0.85
x = self.xBanner
while x < width:
for bi in bannerItems:
if x >= width:
break
position, num, name = '{}'.format(bi[1]), '{}'.format(bi[0]), self.getShortName(bi[0])
if position == '1':
x += tHeight / 2
tWidth = self.checkeredFlag.Width
if x + tWidth > 0 and x < width:
dc.DrawBitmap( self.checkeredFlag, int(x), int(y), False )
x += tWidth + tHeight / 2
dc.SetFont( self.positionFont )
tWidth = dc.GetTextExtent( position )[0]
if x + tWidth > 0 and x < width:
dc.SetTextForeground( wx.WHITE )
dc.DrawText( position, int(x), int(y) )
x += tWidth + tHeight / 4
dc.SetFont( self.bibFont )
tWidth = dc.GetTextExtent(num)[0]
if x + tWidth > 0 and x < width:
dc.SetTextForeground( 'YELLOW' )
dc.DrawText(num, int(x), int(y) )
x += tWidth + tHeight / 3
dc.SetFont( self.nameFont )
tWidth = dc.GetTextExtent(name)[0]
if x + tWidth > 0 and x < width:
dc.SetTextForeground( wx.WHITE )
dc.DrawText(name, int(x), int(y) )
x += tWidth + tHeight
if x < 0:
self.xBanner = x
tBanner = datetime.datetime.now()