Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/patchNetFiles.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-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 patchNetFiles.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2008-08-13
18
19
"""
20
Reads in a file where junctions controlled by
21
tls are stored and one which contains correct
22
lane numbers. Applies the information on
23
the given edge and node file and saves them.
24
25
todo:
26
- make this read XML-files using an XML-API
27
"""
28
from __future__ import absolute_import
29
from __future__ import print_function
30
31
32
from optparse import OptionParser
33
34
35
def getAttr(source, what):
36
mbeg = source.find(what + "=")
37
mbeg = source.find('"', mbeg) + 1
38
mend = source.find('"', mbeg)
39
return source[mbeg:mend]
40
41
42
optParser = OptionParser()
43
optParser.add_option("-v", "--verbose", action="store_true", dest="verbose",
44
default=False, help="tell me what you are doing")
45
optParser.add_option("-t", "--tls-file", dest="lsas",
46
help="File with tls nodes", metavar="FILE")
47
optParser.add_option("-l", "--lanes-file", dest="lanes",
48
help="File with lane number patched", metavar="FILE")
49
optParser.add_option("-e", "--edges-file", dest="edges",
50
help="File with XML-edges", metavar="FILE")
51
optParser.add_option("-n", "--nodes-file", dest="nodes",
52
help="File with XML-nodes", metavar="FILE")
53
(options, args) = optParser.parse_args()
54
55
56
# read in lsa definitions
57
lsas = {}
58
if options.lsas:
59
index = 0
60
fd = open(options.lsas)
61
for line in fd:
62
line = line.strip()
63
if line == "" or line[0] == '#':
64
continue
65
(id, tls) = line.split(":")
66
tls = tls.split(",")
67
if id == "tls":
68
if len(tls) > 1:
69
id = "j_" + str(index)
70
index = index + 1
71
else:
72
id = tls[0]
73
for t in tls:
74
if t in lsas:
75
print("Junction's '" + t + "' TLS already defined")
76
lsas[t] = id
77
fd.close()
78
# read in lane number patches
79
lanes = {}
80
if options.lanes:
81
fd = open(options.lanes)
82
for line in fd:
83
line = line.strip()
84
if line == "" or line[0] == '#':
85
continue
86
(edge, laneNo) = line.split(":")
87
if edge in lanes:
88
print("Edge's '" + edge + "' lane number already defined")
89
lanes[edge] = int(laneNo)
90
fd.close()
91
92
# patch
93
# edges
94
fdi = open(options.edges)
95
fdo = open(options.edges + ".new.xml", "w")
96
for line in fdi:
97
if line.find("<edge ") >= 0:
98
id = getAttr(line, "id")
99
if id in lanes:
100
indexB = line.find("numLanes")
101
indexB = line.find('"', indexB) + 1
102
indexE = line.find('"', indexB)
103
line = line[:indexB] + str(lanes[id]) + line[indexE:]
104
fdo.write(line)
105
fdo.close()
106
fdi.close()
107
# nodes
108
fdi = open(options.nodes)
109
fdo = open(options.nodes + ".new.xml", "w")
110
for line in fdi:
111
if line.find("<node ") >= 0:
112
id = getAttr(line, "id")
113
if id in lsas:
114
indexE = line.find("/>")
115
line = line[:indexE] + \
116
" type=\"traffic_light\" tl=\"" + lsas[id] + "\"/>"
117
fdo.write(line)
118
fdo.close()
119
fdi.close()
120
121