Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/computeStoppingPlaceUsage.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2010-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 computeStoppingPlaceUsage.py
15
# @author Jakob Erdmann
16
# @date 2021-03-23
17
18
19
from __future__ import print_function
20
from __future__ import absolute_import
21
import os
22
import sys
23
from collections import defaultdict
24
25
if 'SUMO_HOME' in os.environ:
26
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
27
import sumolib # noqa
28
from sumolib.miscutils import parseTime # noqa
29
30
31
def get_options(args=None):
32
parser = sumolib.options.ArgumentParser(description="Compute Stopping Place usage")
33
parser.add_argument("-s", "--stop-output-file", dest="stopOutput",
34
help="simulation stop-output file")
35
parser.add_argument("-t", "--stopping-place", dest="stoppingPlace",
36
help="stoppingPlace type (busStop, parkingArea...)", default="parkingArea")
37
parser.add_argument("--csv", action="store_true", default=False,
38
help="write in CSV format")
39
parser.add_argument("--only-changes", action="store_true", default=False, dest="onlyChanges",
40
help="write output only for steps where the occupancy changes")
41
parser.add_argument("-b", "--begin", default=None, help="begin time (when writting all steps)")
42
parser.add_argument("-e", "--end", default=None, help="end time (when writting all steps)")
43
options = parser.parse_args(args=args)
44
if not options.stopOutput:
45
parser.print_help()
46
sys.exit()
47
48
if options.begin:
49
options.begin = int(parseTime(options.begin))
50
if options.end:
51
options.end = int(parseTime(options.end))
52
return options
53
54
55
def main(options):
56
# declare veh counts
57
vehCounts = defaultdict(list)
58
59
time = None
60
for stop in sumolib.xml.parse(options.stopOutput, "stopinfo"):
61
splace = stop.getAttributeSecure(options.stoppingPlace, "")
62
if splace != "":
63
vehCounts[splace].append((parseTime(stop.started), 1))
64
vehCounts[splace].append((parseTime(stop.ended), -1))
65
# iterate over vehCounts
66
for splace, times in vehCounts.items():
67
times.sort()
68
steps = []
69
tPrev = None
70
count = 0
71
for t, change in times:
72
if t != tPrev and tPrev is not None:
73
steps.append((tPrev, count))
74
count += change
75
tPrev = t
76
steps.append((tPrev, count))
77
78
if not options.onlyChanges:
79
# fill missing steps
80
steps2 = []
81
index = 0
82
number = 0
83
if options.begin is not None:
84
time = options.begin
85
# skip steps before begin
86
while steps[index][0] < time and index < len(steps):
87
index += 1
88
else:
89
time = int(steps[0][0])
90
91
abort = False
92
while index < len(steps):
93
# fill gaps between steps
94
newTime, newNumber = steps[index]
95
for t in range(time, int(newTime)):
96
steps2.append((float(t), number))
97
if options.end is not None and t == options.end:
98
abort = True
99
break
100
if abort:
101
break
102
steps2.append((newTime, newNumber))
103
if options.end is not None and newTime == options.end:
104
break
105
number = newNumber
106
time = int(newTime) + 1
107
index += 1
108
109
steps = steps2
110
111
suffix = ".csv" if options.csv else ".xml"
112
with open(splace + suffix, "w") as outf:
113
# write header
114
if options.csv:
115
# write CSV header
116
outf.write("step,number\n")
117
for time, number in steps:
118
outf.write("%s,%s\n" % (time, number))
119
else:
120
# write XML header
121
outf.write("<?xml version= \"1.0\" encoding=\"UTF-8\"?>\n\n")
122
# open route rag
123
outf.write("<stoppingPlace>\n")
124
for time, number in steps:
125
outf.write(' <step time="%s" number="%s"/>\n' % (time, number))
126
outf.write("</stoppingPlace>\n")
127
128
129
if __name__ == "__main__":
130
main(get_options())
131
132