Bug in converting OpenDrive to CommonRoads

Hi,

I wanted to use CommonRoad scenario designer to convert an OpenDrive file to OSM (had to use the lanelet2 converter finally as the OSM one wasn’t yet available). However when first converting the OpenDrive file to CommonRoads, I got the following error:
AttributeError: 'NoneType' object has no attribute 'geo_reference'

This is caused by the OpenDrive file which I used having a namespace tag in the root node (xmlns), like so:
<OpenDRIVE xmlns="http://www.opendrive.org">

This is required as per the OpenDrive specification (OpenDRIVE 1.6):

delimiters <OpenDRIVE>…​</OpenDRIVE>
parent none
instances 1
attributes xmlns=“http://www.opendrive.org

To fix this, I modified opendrive_to_commonroad() in ./crdesigner/map_conversion/map_conversion_interface.py by removing namespaces from the parsed xml file. This is the “right” way to do it according to a stackoverflow post I found (will post the link to it in a comment on this post).

After changing opendrive_to_commonroad() to the following:

def opendrive_to_commonroad(input_file: str) -> Scenario:
    """
    Converts OpenDRIVE file to CommonRoad

    @param input_file: Path to OpenDRIVE file
    @return: CommonRoad scenario
    """
    with open("{}".format(input_file), "r") as file_in:
        # remove namespaces
        root = etree.parse(file_in)

        # Iterate through all XML elements
        for elem in root.getiterator():
            # Skip comments and processing instructions,
            # because they do not have names
            if not (
                isinstance(elem, etree._Comment)
                or isinstance(elem, etree._ProcessingInstruction)
            ):
                # Remove a namespace URI in the element's name
                elem.tag = etree.QName(elem).localname

        # Remove unused namespace declarations
        etree.cleanup_namespaces(root)

        root = root.getroot()
        opendrive = parse_opendrive(root)

    road_network = Network()
    road_network.load_opendrive(opendrive)

    return road_network.export_commonroad_scenario()

I am now able to convert valid OpenDrive files.

Stackoverflow post: https://stackoverflow.com/questions/18159221/remove-namespace-and-prefix-from-xml-in-python-using-lxml

Thank you for notifying us.
We will fix this in the next release of the CommonRoad Scenario Designer.

1 Like