Path: blob/main/tools/route/addParkingAreaStops2Trips.py
169674 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2010-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file addParkingAreaStops2Trips.py14# @author Evamarie Wiessner15# @date 2017-01-091617"""18add stops at parkingAreas to vehicle routes19"""2021from __future__ import print_function22from __future__ import absolute_import23import os24import sys25from random import Random2627# (seed)2829if 'SUMO_HOME' in os.environ:30tools = os.path.join(os.environ['SUMO_HOME'], 'tools')31sys.path.append(tools)32else:33sys.exit("please declare environment variable 'SUMO_HOME'")3435import sumolib # noqa363738def get_options(args=None):39optParser = sumolib.options.ArgumentParser()40optParser.add_option("-r", "--route-file", category='input', dest="routefile", required=True,41help="define the input route file with trips")42optParser.add_option("-o", "--output-file", category='output', dest="outfile",43help="output route file with trips with parking stops")44optParser.add_option("-p", "--parking-areas", category='input', dest="parking", required=True,45help="define the parking areas separated by comma")46optParser.add_option("-d", "--parking-duration", dest="duration",47help="define the parking duration (in seconds)", default=3600)48optParser.add_option("-u", "--parking-until", dest="until",49help="define the parking until duration (in seconds)")50optParser.add_option("-l", "--parking-untilend", dest="untilend",51help="define the parking until end variable duration (in seconds)")52optParser.add_option("-b", "--parking-duration-begin", dest="durationBegin",53help="define the minimum parking duration (in seconds)")54optParser.add_option("-e", "--parking-duration-end", dest="durationEnd",55help="define the maximum parking duration (in seconds)")56optParser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,57help="tell me what you are doing")58optParser.add_option("--random", action="store_true", default=False,59help="use a random seed to initialize the random number generator")60optParser.add_option("-s", "--seed", type=int, default=42,61help="random seed")62return optParser.parse_args(args=args)636465def main(options):66R1 = Random()67R2 = Random()68if not options.random:69R1.seed(options.seed)70R2.seed(options.seed)71infile = options.routefile72# set default output file73if not options.outfile:74options.outfile = infile.replace(".xml", ".parking.xml")75# declare a list with parkings76parkings = []77# save all parkings in a list78for parking in sumolib.xml.parse(options.parking, "parkingArea"):79parkings.append(parking)80# open file81with open(options.outfile, 'w') as outf:82# write header83outf.write("<?xml version= \"1.0\" encoding=\"UTF-8\"?>\n\n")84# open route rag85outf.write("<routes>\n")86# iterate over trips87for trip in sumolib.xml.parse(infile, "trip", heterogeneous=True):88# obtain random parking89random_parking = R1.choice(parkings)90# add child depending of durations91if (options.durationBegin and options.durationEnd):92# obtain random duration93duration = R2.randint(int(options.durationBegin), int(options.durationEnd))94trip.addChild("stop", {"parkingArea": random_parking.id, "duration": duration})95elif options.until:96if options.untilend:97# obtain random duration98until = R2.randint(int(options.until), int(options.untilend))99trip.addChild("stop", {"parkingArea": random_parking.id, "until": until})100else:101trip.addChild("stop", {"parkingArea": random_parking.id, "until": options.until})102else:103trip.addChild("stop", {"parkingArea": random_parking.id, "duration": int(options.duration)})104# write trip105outf.write(trip.toXML(initialIndent=" "))106# close route tag107outf.write("</routes>\n")108109110if __name__ == "__main__":111options = get_options()112main(options)113114115