Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/netduplicate.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2013-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 netduplicate.py
15
# @author Michael Behrisch
16
# @author Jakob Erdmann
17
# @date 2013-04-01
18
19
"""
20
Reads a sumo network and duplication descriptors (prefix:x-offset:y-offset)
21
and creates a disconnected network of duplicates.
22
"""
23
from __future__ import absolute_import
24
from __future__ import print_function
25
26
import sys
27
import os
28
import subprocess
29
import tempfile
30
import shutil
31
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
32
import sumolib # noqa
33
from sumolib.options import ArgumentParser # noqa
34
35
36
def parseArgs():
37
USAGE = "Usage: " + sys.argv[0] + " <net> <prefix:x:y> <prefix:x:y>+"
38
optParser = ArgumentParser(usage=USAGE)
39
sumolib.pullOptions(sumolib.checkBinary("netconvert"), optParser)
40
optParser.add_option(
41
"net", type=ArgumentParser.net_file, help="network to duplicate")
42
optParser.add_option(
43
"desc", nargs="+", help="List of descriptions Prefix:OffsetX:OffsetY+")
44
optParser.add_option(
45
"--drop-types", action="store_true", default=False, help="Remove edge types")
46
options = optParser.parse_args()
47
options.desc = [d.split(":") for d in options.desc]
48
return options
49
50
51
def createPlain(netconvert, netfile, prefix, xOff, yOff):
52
subprocess.call([netconvert,
53
"--sumo-net-file", netfile,
54
"--offset.x", xOff,
55
"--offset.y", yOff,
56
"--plain-output-prefix", prefix])
57
58
59
# run
60
def main():
61
options = parseArgs()
62
netconvert = sumolib.checkBinary("netconvert")
63
nodes = []
64
edges = []
65
conns = []
66
tlls = []
67
tmpDir = tempfile.mkdtemp()
68
for prefix, xoff, yoff in options.desc:
69
createPlain(
70
netconvert, options.net, os.path.join(tmpDir, prefix), xoff, yoff)
71
out = open(os.path.join(tmpDir, "%s_.nod.xml" % prefix), 'w')
72
with open(os.path.join(tmpDir, "%s.nod.xml" % prefix)) as nfile:
73
for line in nfile:
74
if 'location' in line:
75
continue
76
line = line.replace('id="', 'id="%s_' % prefix)
77
line = line.replace('tl="', 'tl="%s_' % prefix)
78
out.write(line)
79
out.close()
80
nodes.append(out.name)
81
out = open(os.path.join(tmpDir, "%s_.edg.xml" % prefix), 'w')
82
with open(os.path.join(tmpDir, "%s.edg.xml" % prefix)) as efile:
83
for line in efile:
84
line = line.replace('id="', 'id="%s_' % prefix)
85
line = line.replace('from="', 'from="%s_' % prefix)
86
line = line.replace('to="', 'to="%s_' % prefix)
87
if options.drop_types:
88
typeStart = line.find('type="')
89
if typeStart >= 0:
90
typeEnd = line.find('"', typeStart + 6)
91
line = line[0:typeStart] + line[typeEnd + 1:]
92
out.write(line)
93
out.close()
94
edges.append(out.name)
95
out = open(os.path.join(tmpDir, "%s_.con.xml" % prefix), 'w')
96
with open(os.path.join(tmpDir, "%s.con.xml" % prefix)) as cfile:
97
for line in cfile:
98
line = line.replace('from="', 'from="%s_' % prefix)
99
line = line.replace('to="', 'to="%s_' % prefix)
100
out.write(line)
101
out.close()
102
conns.append(out.name)
103
out = open(os.path.join(tmpDir, "%s_.tll.xml" % prefix), 'w')
104
with open(os.path.join(tmpDir, "%s.tll.xml" % prefix)) as tfile:
105
for line in tfile:
106
line = line.replace('id="', 'id="%s_' % prefix)
107
line = line.replace('from="', 'from="%s_' % prefix)
108
line = line.replace('to="', 'to="%s_' % prefix)
109
line = line.replace('tl="', 'tl="%s_' % prefix)
110
out.write(line)
111
out.close()
112
tlls.append(out.name)
113
options.node_files = ",".join(nodes)
114
options.edge_files = ",".join(edges)
115
options.connection_files = ",".join(conns)
116
options.tllogic_files = ",".join(tlls)
117
if sumolib.call(netconvert, options) != 0:
118
print("Something went wrong, check '%s'!" % tmpDir, file=sys.stderr)
119
else:
120
shutil.rmtree(tmpDir)
121
122
123
if __name__ == "__main__":
124
main()
125
126