source: subversion/applications/routing/pyroute-dev/poi_waypoints.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: 3.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""Waypoint handler for pyroute
5"""
6
7
8__version__ = "$Rev: 18454 $"
9__license__ = """This program is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program. If not, see <http://www.gnu.org/licenses/>."""
21_debug = 0
22
23
24from xml.sax import make_parser, handler
25from poi_base import *
26import os
27
28class waypointsModule(poiModule, handler.ContentHandler):
29        def __init__(self, modules, filename = None):
30                poiModule.__init__(self, modules)
31                self.storeFields = ('name','sym')
32                self.groups.append(poiGroup("Waypoints"))
33                self.filename = None
34                self.nextNumericWpt = 1
35                if(filename):
36                        if(os.path.exists(filename)):
37                                self.load(filename)
38                                self.filename = filename
39
40        def load(self, filename):
41                self.inField = False
42                parser = make_parser()
43                parser.setContentHandler(self)
44                parser.parse(filename)
45
46        def startElement(self, name, attrs):
47                if name == "wpt":
48                        self.parsedWaypoint = { \
49                                'lat': float(attrs.get('lat')),
50                                'lon': float(attrs.get('lon'))}
51                if name in self.storeFields:
52                        self.fieldText = ''
53                        self.inField = True
54
55        def endElement(self, name):
56                if self.inField:
57                        if name in self.storeFields:
58                                self.parsedWaypoint[name] = self.fieldText
59                        self.inField = False
60                if(name == "wpt"):
61                        self.storeWaypoint(self.parsedWaypoint)
62
63        def characters(self, text):
64                if self.inField:
65                        self.fieldText = self.fieldText + text
66
67        def storeWaypoint(self, waypoint):
68                x = poi(waypoint['lat'], waypoint['lon'])
69                x.title = waypoint['name']
70                self.groups[0].items.append(x)
71                try:
72                        if(int(x.title) >= self.nextNumericWpt):
73                                self.nextNumericWpt = int(x.title) + 1
74                except ValueError:
75                        pass
76
77        def storeNumberedWaypoint(self, waypoint):
78                waypoint['name'] = self.nextNumber()
79                self.storeWaypoint(waypoint)
80
81        def save(self):
82                # Default filename if none was loaded
83                if(self.filename == None):
84                        self.filename = os.path.join(os.path.dirname(__file__), "data", "waypoints.gpx")
85                self.saveAs(self.filename)
86
87        def saveAs(self,filename):
88                if(filename == None):
89                        return
90                f = open(filename, "w")
91                f.write('<?xml version="1.0"?>\n')
92                f.write('<gpx version="1.0" creator="pyroute">\n')
93                for w in self.groups[0].items:
94                        f.write('<wpt lat="%f" lon="%f">\n' % (w.lat, w.lon))
95                        f.write('  <name>%s</name>\n' % w.title)
96                        #f.write('  <cmt>%s</cmt>\n' % w.title)
97                        #f.write('  <desc>%s</desc>\n' % w.title)
98                        f.write('  <sym>%s</sym>\n' % 'flag')
99                        f.write('</wpt>\n')
100                f.write('</gpx>\n')
101                f.close()
102
103        def nextNumber(self):
104                return(str(self.nextNumericWpt))
105
106if __name__ == "__main__":
107        wpt = waypointsModule(None,"data/waypoints.gpx")
108        wpt.report()
109        wpt.saveAs("data/new.gpx")
Note: See TracBrowser for help on using the repository browser.