def setUp(self): """Load the osm file and convert it to a scenario.""" try: commonroad_reader = CommonRoadFileReader( os.path.dirname(os.path.realpath(__file__)) + f"/osm_xml_test_files/{self.xml_file_name}.xml") scenario, _ = commonroad_reader.open() except etree.XMLSyntaxError as xml_error: print(f"SyntaxERror: {xml_error}") print( "There was an error during the loading of the selected CommonRoad file.\n" ) l2osm = L2OSMConverter(self.proj_string) self.osm = l2osm(scenario)
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 ) )
def commonroad_to_osm(args, output_name: str): """Convert from CommonRoad to OSM. Args: args: Object containing parsed arguments. output_name: Name of file where result should be written to. """ try: commonroad_reader = CommonRoadFileReader(args.input_file) scenario, _ = commonroad_reader.open() except etree.XMLSyntaxError as xml_error: print(f"SyntaxERror: {xml_error}") print( "There was an error during the loading of the selected CommonRoad file.\n" ) l2osm = L2OSMConverter(args.proj) 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))