Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/vehroute2amitranOD.py
169674 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2014-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file vehroute2amitranOD.py
16
# @author Michael Behrisch
17
# @author Mirko Barthauer
18
# @date 2014-04-08
19
20
from __future__ import absolute_import
21
import os
22
import sys
23
from collections import defaultdict
24
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..'))
25
import sumolib # noqa
26
from sumolib.options import ArgumentParser # noqa
27
28
29
def convert(vehRoutes, routeOut, odOut, interval):
30
routeDict = {}
31
actorConfig = defaultdict(list) # map type -> list of time slices
32
with open(routeOut, 'w') as routes:
33
routes.write("<routes>\n")
34
for v in sumolib.output.parse(vehRoutes, 'vehicle'):
35
depart = float(v.depart)
36
travelTime = float(v.arrival) - depart
37
if hasattr(v, "routeDistribution"):
38
edges = v.routeDistribution[0].route[-1].edges
39
else:
40
edges = v.route[0].edges
41
ac = getattr(v, "type", "DEFAULT_VEHTYPE")
42
if edges not in routeDict:
43
idx = len(routeDict)
44
routeDict[edges] = idx
45
routes.write(' <route id="%s">\n' % idx)
46
for e in edges.split():
47
routes.write(' <link id="%s"/>\n' % e)
48
routes.write(' </route>\n')
49
else:
50
idx = routeDict[edges]
51
listPos = int(depart / interval)
52
while len(actorConfig[ac]) <= listPos:
53
actorConfig[ac].append(defaultdict(dict))
54
# map (origin, dest) -> map route -> (amount, travel time sum)
55
od = actorConfig[ac][listPos]
56
key = (v.fromTaz, v.toTaz)
57
if idx in od[key]:
58
oldValue = od[key][idx]
59
value = (oldValue[0] + 1, oldValue[1] + travelTime)
60
else:
61
value = (1, travelTime)
62
od[key][idx] = value
63
routes.write("</routes>\n")
64
with open(odOut, 'w') as od:
65
od.write("<demand>\n")
66
for ac, odList in actorConfig.items():
67
od.write(' <actorConfig id="%s">\n' % ac)
68
for idx, odMap in enumerate(odList):
69
if odMap:
70
od.write(' <timeSlice startTime="%s" duration="%s">\n' % (
71
idx * interval * 1000, interval * 1000))
72
for (orig, dest), routeMap in odMap.items():
73
total = 0
74
for amount, _ in routeMap.values():
75
total += amount
76
od.write(' <odPair origin="%s" destination="%s" amount="%s">\n' % (
77
orig, dest, total))
78
for idx, (amount, ttSum) in routeMap.items():
79
od.write((' <routeCost routeId="%s" amount="%s" ' +
80
'averageTraveltime="%s"/>\n') % (idx, amount, int(1000. * ttSum / amount)))
81
total += amount
82
od.write(' </odPair>\n')
83
od.write(' </timeSlice>\n')
84
od.write(' <actorConfig/>\n')
85
od.write("</demand>\n")
86
87
88
if __name__ == "__main__":
89
argParser = ArgumentParser()
90
argParser.add_argument("-r", "--routes", default='routes.xml', category="input",
91
help="name of the amitran route file output [default: %(default)s]")
92
argParser.add_argument("-o", "--od-file", default='od.xml', category="output",
93
help="name of the amitran O/D file output [default: %(default)s]")
94
argParser.add_argument("-i", "--interval", default=3600, type=int,
95
help="aggregation interval in seconds [default: %(default)s]")
96
(options, args) = argParser.parse_args()
97
convert(args[0], options.routes, options.od_file, options.interval)
98
99