3This script filters an .xodr map file to retain only a specified subset of roads based on their IDs.
7- Allows user to define a set of road IDs to keep (road_ids_to_keep).
8- Removes all roads and junctions not associated with the specified IDs.
9- Outputs a new .xodr file containing only the filtered elements.
12Creating a trimmed-down version of a map with only selected road segments for focused simulation or testing.
17road_ids_to_keep = {
"23"}
21input_file =
"original_map.xodr"
22output_file =
"filtered_map.xodr"
25 tree = etree.parse(input_file)
29 for road
in root.findall(
"road"):
30 if road.get(
"id")
not in road_ids_to_keep:
34 for junction
in root.findall(
"junction"):
36 for connection
in junction.findall(
"connection"):
37 if (connection.get(
"incomingRoad")
in road_ids_to_keep
or
38 connection.get(
"connectingRoad")
in road_ids_to_keep):
45 tree.write(output_file, pretty_print=
True, xml_declaration=
True, encoding=
'UTF-8')
46 print(f
"Filtered XODR written to: {output_file}")
49filter_xodr(input_file, output_file, road_ids_to_keep)
def filter_xodr(input_file, output_file, road_ids_to_keep)