Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/binary2plain.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2012-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 binary2plain.py
15
# @author Michael Behrisch
16
# @date 2012-03-11
17
18
from __future__ import absolute_import
19
from __future__ import print_function
20
import sys
21
import struct
22
23
BYTE = 0
24
INTEGER = 1
25
FLOAT = 2
26
STRING = 3
27
LIST = 4
28
XML_TAG_START = 5
29
XML_TAG_END = 6
30
XML_ATTRIBUTE = 7
31
EDGE = 8
32
LANE = 9
33
POSITION_2D = 10
34
POSITION_3D = 11
35
BOUNDARY = 12
36
COLOR = 13
37
NODE_TYPE = 14
38
EDGE_FUNCTION = 15
39
ROUTE = 16
40
SCALED2INT = 17
41
SCALED2INT_POSITION_2D = 18
42
SCALED2INT_POSITION_3D = 19
43
44
45
def read(content, format):
46
return struct.unpack(format, content.read(struct.calcsize(format)))
47
48
49
def readByte(content):
50
return read(content, "B")[0]
51
52
53
def readInt(content, withType=False):
54
if withType:
55
valType = readByte(content)
56
assert valType == INTEGER
57
return read(content, "i")[0]
58
59
60
def readDouble(content):
61
return read(content, "d")[0]
62
63
64
def readString(content):
65
length = readInt(content)
66
return read(content, "%ss" % length)[0]
67
68
69
def readStringList(content):
70
n = readInt(content)
71
list = []
72
for _ in range(n):
73
read(content, "B") # type
74
list.append(readString(content))
75
return list
76
77
78
def readIntListList(content):
79
n = readInt(content)
80
list = []
81
for _ in range(n):
82
read(content, "B") # type
83
n1 = readInt(content)
84
list.append([readInt(content, True) for __ in range(n1)])
85
return list
86
87
88
def readRoute(content):
89
n = readInt(content)
90
list = []
91
first = readInt(content)
92
if first < 0:
93
bits = -first
94
numFields = 8 * 4 / bits
95
mask = (1 << bits) - 1
96
edge = readInt(content)
97
list.append(edges[edge])
98
n -= 1
99
field = numFields
100
while n > 0:
101
if field == numFields:
102
data = readInt(content)
103
field = 0
104
followIndex = (data >> ((numFields - field - 1) * bits)) & mask
105
edge = followers[edge][followIndex]
106
list.append(edges[edge])
107
field += 1
108
n -= 1
109
else:
110
list.append(edges[first])
111
n -= 1
112
while n > 0:
113
list.append(edges[readInt(content)])
114
n -= 1
115
return list
116
117
118
def typedValueStr(content):
119
valType = readByte(content)
120
if valType == BYTE:
121
return str(readByte(content))
122
elif valType == INTEGER:
123
return str(readInt(content))
124
elif valType == FLOAT:
125
return '%.2f' % readDouble(content)
126
elif valType == STRING:
127
return readString(content)
128
elif valType == LIST:
129
return " ".join([typedValueStr(content) for _ in range(readInt(content))])
130
elif valType == EDGE:
131
return edges[readInt(content)]
132
elif valType == LANE:
133
return '%s_%s' % (edges[readInt(content)], readByte(content))
134
elif valType == POSITION_2D:
135
return '%.2f,%.2f' % (readDouble(content), readDouble(content))
136
elif valType == POSITION_3D:
137
return '%.2f,%.2f,%.2f' % (readDouble(content), readDouble(content), readDouble(content))
138
elif valType == BOUNDARY:
139
return '%.2f,%.2f,%.2f,%.2f' % (readDouble(content), readDouble(content),
140
readDouble(content), readDouble(content))
141
elif valType == COLOR:
142
val = read(content, "BBBB")
143
return '%.2f,%.2f,%.2f' % (val[0] / 255., val[1] / 255., val[2] / 255.)
144
elif valType == NODE_TYPE:
145
return nodeTypes[readByte(content)]
146
elif valType == EDGE_FUNCTION:
147
return edgeTypes[readByte(content)]
148
elif valType == ROUTE:
149
return " ".join(readRoute(content))
150
elif valType == SCALED2INT:
151
return '%.2f' % (readInt(content) / 100.)
152
elif valType == SCALED2INT_POSITION_2D:
153
return '%.2f,%.2f' % (readInt(content) / 100., readInt(content) / 100.)
154
elif valType == SCALED2INT_POSITION_3D:
155
return '%.2f,%.2f,%.2f' % (readInt(content) / 100., readInt(content) / 100., readInt(content) / 100.)
156
157
158
out = sys.stdout
159
content = open(sys.argv[1], 'rb')
160
_, version, _ = read(content, "BBB") # type, sbx version, type
161
readString(content) # sumo version
162
read(content, "B") # type
163
elements = readStringList(content)
164
read(content, "B") # type
165
attributes = readStringList(content)
166
read(content, "B") # type
167
nodeTypes = readStringList(content)
168
read(content, "B") # type
169
edgeTypes = readStringList(content)
170
read(content, "B") # type
171
edges = readStringList(content)
172
read(content, "B") # type
173
followers = readIntListList(content)
174
stack = []
175
startOpen = False
176
while True:
177
typ = readByte(content)
178
if typ == XML_TAG_START:
179
if startOpen:
180
out.write(">\n")
181
out.write(" " * len(stack))
182
tag = readByte(content)
183
if version > 1:
184
tag += 256 * readByte(content)
185
stack.append(tag)
186
out.write("<" + elements[tag])
187
startOpen = True
188
elif typ == XML_TAG_END:
189
if startOpen:
190
out.write("/>\n")
191
stack.pop()
192
startOpen = False
193
else:
194
out.write(" " * (len(stack) - 1))
195
out.write("</%s>\n" % elements[stack.pop()])
196
if version == 1:
197
readByte(content)
198
if len(stack) == 0:
199
break
200
elif typ == XML_ATTRIBUTE:
201
attr = readByte(content)
202
if version > 1:
203
attr += 256 * readByte(content)
204
out.write(' %s="%s"' %
205
(attributes[attr], typedValueStr(content)))
206
else:
207
print("Unknown type %s" % typ, file=sys.stderr)
208
209