source: subversion/applications/routing/pyroute/routeAsGpx.py @ 8025

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

Specify the form of transport (cycle, car, foot, horse, etc) when asking
for a route

File size: 2.6 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# routeAsOsm - routes within an OSM file, and generates a
4# GPX file containing the result
5#
6#------------------------------------------------------
7# Usage:
8#  routeAsGpx.py [input OSM file] [start node] [end node] [transport] [description]
9#------------------------------------------------------
10# Copyright 2007, 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#------------------------------------------------------
25# Changelog:
26#  2007-11-04  OJW  Created
27#  2007-11-05  OJW  Multiple forms of transport
28#------------------------------------------------------
29from route import *
30 
31def routeToGpx(nodeList, osmData, description=""):
32  """Format a route (as list of nodes) into a GPX file"""
33 
34  output = ''
35  output = output + "<?xml version='1.0'?>\n";
36 
37  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"
38   
39  output = output + " <rte>\n"
40  output = output + "  <name>%s</name>\n" % description
41 
42  count = 0;
43  for i in nodeList:
44    output = output + "   <rtept lat='%f' lon='%f'>\n" % ( \
45      osmData.nodes[i][0],
46      osmData.nodes[i][1])
47    output = output + "    <name>%d</name>\n" % count
48    output = output + "   </rtept>\n"
49    count = count + 1
50  output = output + " </rte>\n</gpx>\n"
51 
52  return(output)
53
54if __name__ == "__main__":
55  try:
56    # Load data
57    data = LoadOsm(sys.argv[1])
58 
59    # Do routing
60    router = Router(data)
61    result, route = router.doRoute(int(sys.argv[2]), int(sys.argv[3]), sys.argv[4])
62 
63    # Display result
64    if result == 'success':
65      print routeToGpx(route, data, sys.argv[5])
66    else:
67      sys.stderr.write("Failed (%s)" % result)
68 
69  except IndexError:
70    # Not enough argv[]s
71    sys.stderr.write("Usage: routeAsGpx.py [OSM file] [from node] [to node] [transport method] [description]\n")
Note: See TracBrowser for help on using the repository browser.