示例代码说明:
遍历OpenDRIVE数据中每个路口JunctionID,读取需要变更的路口ID和路口名称的TXT文件,若JunctionID与TXT文件中的ID一致,则将TXT对应的点位名称更新到OpenDRIVE数据中Junction name字段。补充:需要保持TXT和OpenDRIVE数据文件编码一致,这里统一设置为UTF-8
# -*- coding: utf-8 -*-
import os
import xml.etree.ElementTree as ET
import sys
import pandas as pd
# 设置需要变更junction_name的文件路径
file_path = 'E:\\nodeID.txt'
df = pd.read_csv(file_path,encoding='utf-8')
xml_filepath = os.path.abspath("E:/OpenDRIVE.xodr")
tree = ET.parse(xml_filepath) # ET是一个xml文件解析库,ET.parse()打开xml文件。parse--"解析"
root = tree.getroot() # Element #根节点
Tag = 0
Code_junctions = root.findall("junction")
for Code_junction in Code_junctions:ids = int(Code_junction.attrib.get('id')) # 找到junction节点下id的属性值if ids in df.values.ravel():Tag = Tag + 1index = df.index[df.apply(lambda row: ids in row.values, axis=1)].tolist()print(index[0])nodename = df.loc[index[0], '点位名称']print(nodename)Code_junction.set('name',nodename)
tree = ET.ElementTree(root)
tree.write(xml_filepath,encoding='utf-8')
print("更新路口名称总数:",Tag,"个")
TXT文件示例:

更新后OpenDRIVE数据文件示例:
