Beispiel #1
0
    def load_opendriveFile(self, path):
        """

        Args:
          path:

        Returns:

        """

        filename = os.path.basename(path)
        self.inputOpenDriveFile.setText(filename)

        # Load road network and print some statistics
        try:
            fh = open(path, "r")
            openDriveXml = parse_opendrive(etree.parse(fh).getroot())
            fh.close()
        except (etree.XMLSyntaxError) as e:
            errorMsg = "XML Syntax Error: {}".format(e)
            QMessageBox.warning(
                self,
                "OpenDRIVE error",
                "There was an error during the loading of the selected OpenDRIVE file.\n\n{}"
                .format(errorMsg),
                QMessageBox.Ok,
            )
            return
        except (TypeError, AttributeError, ValueError) as e:
            errorMsg = "Value Error: {}".format(e)
            QMessageBox.warning(
                self,
                "OpenDRIVE error",
                "There was an error during the loading of the selected OpenDRIVE file.\n\n{}"
                .format(errorMsg),
                QMessageBox.Ok,
            )
            return

        self.loadedRoadNetwork = Network()
        self.loadedRoadNetwork.load_opendrive(openDriveXml)

        self.statsText.setText(
            """Name: {}<br>Version: {}<br>Date: {}<br><br>OpenDRIVE
            Version {}.{}<br><br>Number of roads: {}<br>Total length
            of road network: {:.2f} meters""".format(
                openDriveXml.header.name
                if openDriveXml.header.name else "<i>unset</i>",
                openDriveXml.header.version,
                openDriveXml.header.date,
                openDriveXml.header.revMajor,
                openDriveXml.header.revMinor,
                len(openDriveXml.roads),
                sum([road.length for road in openDriveXml.roads]),
            ))

        self.exportCommonRoadButton.setDisabled(False)
        self.viewOutputButton.setDisabled(False)
 def open_drive_loader(self, fn):
     fi = open(fn.format(os.path.dirname(os.path.realpath(__file__))), "r")
     tree = etree.parse(fi)
     root = tree.getroot()
     geoReference = root[0][0].text
     open_drive = parse_opendrive(root)
     road_network = Network()
     road_network.load_opendrive(open_drive)
     return road_network.export_commonroad_scenario(), geoReference
    def setUp(self):
        """Load the xodr file and create the scenario.

        """
        if not self.xml_output_name:
            self.xml_output_name = self.xodr_file_name
        with open(
                os.path.dirname(os.path.realpath(__file__)) +
                f"/xodr_xml_test_files/{self.xodr_file_name}.xodr",
                "r",
        ) as fh:
            opendrive = parse_opendrive(etree.parse(fh).getroot())
        self.scenario = convert_opendrive(opendrive)
Beispiel #4
0
def main():
    """Helper function to convert an xodr to a lanelet file

    """
    args = parse_arguments()

    if args.output_name:
        output_name = args.output_name
    else:
        output_name = args.xodr_file.rpartition(".")[0]
        output_name = (
            f"{output_name}.osm" if args.osm else f"{output_name}.xml"
        )  # only name of file

    if os.path.isfile(output_name) and not args.force_overwrite:
        print(
            "Not converting because file exists and option 'force-overwrite' not active",
            file=sys.stderr,
        )
        sys.exit(-1)

    with open("{}".format(args.xodr_file), "r") as file_in:
        opendrive = parse_opendrive(etree.parse(file_in).getroot())

    scenario = convert_opendrive(opendrive)

    if not args.osm:
        writer = CommonRoadFileWriter(
            scenario=scenario,
            planning_problem_set=None,
            author="",
            affiliation="",
            source="OpenDRIVE 2 Lanelet Converter",
            tags="",
        )

        with open(f"{output_name}", "w") as file_out:
            writer.write_scenario_to_file_io(file_out)

    else:
        l2osm = L2OSMConverter(args.osm)
        osm = l2osm(scenario)
        with open(f"{output_name}", "wb") as file_out:
            file_out.write(
                etree.tostring(osm,
                               xml_declaration=True,
                               encoding="UTF-8",
                               pretty_print=True))
    def convert(self, output_file_path):
        # Open File
        file_in = open(self.input_file_path.format(os.path.dirname(os.path.realpath(__file__))), "r")
        
        # Parse XML
        tree = etree.parse(file_in)
        root = tree.getroot()
        open_drive = parse_opendrive(root)

        # Access
        header = root.find('header')
        geoReference = header.find('geoReference').text

        # Build CommonRoad Lanelet1 network
        road_network = Network()
        road_network.load_opendrive(open_drive)

        simple_geoReference = geoReference

        print('Found geoReference: ' + simple_geoReference)

        if '+geoidgrids=' in simple_geoReference:
            print('Ignoring +geoidgrids in projection as it is not currently supported')
            geoidgrids_tag = re.search('.*?(\+geoidgrids\=.*?\ +).*$', simple_geoReference).group(1)
            simple_geoReference = simple_geoReference.replace(geoidgrids_tag, '')
            print('Used conversion projection is ' + simple_geoReference)

        file_in.close()

        # Convert to Lanelet2 OSM
        osm_converter = L2OSMConverter(simple_geoReference)
        
        osm_map = osm_converter(road_network.export_commonroad_scenario())

        # Add georeference back to osm
        geo_tag = etree.Element('geoReference')
        geo_tag.text = geoReference
        osm_map.insert(0, geo_tag)

        with open(output_file_path, "wb") as file_out:
            file_out.write(
                etree.tostring(
                    osm_map, xml_declaration=True, encoding="UTF-8", pretty_print=True
                )
            )
Beispiel #6
0
def main():
    """Helper function to convert an xodr to a lanelet file
    script uses sys.argv[1] as file to be converted

    """
    args = parse_arguments()

    if args.output_name:
        output_name = args.output_name
    else:
        output_name = args.xodr_file.rpartition(".")[0]
        output_name = f"{output_name}.xml"  # only name of file

    if os.path.isfile(output_name) and not args.force_overwrite:
        print(
            "Not converting because file exists and option 'force-overwrite' not active",
            file=sys.stderr,
        )
        sys.exit(-1)

    with open("{}".format(args.xodr_file), "r") as file_in:
        opendrive = parse_opendrive(etree.parse(file_in).getroot())

    scenario = convert_opendrive(opendrive)

    if not args.osm:
        writer = ExtendedCommonRoadFileWriter(
            scenario, source="OpenDRIVE 2 Lanelet Converter")

        with open(f"{output_name}", "w") as file_out:
            writer.write_scenario_to_file_io(file_out)

    else:
        l2osm = OSMConverter(args.osm)
        osm = l2osm(scenario)
        with open(f"{output_name}", "w") as file_out:
            file_out.write(
                etree.tostring(osm, encoding="unicode", pretty_print=True))