def osm_to_commonroad(args, output_name: str): """Convert from OSM to CommonRoad. Args: args: Object containing parsed arguments. output_name: Name of file where result should be written to. """ with open(f"{args.input_file}", "r") as file_in: parser = OSMParser(etree.parse(file_in).getroot()) osm = parser.parse() osm2l = OSM2LConverter(proj_string=args.proj) scenario = osm2l(osm, detect_adjacencies=args.adjacencies, left_driving_system=args.left_driving) if scenario: writer = CommonRoadFileWriter( scenario=scenario, planning_problem_set=None, author="", affiliation="", source="OSM 2 CommonRoad Converter", tags="", ) with open(f"{output_name}", "w") as file_out: writer.write_scenario_to_file_io(file_out) else: print("Could not convert from OSM to CommonRoad format!")
def test_opendrive_scenario(self): """Test if the scenario is equal to the loaded xml file. Disregard the different dates. """ with open( os.path.dirname(os.path.realpath(__file__)) + f"/xodr_xml_test_files/{self.xml_output_name}.xml", "r", ) as fh: parser = etree.XMLParser(remove_blank_text=True) tree_import = etree.parse(fh, parser=parser).getroot() string_io = StringIO() writer = CommonRoadFileWriter( scenario=self.scenario, planning_problem_set=None, author="", affiliation="", source="OpenDRIVE 2 Lanelet Converter", tags="", ) writer.write_scenario_to_file_io(string_io) tree_generated = etree.fromstring(string_io.getvalue(), parser=parser) # set same date so this won't change the comparison tree_import.set("date", "2018-10-26") tree_generated.set("date", "2018-10-26") # compare both element trees trees_are_equal = elements_equal(tree_import, tree_generated) self.assertTrue(trees_are_equal)
def convert_scenario(input_folder, output_folder): if not os.path.isdir(output_folder): os.makedirs(output_folder) file_names = os.listdir(input_folder) for file_name in tqdm(file_names): label_path = input_folder + file_name label = load_label(label_path) # Use the result of moving object classifier to set corresponding the dynamic constraints if os.path.exists(args.dyna_obj_folder + file_name): file_path = args.init_scenario_folder + dyna_init_scenario else: file_path = args.init_scenario_folder + static_init_scenario scenario, planning_problem_set = CommonRoadFileReader(file_path).open() for i in range(len(label)): if label[i][0] != 'Car' and label[i][0] != 'Van' and label[i][ 0] != 'Truck' and label[i][0] != 'Misc': continue # regulate orientation to [-pi, pi] orient = label[i][7] while orient < -np.pi: orient += 2 * np.pi while orient > np.pi: orient -= 2 * np.pi static_obstacle_id = scenario.generate_object_id() static_obstacle_type = ObstacleType.PARKED_VEHICLE static_obstacle_shape = Rectangle(width=label[i][5][1], length=label[i][5][2]) static_obstacle_initial_state = State( position=np.array([label[i][6][2], -label[i][6][0]]), orientation=-(orient - 0.5 * np.pi), time_step=0) static_obstacle = StaticObstacle(static_obstacle_id, static_obstacle_type, static_obstacle_shape, static_obstacle_initial_state) scenario.add_objects(static_obstacle) author = 'Jindi Zhang' affiliation = 'City University of Hong Kong' source = '' tags = {Tag.CRITICAL, Tag.INTERSTATE} fw = CommonRoadFileWriter(scenario, planning_problem_set, author, affiliation, source, tags) output_file_name = output_folder + '/' + file_name.split( '.')[0] + '.xml' fw.write_to_file(output_file_name, OverwriteExistingFile.ALWAYS)
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 exportAsCommonRoad(self): """ """ if not self.loadedRoadNetwork: return path, _ = QFileDialog.getSaveFileName( self, "QFileDialog.getSaveFileName()", "", "CommonRoad files *.xml (*.xml)", options=QFileDialog.Options(), ) if not path: return try: with open(path, "w") as fh: writer = CommonRoadFileWriter( scenario=self.loadedRoadNetwork.export_commonroad_scenario( ), planning_problem_set=None, author="", affiliation="", source="OpenDRIVE 2 Lanelet Converter", tags="", ) writer.write_scenario_to_file_io(fh) except (IOError) as e: QMessageBox.critical( self, "CommonRoad file not created!", "The CommonRoad file was not exported due to an error.\n\n{}". format(e), QMessageBox.Ok, ) return QMessageBox.information( self, "CommonRoad file created!", "The CommonRoad file was successfully exported.", QMessageBox.Ok, )