source: subversion/applications/routing/pyroute/routeAsOsm.py @ 5939

Last change on this file since 5939 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 an
4# OSM file containing the result
5#
6#------------------------------------------------------
7# Usage:
8#  routeAsOsm.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 routeToOsm(nodeList, osmData, description="", transport=""):
32  """Format a route (as list of nodes) into an OSM file"""
33  output = ''
34  output = output + "<?xml version='1.0' encoding='UTF-8'?>\n";
35  output = output + "<osm version='0.5' generator='pyroute'>\n";
36 
37  for i in nodeList:
38    output = output + " <node id='%d' lat='%f' lon='%f' />\n" % ( \
39      i,
40      osmData.nodes[i][0],
41      osmData.nodes[i][1])
42
43  output = output + " <way id='1'>\n"
44  for i in nodeList:
45    output = output + "  <nd ref='%d' />\n" % i
46   
47  output = output + "  <tag k='route_display' v='yes' />\n"
48  output = output + "  <tag k='name' v='%s' />\n" % description
49  output = output + "  <tag k='transport' v='%s' />\n" % transport
50  output = output + " </way>\n"
51  output = output + "</osm>"
52 
53  return(output)
54 
55
56if __name__ == "__main__":
57  try:
58    # Load data
59    data = LoadOsm(sys.argv[1])
60 
61    # Do routing
62    router = Router(data)
63    result, route = router.doRoute(int(sys.argv[2]), int(sys.argv[3]), sys.argv[4])
64   
65    # Display result
66    if result == 'success':
67      print routeToOsm(route, data, sys.argv[5], sys.argv[4])
68    else:
69      sys.stderr.write("Failed (%s)\n" % result)
70     
71  except IndexError:
72    # Not enough argv[]s
73    sys.stderr.write("Usage: routeAsOsm.py [OSM file] [from node] [to node] [transport method] [description]\n")
Note: See TracBrowser for help on using the repository browser.