Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/xmlman.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file xmlman.py15# @author Joerg Schweizer16# @date 20121718#import classman as cm19#XMLTAG = 'xmltag'20import numpy as np21np.set_printoptions(suppress=True, precision=4)222324def write_obj_to_xml(obj, filepath,25encoding='UTF-8', # 'iso-8859-1'26):27"""28Universal xml writer for objects29"""30try:31fd = open(filepath, 'w')32except:33print 'WARNING in write_obj_to_xml: could not open', filepath34return False35fd.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)36indent = 037obj.write_xml(fd, indent)38fd.close()394041def begin(attr, indent=0):42return indent*' '+'<%s>\n' % attr434445def end(attr, indent=0):46return indent*' '+'</%s>\n' % attr474849def start(attr, indent=0):50return indent*' '+'<%s ' % attr515253def stop():54return '>\n'555657def stopit():58return '/>\n'596061def num(attr, x):62return ' %s="%s"' % (attr, x)636465INTTYPES = (np.int32, np.int32)66FLOATTYPES = (np.int32, np.int32)676869def arr(attr, a, sep=' '):70# TODO: use https://www.decalage.info/en/python/print_list71s = ' '72# print 'arr',attr,a73# if type(a)==np.ndarray:74# dt = a.dtype75# if dt in76# format = '%.3f'77# else:78#7980if len(a) > 0:81format = '%s'82s += '%s="' % attr83for i in xrange(len(a)):84# print ' a[i]',a[i],type(a[i]),str(a[i]),type(str(a[i]))85#ss = str(a[i])86# print ' ',type(s),type(ss),type(sep)87s += format % a[i]+sep88return s[:-1]+'"'89else:90# return s+'%s="<>"'%attr # NO!!91return s+'%s=""' % attr929394def color(attr, val, sep=','):95return arr(attr, np.array(val[0:3]*255+0.5, np.int32), sep)969798def mat(attr, m):99# TODO: use https://www.decalage.info/en/python/print_list100s = ' '101if len(m) > 0:102s += '%s="' % attr103for i in xrange(len(m)):104r = m[i]105for j in xrange(len(r)-1):106s += '%s,' % r[j]107s += '%s ' % r[-1]108return s[:-1]+'"'109else:110# return s+'%s="<>"'%attr# NO!!111return s+'%s=""' % attr112113114def parse_color(s, sep=','):115# print 'parseColor',s116arr = s.split(sep)117color = [1.0, 1.0, 1.0, 1.0]118#np.ones(4,dtype = np.float32)119i = 0120for a in arr:121color[i] = float(a)122i += 1123124return color125126127def parse_color_old(s):128# print 'parseColor',s129arr = s.split(',')130ret = []131for a in arr:132ret.append(float(a))133134return tuple(ret)135136137def process_shape(shapeString, offset=[0.0, 0.0]):138cshape = []139es = shapeString.rstrip().split(" ")140for e in es:141p = e.split(",")142if len(p) == 2:143# 2D coordinates with elevetion = 0144cshape.append(np.array([float(p[0])-offset[0], float(p[1]) - offset[1], 0.0], np.float32))145elif len(p) == 3:146# 3D coordinates147cshape.append(np.array([float(p[0])-offset[0], float(p[1]) - offset[1], float(p[2])], np.float32))148else:149# print 'WARNING: illshaped shape',e150# cshape.append(np.array([0,0,0],np.float))151return [] # np.zeros((0,2),np.float32)#None152153# np.array(cshape,np.float32)154return cshape # keep list of vertex arrays155156157# helper function for parsing comment line in xml file158def read_keyvalue(line, key):159data = line.split(' ')160for element in data:161if (element.find(key) >= 0):162k, v = element.split('=')163value = v.replace('"', '').replace('<', '').replace('/>', '').strip()164break165return value166167# def getShapeXml(shape):168## s = " shape=\""169# for i,c in enumerate(shape):170# if i!=0:171## s+=(" ")172## s+=(str(c[0]) + "," + str(c[1]))173# s+=("\"")174# return s175##176# def getListXml(l):177## s = " shape=\""178# for item in l:179## s+= str(item) + " "180# s+=("\"")181# return s182183184