Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/addParkingAreaStops2Trips.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 addParkingAreaStops2Trips.py
15
# @author Evamarie Wiessner
16
# @date 2017-01-09
17
18
"""
19
add stops at parkingAreas to vehicle routes
20
"""
21
22
from __future__ import print_function
23
from __future__ import absolute_import
24
import os
25
import sys
26
from random import Random
27
28
# (seed)
29
30
if 'SUMO_HOME' in os.environ:
31
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
32
sys.path.append(tools)
33
else:
34
sys.exit("please declare environment variable 'SUMO_HOME'")
35
36
import sumolib # noqa
37
38
39
def get_options(args=None):
40
optParser = sumolib.options.ArgumentParser()
41
optParser.add_option("-r", "--route-file", category='input', dest="routefile", required=True,
42
help="define the input route file with trips")
43
optParser.add_option("-o", "--output-file", category='output', dest="outfile",
44
help="output route file with trips with parking stops")
45
optParser.add_option("-p", "--parking-areas", category='input', dest="parking", required=True,
46
help="define the parking areas separated by comma")
47
optParser.add_option("-d", "--parking-duration", dest="duration",
48
help="define the parking duration (in seconds)", default=3600)
49
optParser.add_option("-u", "--parking-until", dest="until",
50
help="define the parking until duration (in seconds)")
51
optParser.add_option("-l", "--parking-untilend", dest="untilend",
52
help="define the parking until end variable duration (in seconds)")
53
optParser.add_option("-b", "--parking-duration-begin", dest="durationBegin",
54
help="define the minimum parking duration (in seconds)")
55
optParser.add_option("-e", "--parking-duration-end", dest="durationEnd",
56
help="define the maximum parking duration (in seconds)")
57
optParser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
58
help="tell me what you are doing")
59
optParser.add_option("--random", action="store_true", default=False,
60
help="use a random seed to initialize the random number generator")
61
optParser.add_option("-s", "--seed", type=int, default=42,
62
help="random seed")
63
return optParser.parse_args(args=args)
64
65
66
def main(options):
67
R1 = Random()
68
R2 = Random()
69
if not options.random:
70
R1.seed(options.seed)
71
R2.seed(options.seed)
72
infile = options.routefile
73
# set default output file
74
if not options.outfile:
75
options.outfile = infile.replace(".xml", ".parking.xml")
76
# declare a list with parkings
77
parkings = []
78
# save all parkings in a list
79
for parking in sumolib.xml.parse(options.parking, "parkingArea"):
80
parkings.append(parking)
81
# open file
82
with open(options.outfile, 'w') as outf:
83
# write header
84
outf.write("<?xml version= \"1.0\" encoding=\"UTF-8\"?>\n\n")
85
# open route rag
86
outf.write("<routes>\n")
87
# iterate over trips
88
for trip in sumolib.xml.parse(infile, "trip", heterogeneous=True):
89
# obtain random parking
90
random_parking = R1.choice(parkings)
91
# add child depending of durations
92
if (options.durationBegin and options.durationEnd):
93
# obtain random duration
94
duration = R2.randint(int(options.durationBegin), int(options.durationEnd))
95
trip.addChild("stop", {"parkingArea": random_parking.id, "duration": duration})
96
elif options.until:
97
if options.untilend:
98
# obtain random duration
99
until = R2.randint(int(options.until), int(options.untilend))
100
trip.addChild("stop", {"parkingArea": random_parking.id, "until": until})
101
else:
102
trip.addChild("stop", {"parkingArea": random_parking.id, "until": options.until})
103
else:
104
trip.addChild("stop", {"parkingArea": random_parking.id, "duration": int(options.duration)})
105
# write trip
106
outf.write(trip.toXML(initialIndent=" "))
107
# close route tag
108
outf.write("</routes>\n")
109
110
111
if __name__ == "__main__":
112
options = get_options()
113
main(options)
114
115