Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/tracegenerator.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2013-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file tracegenerator.py
15
# @author Michael Behrisch
16
# @date 2013-10-23
17
18
19
from __future__ import print_function
20
from __future__ import absolute_import
21
import os
22
import sys
23
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
24
25
import sumolib # noqa
26
27
28
def generateTrace(route, step, x=0., y=0.):
29
trace = []
30
for edge in route:
31
numSteps = int(edge.getLength() / step)
32
for p in range(numSteps):
33
pos = sumolib.geomhelper.positionAtShapeOffset(edge.getShape(), p * step)
34
trace.append((pos[0] + x, pos[1] + y))
35
return trace
36
37
38
if __name__ == "__main__":
39
ap = sumolib.options.ArgumentParser()
40
ap.add_argument("-v", "--verbose", action="store_true",
41
default=False, help="tell me what you are doing")
42
ap.add_argument("-n", "--net", category="input", type=ap.net_file,
43
required=True, help="SUMO network to use (mandatory)", metavar="FILE")
44
ap.add_argument("-2", "--net2", category="input", type=ap.net_file,
45
help="immediately match routes to a second network", metavar="FILE")
46
ap.add_argument("-r", "--routes", category="input", type=ap.route_file,
47
required=True, help="route file to use (mandatory)", metavar="FILE")
48
ap.add_argument("-s", "--step", default="10",
49
type=float, help="distance between successive trace points")
50
ap.add_argument("-d", "--delta", default="1", type=float,
51
help="maximum distance between edge and trace points when matching to the second net")
52
ap.add_argument("-x", "--x-offset", default=0.,
53
type=float, help="offset to add to traces")
54
ap.add_argument("-y", "--y-offset", default=0.,
55
type=float, help="offset to add to traces")
56
ap.add_argument("-o", "--output", category="output", type=ap.file,
57
required=True, help="trace or route output (mandatory)", metavar="FILE")
58
options = ap.parse_args()
59
60
if options.verbose:
61
print("Reading net ...")
62
net = sumolib.net.readNet(options.net)
63
net2 = None
64
if options.net2:
65
net.move(-net.getLocationOffset()[0], -net.getLocationOffset()[1])
66
net2 = sumolib.net.readNet(options.net2)
67
net2.move(-net2.getLocationOffset()[0], -net2.getLocationOffset()[1])
68
69
if options.verbose:
70
print("Reading routes ...")
71
72
f = open(options.output, "w")
73
for route in sumolib.output.parse_fast(options.routes, "route", ["id", "edges"]):
74
edges = [net.getEdge(e) for e in route.edges.split()]
75
trace = generateTrace(edges, options.step, options.x_offset, options.y_offset)
76
if net2:
77
path = sumolib.route.mapTrace(trace, net2, options.delta)
78
if not path or path == ["*"]:
79
print("No match for", route.id)
80
print(route.id, path, file=f)
81
else:
82
print("%s:%s" %
83
(route.id, " ".join(["%s,%s" % p for p in trace])), file=f)
84
f.close()
85
86