source: subversion/applications/routing/pyroute/mod_waypoints.py @ 5853

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

automatic numbering of waypoints

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