1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 GPX file conversion handling.
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
33 """
34 Process a GPX 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 gpx 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
62 """
63 Get the point data from the GPX 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 segs = tree.findall('trk/trkseg')
76 points = []
77 for seg in segs:
78 pts = seg.findall('trkpt')
79 for pt in pts:
80 points.append( {'index': count,
81 'lat': pt.attrib['lat'],
82 'lon': pt.attrib['lon'],
83 'ele': pt.findtext('ele')} )
84 count += 1
85 self.template_data['points'] = points
86