source: subversion/applications/routing/pyroute/lib_gpx.py @ 5894

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

save tracklogs

File size: 2.4 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Library for handling GPX files
4#
5# Usage:
6#
7#-----------------------------------------------------------------------------
8# Copyright 2007, Oliver White
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#-----------------------------------------------------------------------------
23import os
24import cairo
25from xml.sax import make_parser, handler
26
27class lib_gpx(handler.ContentHandler):
28  """  """
29  def __init__(self):
30    self.lines = []
31
32  def draw(self,cr,proj):
33    cr.set_line_cap(cairo.LINE_CAP_ROUND)
34    cr.set_source_rgb(0.7,0.7,0)
35    cr.set_line_width(5)
36
37    for l in self.lines:
38      first = 1
39      for p in l:
40        x,y = proj.ll2xy(p[0], p[1])
41        if(first):
42          cr.move_to(x,y)
43          first = 0
44        else:
45              cr.line_to(x,y)
46      cr.stroke()
47     
48  def saveAs(self,filename, title="untitled"):
49                file = open(filename,"w")
50                file.write("<?xml version=\"1.0\"?>\n")
51                file.write('<gpx>\n')
52                file.write('<trk>\n')
53                file.write('<name>%s</name>\n' % title)
54                for l in self.lines:
55                        file.write('<trkseg>\n')
56                        for p in l:
57                                file.write('<trkpt lat="%f" lon="%f"/>\n'%(p[0], p[1]))
58                        file.write('</trkseg>\n')
59                file.write('</trk>\n')
60                file.write('</gpx>\n')
61
62                file.close()
63
64  def load(self, filename):
65    if(not os.path.exists(filename)):
66      print "No such tracklog \"%s\"" % filename
67      return
68    self.inField = False
69    parser = make_parser()
70    parser.setContentHandler(self)
71    parser.parse(filename)
72 
73  def startElement(self, name, attrs):
74    if name == 'trk':
75      pass
76    if name == 'trkseg':
77      self.latest = []
78      self.lines.append(self.latest)
79      pass
80    if name == "trkpt":
81                        lat = float(attrs.get('lat'))
82                        lon = float(attrs.get('lon'))
83                        self.latest.append([lat,lon])
Note: See TracBrowser for help on using the repository browser.