source: subversion/applications/routing/pyroute/follow/follow.py

Last change on this file was 5359, checked in by ojw, 12 years ago

replayer for tracklogs

File size: 1.2 KB
Line 
1import sys
2from xml.sax import make_parser, handler
3from time import *
4
5class LoadTrack(handler.ContentHandler):
6  def __init__(self, filename):
7    self.inTrkpt = 0
8    self.inTime = 0
9    self.samples = []
10    parser = make_parser()
11    parser.setContentHandler(self)
12    parser.parse(filename)
13 
14  def startElement(self, name, attrs):
15    """Handle XML elements"""
16    if name == "trkpt":
17      self.tags = { \
18        'lat':float(attrs.get('lat')), 
19        'lon':float(attrs.get('lon'))}
20      self.inTrkpt = 1
21    if name == "time":
22      if self.inTrkpt:
23        self.inTime = 1
24        self.timeStr = ''
25     
26  def characters(self, content):
27    if(self.inTime):
28      self.timeStr = self.timeStr + content
29 
30  def endElement(self, name):
31    if name == 'time':
32      if(self.inTime):
33        self.inTime = 0
34        self.tags['t'] = mktime(strptime(self.timeStr[0:-1], "%Y-%m-%dT%H:%M:%S"))
35    if name == 'trkpt':
36      self.samples.append(self.tags)
37      self.inTrkpt = 0
38   
39  def play(self, filename):
40    for pos in self.samples:
41      file = open(filename, 'w')
42      file.write("%f,%f\n" % (pos['lat'],pos['lon']))
43      file.close
44      sleep(0.5)
45     
46if __name__ == "__main__":
47  track = LoadTrack(sys.argv[1])
48  track.play(sys.argv[2])
Note: See TracBrowser for help on using the repository browser.