source: subversion/applications/routing/pyroute-dev/lib_gpx.py @ 18454

Last change on this file since 18454 was 18454, checked in by buerste, 10 years ago

-further updates of spaces to tabs

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