Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/generateBidiDistricts.py
169660 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2012-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 generateBidiDistricts.py
15
# @author Jakob Erdmann
16
# @date 2015-07-31
17
18
"""
19
Generate a taz (district) file which groups edges in opposite directions
20
belonging to the same road. For each edge, a taz is created which contains this edge
21
and its opposite.
22
This allows routing without the need for an
23
initial/final turn-around by replacing the attribute names 'from' and 'to' with
24
'fromTaz' and 'toTaz'
25
"""
26
from __future__ import absolute_import
27
from functools import reduce
28
29
import sumolib # noqa
30
31
32
def parse_args():
33
arg_parser = sumolib.options.ArgumentParser()
34
arg_parser.add_argument("-o", "--outfile", category="output", help="name of output file")
35
arg_parser.add_argument("-r", "--radius", category="processing", type=float, default=10.,
36
help="maximum air distance around the edge")
37
arg_parser.add_argument("-t", "--travel-distance", category="processing", type=float,
38
help="maximum travel distance in the graph")
39
arg_parser.add_argument("--symmetrical", action="store_true", default=False,
40
help="extend the bidi-relationship to be symmetrical")
41
arg_parser.add_argument("net", category="input", help="SUMO network file")
42
options = arg_parser.parse_args()
43
if options.outfile is None:
44
options.outfile = options.net + ".taz.xml"
45
return options
46
47
48
def getCandidates(edge, net, radius):
49
candidates = []
50
for x, y in edge.getShape():
51
nearby = set()
52
for edge2, dist in net.getNeighboringEdges(x, y, radius):
53
nearby.add(edge2)
54
candidates.append(nearby)
55
return candidates
56
57
58
ASYM_BIDI_CACHE = {} # edge : opposites
59
60
61
def computeBidiTazAsymByRadius(edge, net, radius):
62
if edge not in ASYM_BIDI_CACHE:
63
candidates = getCandidates(edge, net, radius)
64
opposites = reduce(lambda a, b: a.intersection(b), candidates)
65
opposites.update(set(edge.getToNode().getOutgoing()).intersection(
66
set(edge.getFromNode().getIncoming())))
67
ASYM_BIDI_CACHE[edge] = opposites
68
return ASYM_BIDI_CACHE[edge]
69
70
71
def computeAllBidiTaz(net, radius, travelDist, symmetrical):
72
for edge in net.getEdges():
73
travelOpposites = set()
74
if travelDist is not None:
75
queue = [(edge, -1.)]
76
while not len(queue) == 0:
77
edge2, dist = queue.pop()
78
if edge2 not in travelOpposites and dist < travelDist:
79
travelOpposites.add(edge2)
80
if dist == -1.:
81
dist = 0.
82
else:
83
dist += edge2.getLength()
84
toN = edge2.getToNode()
85
fromN = edge2.getFromNode()
86
for e in toN.getOutgoing() + toN.getIncoming() + fromN.getOutgoing() + fromN.getIncoming():
87
queue.append((e, dist))
88
if radius is not None and radius > 0.:
89
opposites = computeBidiTazAsymByRadius(edge, net, radius)
90
if symmetrical:
91
candidates = reduce(
92
lambda a, b: a.union(b), getCandidates(edge, net, radius))
93
for cand in candidates:
94
if edge in computeBidiTazAsymByRadius(cand, net, radius):
95
opposites.add(cand)
96
travelOpposites.update(opposites)
97
98
yield edge, travelOpposites
99
100
101
def main(netFile, outFile, radius, travelDist, symmetrical):
102
net = sumolib.net.readNet(netFile, withConnections=False, withFoes=False)
103
with sumolib.openz(outFile, mode='w') as outf:
104
sumolib.writeXMLHeader(outf)
105
outf.write(u'<tazs>\n')
106
for taz, edges in computeAllBidiTaz(net, radius, travelDist, symmetrical):
107
outf.write(u' <taz id="%s" edges="%s"/>\n' % (
108
taz.getID(), ' '.join(sorted([e.getID() for e in edges]))))
109
outf.write(u'</tazs>\n')
110
return net
111
112
113
if __name__ == "__main__":
114
opts = parse_args()
115
main(opts.net, opts.outfile, opts.radius, opts.travel_distance, opts.symmetrical)
116
117