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

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

Module for doing routing, and formatting the output as an OSM XML file

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