Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
filter_roads.py
Go to the documentation of this file.
1"""
2Purpose:
3This script filters an .xodr map file to retain only a specified subset of roads based on their IDs.
4
5Key Features:
6
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.
10
11Use Case:
12Creating a trimmed-down version of a map with only selected road segments for focused simulation or testing.
13"""
14from lxml import etree
15
16# === EDIT THIS LIST: IDs of roads you want to keep ===
17road_ids_to_keep = {"23"}
18
19# === Input and output file paths ===
20# TODO: Change these to match actual files
21input_file = "original_map.xodr"
22output_file = "filtered_map.xodr"
23
24def filter_xodr(input_file, output_file, road_ids_to_keep):
25 tree = etree.parse(input_file)
26 root = tree.getroot()
27
28 # Remove roads not in the list
29 for road in root.findall("road"):
30 if road.get("id") not in road_ids_to_keep:
31 root.remove(road)
32
33 # Remove unrelated junctions
34 for junction in root.findall("junction"):
35 remove = True
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):
39 remove = False
40 break
41 if remove:
42 root.remove(junction)
43
44 # Write the result to output
45 tree.write(output_file, pretty_print=True, xml_declaration=True, encoding='UTF-8')
46 print(f"Filtered XODR written to: {output_file}")
47
48# === Run the filter ===
49filter_xodr(input_file, output_file, road_ids_to_keep)
def filter_xodr(input_file, output_file, road_ids_to_keep)
Definition: filter_roads.py:24