Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/generateTLSE2Detectors.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-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 generateTLSE2Detectors.py
15
# @author Daniel Krajzewicz
16
# @author Karol Stosiek
17
# @author Lena Kalleske
18
# @author Michael Behrisch
19
# @date 2007-10-25
20
21
from __future__ import absolute_import
22
from __future__ import print_function
23
24
import logging
25
import os
26
import sys
27
28
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
29
import sumolib # noqa
30
31
32
def adjust_detector_length(requested_detector_length,
33
requested_distance_to_tls,
34
lane_length):
35
""" Adjusts requested detector's length according to
36
the lane length and requested distance to TLS.
37
38
If requested detector length is negative, the resulting detector length
39
will match the distance between requested distance to TLS and lane
40
beginning.
41
42
43
If the requested detector length is positive, it will be adjusted
44
according to the end of lane ending with TLS: the resulting length
45
will be either the requested detector length or, if it's too long
46
to be placed in requested distance from TLS, it will be shortened to
47
match the distance between requested distance to TLS
48
and lane beginning. """
49
50
if requested_detector_length == -1:
51
return lane_length - requested_distance_to_tls
52
53
return min(lane_length - requested_distance_to_tls,
54
requested_detector_length)
55
56
57
def adjust_detector_position(final_detector_length,
58
requested_distance_to_tls,
59
lane_length):
60
""" Adjusts the detector's position. If the detector's length
61
and the requested distance to TLS together are longer than
62
the lane itself, the position will be 0; it will be
63
the maximum distance from lane end otherwise (taking detector's length
64
and requested distance to TLS into accout). """
65
66
return max(0,
67
lane_length - final_detector_length - requested_distance_to_tls)
68
69
70
if __name__ == "__main__":
71
# pylint: disable-msg=C0103
72
73
logging.basicConfig(level="INFO")
74
75
usage = "generateTLSE2Detectors.py -n example.net.xml -l 250 -d .1 -f 60"
76
argParser = sumolib.options.ArgumentParser(usage=usage)
77
argParser.add_argument("-n", "--net-file",
78
dest="net_file",
79
help="Network file to work with. Mandatory.")
80
argParser.add_argument("-l", "--detector-length",
81
dest="requested_detector_length",
82
help="Length of the detector in meters "
83
"(-1 for maximal length).",
84
type=int,
85
default=250)
86
argParser.add_argument("-d", "--distance-to-TLS",
87
dest="requested_distance_to_tls",
88
help="Distance of the detector to the traffic "
89
"light in meters. Defaults to 0.1m.",
90
type=float,
91
default=.1)
92
argParser.add_argument("-f", "--frequency",
93
dest="frequency",
94
help="Detector's frequency. Defaults to 60.",
95
type=int,
96
default=60)
97
argParser.add_argument("-o", "--output",
98
dest="output",
99
help="The name of the file to write the detector "
100
"definitions into. Defaults to e2.add.xml.",
101
default="e2.add.xml")
102
argParser.add_argument("-r", "--results-file",
103
dest="results",
104
help="The name of the file the detectors write "
105
"their output into. Defaults to e2output.xml.",
106
default="e2output.xml")
107
argParser.add_argument("--tl-coupled", action="store_true",
108
dest="tlCoupled",
109
default=False,
110
help="Couple output frequency to traffic light phase")
111
112
options = argParser.parse_args()
113
if not options.net_file:
114
print("Missing arguments")
115
argParser.print_help()
116
exit()
117
118
logging.info("Reading net...")
119
net = sumolib.net.readNet(options.net_file)
120
121
logging.info("Generating detectors...")
122
detectors_xml = sumolib.xml.create_document("additional")
123
lanes_with_detectors = set()
124
for tls in net._tlss:
125
for connection in tls._connections:
126
lane = connection[0]
127
lane_length = lane.getLength()
128
lane_id = lane.getID()
129
130
logging.debug("Creating detector for lane %s" % (str(lane_id)))
131
132
if lane_id in lanes_with_detectors:
133
logging.warning("Detector for lane %s already generated" % lane_id)
134
continue
135
136
lanes_with_detectors.add(lane_id)
137
138
final_detector_length = adjust_detector_length(
139
options.requested_detector_length,
140
options.requested_distance_to_tls,
141
lane_length)
142
final_detector_position = adjust_detector_position(
143
final_detector_length,
144
options.requested_distance_to_tls,
145
lane_length)
146
147
detector_xml = detectors_xml.addChild("laneAreaDetector")
148
detector_xml.setAttribute("file", options.results)
149
if options.tlCoupled:
150
detector_xml.setAttribute("tl", tls.getID())
151
else:
152
detector_xml.setAttribute("freq", str(options.frequency))
153
detector_xml.setAttribute("friendlyPos", "x")
154
detector_xml.setAttribute("id", "e2det_" + str(lane_id))
155
detector_xml.setAttribute("lane", str(lane_id))
156
detector_xml.setAttribute("length", str(final_detector_length))
157
detector_xml.setAttribute("pos", str(final_detector_position))
158
159
detector_file = open(options.output, 'w')
160
detector_file.write(detectors_xml.toXML())
161
detector_file.close()
162
163
logging.info("%d e2 detectors generated!" % len(lanes_with_detectors))
164
165