source: subversion/applications/routing/pyroute-dev/routeAsOsm.py @ 18454

Last change on this file since 18454 was 18454, checked in by buerste, 10 years ago

-further updates of spaces to tabs

  • Property svn:keywords set to Rev
File size: 2.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""routeAsOsm - routes within an OSM file, and generates an
5OSM file containing the result
6
7Usage:
8        routeAsOsm.py [input OSM file] [start node] [end node] [transport] [description]
9"""
10
11__version__ = "$Rev: 18454 $"
12__license__ = """This program is free software: you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program. If not, see <http://www.gnu.org/licenses/>."""
24_debug = 0
25
26
27from route import *
28
29def routeToOsm(nodeList, osmData, description="", transport=""):
30        """Format a route (as list of nodes) into an OSM file"""
31        output = ''
32        output = output + "<?xml version='1.0' encoding='UTF-8'?>\n";
33        output = output + "<osm version='0.5' generator='pyroute'>\n";
34
35        for i in nodeList:
36                output = output + " <node id='%d' lat='%f' lon='%f' />\n" % ( \
37                        i,
38                        osmData.nodes[i][0],
39                        osmData.nodes[i][1])
40
41        output = output + " <way id='1'>\n"
42        for i in nodeList:
43                output = output + "     <nd ref='%d' />\n" % i
44
45        output = output + "  <tag k='route_display' v='yes' />\n"
46        output = output + "  <tag k='name' v='%s' />\n" % description
47        output = output + "  <tag k='transport' v='%s' />\n" % transport
48        output = output + " </way>\n"
49        output = output + "</osm>"
50
51        return(output)
52
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 routeToOsm(route, data, sys.argv[5], sys.argv[4])
66                else:
67                        sys.stderr.write("Failed (%s)\n" % result)
68
69        except IndexError:
70                # Not enough argv[]s
71                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.