Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/xmlman.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
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 xmlman.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
#import classman as cm
20
#XMLTAG = 'xmltag'
21
import numpy as np
22
np.set_printoptions(suppress=True, precision=4)
23
24
25
def write_obj_to_xml(obj, filepath,
26
encoding='UTF-8', # 'iso-8859-1'
27
):
28
"""
29
Universal xml writer for objects
30
"""
31
try:
32
fd = open(filepath, 'w')
33
except:
34
print 'WARNING in write_obj_to_xml: could not open', filepath
35
return False
36
fd.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)
37
indent = 0
38
obj.write_xml(fd, indent)
39
fd.close()
40
41
42
def begin(attr, indent=0):
43
return indent*' '+'<%s>\n' % attr
44
45
46
def end(attr, indent=0):
47
return indent*' '+'</%s>\n' % attr
48
49
50
def start(attr, indent=0):
51
return indent*' '+'<%s ' % attr
52
53
54
def stop():
55
return '>\n'
56
57
58
def stopit():
59
return '/>\n'
60
61
62
def num(attr, x):
63
return ' %s="%s"' % (attr, x)
64
65
66
INTTYPES = (np.int32, np.int32)
67
FLOATTYPES = (np.int32, np.int32)
68
69
70
def arr(attr, a, sep=' '):
71
# TODO: use https://www.decalage.info/en/python/print_list
72
s = ' '
73
# print 'arr',attr,a
74
# if type(a)==np.ndarray:
75
# dt = a.dtype
76
# if dt in
77
# format = '%.3f'
78
# else:
79
#
80
81
if len(a) > 0:
82
format = '%s'
83
s += '%s="' % attr
84
for i in xrange(len(a)):
85
# print ' a[i]',a[i],type(a[i]),str(a[i]),type(str(a[i]))
86
#ss = str(a[i])
87
# print ' ',type(s),type(ss),type(sep)
88
s += format % a[i]+sep
89
return s[:-1]+'"'
90
else:
91
# return s+'%s="<>"'%attr # NO!!
92
return s+'%s=""' % attr
93
94
95
def color(attr, val, sep=','):
96
return arr(attr, np.array(val[0:3]*255+0.5, np.int32), sep)
97
98
99
def mat(attr, m):
100
# TODO: use https://www.decalage.info/en/python/print_list
101
s = ' '
102
if len(m) > 0:
103
s += '%s="' % attr
104
for i in xrange(len(m)):
105
r = m[i]
106
for j in xrange(len(r)-1):
107
s += '%s,' % r[j]
108
s += '%s ' % r[-1]
109
return s[:-1]+'"'
110
else:
111
# return s+'%s="<>"'%attr# NO!!
112
return s+'%s=""' % attr
113
114
115
def parse_color(s, sep=','):
116
# print 'parseColor',s
117
arr = s.split(sep)
118
color = [1.0, 1.0, 1.0, 1.0]
119
#np.ones(4,dtype = np.float32)
120
i = 0
121
for a in arr:
122
color[i] = float(a)
123
i += 1
124
125
return color
126
127
128
def parse_color_old(s):
129
# print 'parseColor',s
130
arr = s.split(',')
131
ret = []
132
for a in arr:
133
ret.append(float(a))
134
135
return tuple(ret)
136
137
138
def process_shape(shapeString, offset=[0.0, 0.0]):
139
cshape = []
140
es = shapeString.rstrip().split(" ")
141
for e in es:
142
p = e.split(",")
143
if len(p) == 2:
144
# 2D coordinates with elevetion = 0
145
cshape.append(np.array([float(p[0])-offset[0], float(p[1]) - offset[1], 0.0], np.float32))
146
elif len(p) == 3:
147
# 3D coordinates
148
cshape.append(np.array([float(p[0])-offset[0], float(p[1]) - offset[1], float(p[2])], np.float32))
149
else:
150
# print 'WARNING: illshaped shape',e
151
# cshape.append(np.array([0,0,0],np.float))
152
return [] # np.zeros((0,2),np.float32)#None
153
154
# np.array(cshape,np.float32)
155
return cshape # keep list of vertex arrays
156
157
158
# helper function for parsing comment line in xml file
159
def read_keyvalue(line, key):
160
data = line.split(' ')
161
for element in data:
162
if (element.find(key) >= 0):
163
k, v = element.split('=')
164
value = v.replace('"', '').replace('<', '').replace('/>', '').strip()
165
break
166
return value
167
168
# def getShapeXml(shape):
169
## s = " shape=\""
170
# for i,c in enumerate(shape):
171
# if i!=0:
172
## s+=(" ")
173
## s+=(str(c[0]) + "," + str(c[1]))
174
# s+=("\"")
175
# return s
176
##
177
# def getListXml(l):
178
## s = " shape=\""
179
# for item in l:
180
## s+= str(item) + " "
181
# s+=("\"")
182
# return s
183
184