4This script parses an OpenDRIVE (.xodr) map file and visualizes the basic road geometries using matplotlib.
8- Loads and parses an .xodr file using xml.etree.ElementTree.
9- Extracts position, heading, and length for each road geometry segment.
10- Plots each road segment as an arrow on a 2D plane.
11- Optionally overlays road names on the visualization for identification.
14Quick inspection and debugging of road layouts from .xodr files in a readable 2D plot.
22import matplotlib.pyplot
as plt
23import xml.etree.ElementTree
as ET
27xodr_file_path =
"xodr_map.xodr"
28tree = ET.parse(xodr_file_path)
31fig, ax = plt.subplots(figsize=(10, 10))
34for road
in root.findall(
".//road"):
35 road_name = road.get(
"name",
"")
36 for geometry
in road.findall(
".//geometry"):
40 hdg =
float(geometry.get(
"hdg"))
41 length =
float(geometry.get(
"length"))
43 dx = length * np.cos(hdg)
44 dy = length * np.sin(hdg)
46 ax.arrow(x, y, dx, dy, head_width=1.0, length_includes_head=
True)
49 ax.text(x, y, road_name, fontsize=6)
50 except Exception
as e:
51 print(
"Error in geometry:", ET.tostring(geometry, encoding=
'unicode'))
55ax.set_title(
"Basic Road Geometry Visualization (XODR)")