Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/import/visum/visum_export.py
185787 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 visum_export.py
15
# @author Michael Behrisch
16
# @date 2025-10-04
17
18
"""
19
Saves all relevant data (network, demand matrices and routes) from a VISUM .ver file
20
using VISUM's COM interface and packs them in a zip. If possible the data is retrieved
21
in the old line based format as well as in XML (ANM). VISUM needs to be installed
22
and have a current license including COM access on the machine you run this script on.
23
24
The easiest way to use it is to copy the script together with run_export.bat
25
and .ver file in a clean directory and start run_export.bat
26
which should find an appropriate python.
27
The script understands two parameters. The first is the .ver file to use (by default the
28
first one in the same directory as the script), the second is the name of the zip
29
(default is the name of the .ver but with the .zip ending).
30
The script is designed to support a wide range of VISUM versions with different COM APIs.
31
It has been tested successfully with VISUM 13 and VISUM 22.
32
"""
33
34
import glob
35
import os
36
import sys
37
import zipfile
38
39
import win32com.client
40
41
42
def load(ver_file):
43
try:
44
visum = win32com.client.Dispatch("Visum.Visum")
45
visum.LoadVersion(os.path.abspath(ver_file))
46
except Exception as e:
47
print("Could not load Visum or the ver file %s (%s)." % (ver_file, e), file=sys.stderr)
48
return None
49
return visum
50
51
52
def write(zipf, f, name):
53
if os.path.isfile(f):
54
zipf.write(f, os.path.basename(name))
55
os.remove(f)
56
57
58
def main(visum, ver_file, zip_file):
59
out_dir = os.path.dirname(zip_file)
60
if out_dir:
61
os.makedirs(out_dir, exist_ok=True)
62
out_dir = os.path.abspath(out_dir)
63
64
base_name = os.path.splitext(os.path.basename(ver_file))[0]
65
network = os.path.join(out_dir, base_name + "_anm_network.xml")
66
routes = os.path.join(out_dir, base_name + "_anm_routes.xml")
67
matrices = os.path.join(out_dir, base_name + "_anm_matrices.xml")
68
net = os.path.join(out_dir, base_name + ".net")
69
dmd = os.path.join(out_dir, base_name + ".dmd")
70
for f in (network, routes, matrices, net, dmd):
71
if os.path.exists(f):
72
os.remove(f)
73
try:
74
visum.SaveNet(net)
75
print("Wrote %s successfully." % net)
76
except Exception as e:
77
print("Could not write net %s (%s)." % (net, e), file=sys.stderr)
78
if hasattr(visum, "IO"):
79
try:
80
visum.IO.ExportANMNet(network, "")
81
print("Wrote %s successfully." % network)
82
except Exception as e:
83
print("Could not write network %s (%s)." % (network, e), file=sys.stderr)
84
try:
85
visum.IO.ExportANMRoutes(routes, "", True, False)
86
print("Wrote %s successfully." % routes)
87
visum.IO.ExportANMRoutes(matrices, "", False, True)
88
print("Wrote %s successfully." % matrices)
89
except Exception as e:
90
print("Could not write routes %s (%s)." % (routes, e), file=sys.stderr)
91
try:
92
visum.IO.SaveDemandFile(dmd, True)
93
print("Wrote %s successfully." % dmd)
94
except Exception as e:
95
print("Could not write dmd %s (%s)." % (dmd, e), file=sys.stderr)
96
if not os.path.exists(network + ".anm"):
97
try:
98
visum.ExportAnmNet(network, "")
99
print("Wrote %s successfully." % network)
100
except Exception as e:
101
print("Could not write network %s (%s)." % (network, e), file=sys.stderr)
102
if not os.path.exists(routes + ".anmRoutes"):
103
try:
104
visum.ExportAnmRoutes(routes, "", True, False)
105
print("Wrote %s successfully." % routes)
106
visum.ExportAnmRoutes(matrices, "", False, True)
107
print("Wrote %s successfully." % matrices)
108
except Exception as e:
109
print("Could not write routes %s (%s)." % (routes, e), file=sys.stderr)
110
if not os.path.exists(dmd):
111
try:
112
visum.SaveDemandFile(dmd, True)
113
print("Wrote %s successfully." % dmd)
114
except Exception as e:
115
print("Could not write dmd %s (%s)." % (dmd, e), file=sys.stderr)
116
with zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED) as zipf:
117
write(zipf, network + ".anm", network)
118
write(zipf, routes + ".anmRoutes", routes)
119
write(zipf, matrices + ".anmRoutes", matrices)
120
write(zipf, net, net)
121
write(zipf, dmd, dmd)
122
123
124
if __name__ == "__main__":
125
if len(sys.argv) > 1:
126
ver_file = sys.argv[1]
127
else:
128
ver_files = glob.glob("*.ver")
129
if not ver_files:
130
sys.exit("No .ver file found in current folder!")
131
ver_file = ver_files[0]
132
visum = load(ver_file)
133
if visum:
134
if len(sys.argv) > 2:
135
zip_file = sys.argv[2]
136
else:
137
zip_file = os.path.splitext(ver_file)[0] + ".zip"
138
main(visum, ver_file, zip_file)
139
140