Package sabx10 :: Package utils :: Module tcx2sabx
[hide private]
[frames] | no frames]

Source Code for Module sabx10.utils.tcx2sabx

 1  ############################################################################### 
 2  # 
 3  # sabx10 - an SABX file manipulation library 
 4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
 5  # 
 6  # This file is part of sabx10. 
 7  # 
 8  # sabx10 is free software: you can redistribute it and/or modify it under the 
 9  # terms of the GNU General Public License as published by the Free Software 
10  # Foundation, either version 3 of the License, or (at your option) any later 
11  # version. 
12  # 
13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU General Public License along with 
18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
19  # 
20  ############################################################################### 
21  """ 
22  Handle converting TCX files to SABX 1.0 files. 
23  """ 
24   
25  import os.path 
26  import sys 
27  import uuid 
28   
29  from sabx10.oxm import parse_no_def_namespaces 
30  from sabx10.templating import TemplateProcessor 
31   
32 -class TcxProcessor(TemplateProcessor):
33 """ 34 Process a TCX file, pulling out all its point data for processing. Add in 35 a uuid and title, then process it all through a jinja2 template. This 36 class sub-classes L{TemplateProcessor} to do most of the grunt work in 37 handling command-line options and processing the jinja2 template. 38 """ 39
40 - def __init__(self, template_file=None, man=None):
41 """ 42 Add C{optparse} options for the infile and the index. Seed 43 template_data with uuid. 44 45 @param template_file: (optional) file name of template file 46 @type template_file: C{string} 47 @param man: (optional) extended program help 48 @type man: C{string} 49 """ 50 TemplateProcessor.__init__(self, template_file, man) 51 52 self.parser.add_option("-i", "--infile", dest="in_file", 53 help="input tcx data from FILE", 54 metavar="FILE") 55 self.parser.add_option("-n", "--index", dest="start_index", 56 type="int", default=1, 57 help="starting point index") 58 59 self.template_data['uuid'] = uuid.uuid4()
60
61 - def get_template_data(self):
62 """ 63 Get the point data from the TCX file and save it in template_data. 64 """ 65 if self.options.in_file: 66 self.in_file = open(self.options.in_file, "r") 67 self.template_data['title'] = \ 68 os.path.basename(self.options.in_file) 69 else: 70 self.in_file = sys.stdin 71 self.template_data['title'] = "stdin" 72 73 tree = parse_no_def_namespaces(self.in_file) 74 count = self.options.start_index 75 laps = tree.findall('Activities/Activity/Lap') 76 points = [] 77 for lap in laps: 78 pts = lap.findall('Track/Trackpoint') 79 for pt in pts: 80 if pt.find('Position') and \ 81 pt.findtext('AltitudeMeters'): 82 points.append( {'index': count, 83 'lat': pt.findtext( 84 'Position/LatitudeDegrees'), 85 'lon': pt.findtext( 86 'Position/LongitudeDegrees'), 87 'ele': pt.findtext( 88 'AltitudeMeters')} ) 89 count += 1 90 self.template_data['points'] = points
91