source: subversion/applications/routing/pyroutelib2/routeAsGpx.py @ 8032

Revision 8032, 2.9 KB checked in by ojw, 6 years ago (diff)

update header

Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# routeAsOsm - routes with OSM data, and generates a
4# GPX file containing the result
5#
6#------------------------------------------------------
7# Usage:
8#  routeAsGpx.py [lat1] [lon1] [lat2] [lon2] [transport]
9#------------------------------------------------------
10# Copyright 2007-2008, Oliver White
11#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24#------------------------------------------------------
25from route import *
26 
27def routeToGpx(lat1,lon1,lat2,lon2, transport, description="", style="track"):
28  """Format a route (as list of nodes) into a GPX file"""
29  data = LoadOsm(transport)
30
31  node1 = data.findNode(lat1,lon1)
32  node2 = data.findNode(lat2,lon2)
33
34  router = Router(data)
35  result, route = router.doRoute(node1, node2)
36  if result != 'success':
37    return
38
39  output = ''
40  output = output + "<?xml version='1.0'?>\n";
41 
42  output = output + "<gpx version='1.1' creator='pyroute' xmlns='http://www.topografix.com/GPX/1/1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'>\n"
43   
44  if(style == 'track'):
45    output = output + " <trk>\n"
46    output = output + "  <name>%s</name>\n" % description
47    output = output + "  <trkseg>\n"
48    count = 0;
49    for i in route:
50      node = data.rnodes[i]
51      output = output + "   <trkpt lat='%f' lon='%f'>\n" % ( \
52        node[0],
53        node[1])
54      output = output + "   </trkpt>\n"
55      count = count + 1
56    output = output + "  </trkseg>\n  </trk>\n</gpx>\n"
57
58  elif(style == 'route'):
59    output = output + " <rte>\n"
60    output = output + "  <name>%s</name>\n" % description
61   
62    count = 0;
63    for i in route:
64      node = data.rnodes[i]
65      output = output + "   <rtept lat='%f' lon='%f'>\n" % ( \
66        node[0],
67        node[1])
68      output = output + "    <name>%d</name>\n" % count
69      output = output + "   </rtept>\n"
70      count = count + 1
71    output = output + " </rte>\n</gpx>\n"
72 
73  return(output)
74
75if __name__ == "__main__":
76 
77  try:
78    print routeToGpx(
79      float(sys.argv[1]),
80      float(sys.argv[2]),
81      float(sys.argv[3]),
82      float(sys.argv[4]),
83      sys.argv[5],
84      "Route",
85      "track")
86  except IndexError:
87    sys.stderr.write("Usage: routeAsGpx.py lat1 lon1 lat2 lon2 transport\n")
Note: See TracBrowser for help on using the repository browser.