Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/generateTLSE1Detectors.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 generateTLSE1Detectors.py
15
# @author Daniel Krajzewicz
16
# @author Karol Stosiek
17
# @author Michael Behrisch
18
# @date 2011-10-07
19
20
from __future__ import absolute_import
21
from __future__ import print_function
22
23
import logging
24
import optparse
25
import os
26
import sys
27
from generateTLSE2Detectors import adjust_detector_position
28
29
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
30
import sumolib # noqa
31
32
33
if __name__ == "__main__":
34
# pylint: disable-msg=C0103
35
36
logging.basicConfig(level="INFO")
37
38
option_parser = optparse.OptionParser()
39
option_parser.add_option("-n", "--net-file",
40
dest="net_file",
41
help="Network file to work with. Mandatory.",
42
type="string")
43
option_parser.add_option("-d", "--distance-to-TLS",
44
dest="requested_distance_to_tls",
45
help="Distance of the detector to the traffic "
46
"light in meters. Defaults to 0.1m.",
47
type="float",
48
default=.1)
49
option_parser.add_option("-f", "--frequency",
50
dest="frequency",
51
help="Detector's frequency. Defaults to 60.",
52
type="int",
53
default=60)
54
option_parser.add_option("-o", "--output",
55
dest="output",
56
help="The name of the file to write the detector "
57
"definitions into. Defaults to e1.add.xml.",
58
type="string",
59
default="e1.add.xml")
60
option_parser.add_option("-r", "--results-file",
61
dest="results",
62
help="The name of the file the detectors write "
63
"their output into. Defaults to e1output.xml.",
64
type="string",
65
default="e1output.xml")
66
option_parser.set_usage("generateTLSE1Detectors.py -n example.net.xml -d .1 -f 60")
67
68
(options, args) = option_parser.parse_args()
69
if not options.net_file:
70
print("Missing arguments")
71
option_parser.print_help()
72
exit()
73
74
logging.info("Reading net...")
75
net = sumolib.net.readNet(options.net_file)
76
77
logging.info("Generating detectors...")
78
detectors_xml = sumolib.xml.create_document("additional")
79
lanes_with_detectors = set()
80
for tls in net._tlss:
81
for connection in tls._connections:
82
lane = connection[0]
83
lane_length = lane.getLength()
84
lane_id = lane.getID()
85
86
logging.debug("Creating detector for lane %s" % (str(lane_id)))
87
88
if lane_id in lanes_with_detectors:
89
logging.warning("Detector for lane %s already generated" % lane_id)
90
continue
91
92
lanes_with_detectors.add(lane_id)
93
94
final_detector_position = adjust_detector_position(
95
0,
96
options.requested_distance_to_tls,
97
lane_length)
98
99
detector_xml = detectors_xml.addChild("e1Detector")
100
detector_xml.setAttribute("file", options.results)
101
detector_xml.setAttribute("freq", str(options.frequency))
102
detector_xml.setAttribute("friendlyPos", "x")
103
detector_xml.setAttribute("id", "e1det_" + str(lane_id))
104
detector_xml.setAttribute("lane", str(lane_id))
105
detector_xml.setAttribute("pos", str(final_detector_position))
106
107
detector_file = open(options.output, 'w')
108
detector_file.write(detectors_xml.toXML())
109
detector_file.close()
110
111
logging.info("%d e1 detectors generated!" % len(lanes_with_detectors))
112
113