Module scenariogeneration.xosc.scenario
scenariogeneration https://github.com/pyoscx/scenariogeneration
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
Copyright (c) 2022 The scenariogeneration Authors.
Classes
class RoadNetwork (roadfile=None, scenegraph=None)
-
Expand source code
class RoadNetwork(VersionBase): """The RoadNetwork class creates the RoadNetwork of the openScenario Parameters ---------- roadfile (str): path to the opendrive file scenegraph (str): path to the opensceengraph file (optional) Attributes ---------- road_file (str): path to the opendrive file scene (str): path to the opensceengraph file traffic_signals (list of TrafficSignalController): all traffic signals in the roadnetwork used_area_positions (list of Positions): the positions that determines the used area of the roadnetwork Methods ------- parse(element) parses a ElementTree created by the class and returns an instance of the class get_element() Returns the full ElementTree of the class """ def __init__(self, roadfile=None, scenegraph=None): """Initalizes the RoadNetwork Parameters ---------- roadfile (str): path to the opendrive file (optional) scenegraph (str): path to the opensceengraph file (optional) """ self.road_file = roadfile self.scene = scenegraph self.traffic_signals = [] self.used_area_positions = [] def __eq__(self, other): if isinstance(other, RoadNetwork): if ( self.road_file == other.road_file and self.scene == other.scene and self.traffic_signals == other.traffic_signals and self.used_area_positions == other.used_area_positions ): return True return False @staticmethod def parse(element): """Parses the xml element of RoadNetwork Parameters ---------- element (xml.etree.ElementTree.Element): A RoadNetwork element (same as generated by the class itself) Returns ------- roadnetwork (RoadNetwork): a RoadNetwork object """ logicFile = None if element.find("LogicFile") is not None: logicFile = element.find("LogicFile").attrib["filepath"] sceneGraphFile = None if element.find("SceneGraphFile") is not None: sceneGraphFile = element.find("SceneGraphFile").attrib["filepath"] roadnetwork = RoadNetwork(roadfile=logicFile, scenegraph=sceneGraphFile) position_elements = element.findall("UsedArea/Position") if position_elements is not None: for position in position_elements: roadnetwork.add_used_area_position( _PositionFactory.parse_position(position) ) tsc_elements = element.findall("TrafficSignals/TrafficSignalController") if tsc_elements is not None: for tsc_element in tsc_elements: controller = TrafficSignalController.parse(tsc_element) roadnetwork.add_traffic_signal_controller(controller) return roadnetwork def add_traffic_signal_controller(self, traffic_signal_controller): """adds a TrafficSignalController to the RoadNetwork Parameters ---------- traffic_signal_controller (TrafficSignalController): the traffic signal controller to add """ if not isinstance(traffic_signal_controller, TrafficSignalController): raise TypeError( "traffic_signal_controller input is not of type TrafficSignalController" ) self.traffic_signals.append(traffic_signal_controller) return self def add_used_area_position(self, position): """adds a position to determine the usedArea of the roadnetwork used, this feature was added in OpenSCENARIO V1.1. Atleast 2 positions are required. Parameters ---------- position (*Position): any position to determine the used area """ if not isinstance(position, _PositionType): raise TypeError("position input is not a valid position Type") self.used_area_positions.append(position) return self def get_element(self): """returns the elementTree of the RoadNetwork""" roadnetwork = ET.Element("RoadNetwork") if self.road_file: ET.SubElement(roadnetwork, "LogicFile", {"filepath": self.road_file}) if self.scene: ET.SubElement(roadnetwork, "SceneGraphFile", {"filepath": self.scene}) if self.traffic_signals: trafsign_element = ET.SubElement(roadnetwork, "TrafficSignals") for ts in self.traffic_signals: trafsign_element.append(ts.get_element()) if len(self.used_area_positions) == 1: raise NotEnoughInputArguments( 'To use "usedArea" more than 1 used_area_position is needed.' ) elif len(self.used_area_positions) > 1 and self.isVersion(minor=0): raise OpenSCENARIOVersionError( "UsedArea is not supported in OpenSCENARIO V1.0, was introduced in OpenSCENARIO V1.1" ) elif len(self.used_area_positions) > 1 and not self.isVersion(minor=0): usedarea = ET.SubElement(roadnetwork, "UsedArea") for p in self.used_area_positions: usedarea.append(p.get_element()) return roadnetwork
The RoadNetwork class creates the RoadNetwork of the openScenario
Parameters
roadfile (str): path to the opendrive file scenegraph (str): path to the opensceengraph file (optional)
Attributes
road_file (str): path to the opendrive file scene (str): path to the opensceengraph file traffic_signals (list of TrafficSignalController): all traffic signals in the roadnetwork used_area_positions (list of Positions): the positions that determines the used area of the roadnetwork
Methods
parse(element) parses a ElementTree created by the class and returns an instance of the class get_element() Returns the full ElementTree of the class
Initalizes the RoadNetwork
Parameters
roadfile (str): path to the opendrive file (optional) scenegraph (str): path to the opensceengraph file (optional)
Ancestors
Static methods
def parse(element)
-
Expand source code
@staticmethod def parse(element): """Parses the xml element of RoadNetwork Parameters ---------- element (xml.etree.ElementTree.Element): A RoadNetwork element (same as generated by the class itself) Returns ------- roadnetwork (RoadNetwork): a RoadNetwork object """ logicFile = None if element.find("LogicFile") is not None: logicFile = element.find("LogicFile").attrib["filepath"] sceneGraphFile = None if element.find("SceneGraphFile") is not None: sceneGraphFile = element.find("SceneGraphFile").attrib["filepath"] roadnetwork = RoadNetwork(roadfile=logicFile, scenegraph=sceneGraphFile) position_elements = element.findall("UsedArea/Position") if position_elements is not None: for position in position_elements: roadnetwork.add_used_area_position( _PositionFactory.parse_position(position) ) tsc_elements = element.findall("TrafficSignals/TrafficSignalController") if tsc_elements is not None: for tsc_element in tsc_elements: controller = TrafficSignalController.parse(tsc_element) roadnetwork.add_traffic_signal_controller(controller) return roadnetwork
Parses the xml element of RoadNetwork
Parameters
element (xml.etree.ElementTree.Element): A RoadNetwork element (same as generated by the class itself)
Returns
roadnetwork (RoadNetwork): a RoadNetwork object
Methods
def add_traffic_signal_controller(self, traffic_signal_controller)
-
Expand source code
def add_traffic_signal_controller(self, traffic_signal_controller): """adds a TrafficSignalController to the RoadNetwork Parameters ---------- traffic_signal_controller (TrafficSignalController): the traffic signal controller to add """ if not isinstance(traffic_signal_controller, TrafficSignalController): raise TypeError( "traffic_signal_controller input is not of type TrafficSignalController" ) self.traffic_signals.append(traffic_signal_controller) return self
adds a TrafficSignalController to the RoadNetwork
Parameters
traffic_signal_controller (TrafficSignalController): the traffic signal controller to add
def add_used_area_position(self, position)
-
Expand source code
def add_used_area_position(self, position): """adds a position to determine the usedArea of the roadnetwork used, this feature was added in OpenSCENARIO V1.1. Atleast 2 positions are required. Parameters ---------- position (*Position): any position to determine the used area """ if not isinstance(position, _PositionType): raise TypeError("position input is not a valid position Type") self.used_area_positions.append(position) return self
adds a position to determine the usedArea of the roadnetwork used, this feature was added in OpenSCENARIO V1.1. Atleast 2 positions are required.
Parameters
position (*Position): any position to determine the used area
def get_element(self)
-
Expand source code
def get_element(self): """returns the elementTree of the RoadNetwork""" roadnetwork = ET.Element("RoadNetwork") if self.road_file: ET.SubElement(roadnetwork, "LogicFile", {"filepath": self.road_file}) if self.scene: ET.SubElement(roadnetwork, "SceneGraphFile", {"filepath": self.scene}) if self.traffic_signals: trafsign_element = ET.SubElement(roadnetwork, "TrafficSignals") for ts in self.traffic_signals: trafsign_element.append(ts.get_element()) if len(self.used_area_positions) == 1: raise NotEnoughInputArguments( 'To use "usedArea" more than 1 used_area_position is needed.' ) elif len(self.used_area_positions) > 1 and self.isVersion(minor=0): raise OpenSCENARIOVersionError( "UsedArea is not supported in OpenSCENARIO V1.0, was introduced in OpenSCENARIO V1.1" ) elif len(self.used_area_positions) > 1 and not self.isVersion(minor=0): usedarea = ET.SubElement(roadnetwork, "UsedArea") for p in self.used_area_positions: usedarea.append(p.get_element()) return roadnetwork
returns the elementTree of the RoadNetwork
class Scenario (name,
author,
parameters,
entities,
storyboard,
roadnetwork,
catalog,
osc_minor_version=2,
license=None,
creation_date=None,
header_properties=None,
variable_declaration=None)-
Expand source code
class Scenario(VersionBase): """The Scenario class collects all parts of OpenScenario and creates a .xml file Parameters ---------- header (FileHeader): the header of the scenario file parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario osc_minor_version (int): used to set if another than the newest version of OpenSCENARIO should be used Default: 2 licence (License): optional license to the file header Default: None createtion_date (datetime.datetime): optional creation date of the scenario Default: None (will be at the time of generation) Attributes ---------- header (FileHeader): the header of the scenario file parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario header (FileHeader): header of the scenario Methods ------- parse(element) parses a ElementTree created by the class and returns an instance of the class get_element() Returns the full ElementTree of the class write_xml(filename) write a open scenario xml """ _XMLNS = XMLNS _XSI = XSI def __init__( self, name, author, parameters, entities, storyboard, roadnetwork, catalog, osc_minor_version=_MINOR_VERSION, license=None, creation_date=None, header_properties=None, variable_declaration=None, ): """Initalizes the Scenario class, and creates the header. Parameters ---------- name (str): name of the scenario author (str): the author fo the scenario parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario osc_minor_version (int): used to set if another than the newest version of OpenSCENARIO should be used Default: 1 licence (License): optional license to the file header (Valid from OSC V1.1) Default: None createtion_date (datetime.datetime): optional creation date of the scenario Default: None (will be at the time of generation) header_properties (Properties): properties that can be added to the header (Valid from OSC V1.2) Default: None variable_declaration (VariableDeclarations): (Valid from OSC V1.2) """ if not isinstance(entities, Entities): raise TypeError("entities input is not of type Entities") if not isinstance(storyboard, StoryBoard): raise TypeError("storyboard input is not of type StoryBoard") if not isinstance(roadnetwork, RoadNetwork): raise TypeError("roadnetwork input is not of type RoadNetwork") if not isinstance(catalog, Catalog): raise TypeError("catalog input is not of type StorCatalogyBoard") if not isinstance(parameters, ParameterDeclarations): raise TypeError("parameters input is not of type ParameterDeclarations") if variable_declaration and not isinstance( variable_declaration, VariableDeclarations ): raise TypeError( "variable_declaration input is not of type VariableDeclarations" ) self.variable_declaration = variable_declaration self.entities = entities self.storyboard = storyboard self.roadnetwork = roadnetwork self.catalog = catalog self.parameters = parameters self.header = FileHeader( author, name, revMinor=osc_minor_version, license=license, creation_date=creation_date, properties=header_properties, ) def __eq__(self, other): if isinstance(other, Scenario): if ( self.entities == other.entities and self.storyboard == other.storyboard and self.roadnetwork == other.roadnetwork and self.catalog == other.catalog and self.header == other.header and self.parameters == other.parameters and self.variable_declaration == other.variable_declaration ): return True return False @staticmethod def parse(element): """Parses the xml element of Scenario Parameters ---------- element (xml.etree.ElementTree.Element): A Scenario element (same as generated by the class itself) Returns ------- scenario (Scenario): a Scenario object """ header = FileHeader.parse(element.find("FileHeader")) parameters = ParameterDeclarations() if element.find("ParameterDeclarations") is not None: parameters = ParameterDeclarations.parse( element.find("ParameterDeclarations") ) catalog = Catalog.parse(element.find("CatalogLocations")) storyboard = StoryBoard.parse(element.find("Storyboard")) entities = Entities.parse(element.find("Entities")) roadnetwork = RoadNetwork.parse(element.find("RoadNetwork")) variables = None if element.find("VariableDeclarations") is not None: variables = VariableDeclarations.parse(element.find("VariableDeclarations")) return Scenario( header.description, header.author, parameters, entities, storyboard, roadnetwork, catalog, header._revMinor, header.properties, variables, ) def get_element(self): """returns the elementTree of the Scenario""" element = ET.Element( "OpenSCENARIO", attrib={ "xmlns:xsi": self._XMLNS, "xsi:noNamespaceSchemaLocation": self._XSI, }, ) element.append(self.header.get_element()) if self.parameters.get_element(): element.append(self.parameters.get_element()) element.append(self.catalog.get_element()) element.append(self.roadnetwork.get_element()) element.append(self.entities.get_element()) element.append(self.storyboard.get_element()) if self.variable_declaration: element.append(self.variable_declaration.get_element()) return element def write_xml(self, filename, prettyprint=True, encoding="utf-8"): """write_xml writes the OpenSCENARIO xml file Parameters ---------- filename (str): path and filename of the wanted xml file prettyprint (bool): pretty print or ugly print? Default: True encoding (str): specifies the output encoding Default: 'utf-8' """ printToFile(self.get_element(), filename, prettyprint, encoding)
The Scenario class collects all parts of OpenScenario and creates a .xml file
Parameters
header (FileHeader): the header of the scenario file parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario osc_minor_version (int): used to set if another than the newest version of OpenSCENARIO should be used Default: 2 licence (License): optional license to the file header Default: None createtion_date (datetime.datetime): optional creation date of the scenario Default: None (will be at the time of generation)
Attributes
header (FileHeader): the header of the scenario file parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario header (FileHeader): header of the scenario
Methods
parse(element) parses a ElementTree created by the class and returns an instance of the class get_element() Returns the full ElementTree of the class write_xml(filename) write a open scenario xml
Initalizes the Scenario class, and creates the header.
Parameters
name (str): name of the scenario author (str): the author fo the scenario parameters (ParameterDeclarations): the parameters to be used in the scenario entities (Entities): the entities in the scenario storyboard (StoryBoard): the storyboard of the scenario roadnetwork (RoadNetwork): the roadnetwork of the scenario catalog (Catalog): the catalogs used in the scenario osc_minor_version (int): used to set if another than the newest version of OpenSCENARIO should be used Default: 1 licence (License): optional license to the file header (Valid from OSC V1.1) Default: None createtion_date (datetime.datetime): optional creation date of the scenario Default: None (will be at the time of generation) header_properties (Properties): properties that can be added to the header (Valid from OSC V1.2) Default: None variable_declaration (VariableDeclarations): (Valid from OSC V1.2)
Ancestors
Static methods
def parse(element)
-
Expand source code
@staticmethod def parse(element): """Parses the xml element of Scenario Parameters ---------- element (xml.etree.ElementTree.Element): A Scenario element (same as generated by the class itself) Returns ------- scenario (Scenario): a Scenario object """ header = FileHeader.parse(element.find("FileHeader")) parameters = ParameterDeclarations() if element.find("ParameterDeclarations") is not None: parameters = ParameterDeclarations.parse( element.find("ParameterDeclarations") ) catalog = Catalog.parse(element.find("CatalogLocations")) storyboard = StoryBoard.parse(element.find("Storyboard")) entities = Entities.parse(element.find("Entities")) roadnetwork = RoadNetwork.parse(element.find("RoadNetwork")) variables = None if element.find("VariableDeclarations") is not None: variables = VariableDeclarations.parse(element.find("VariableDeclarations")) return Scenario( header.description, header.author, parameters, entities, storyboard, roadnetwork, catalog, header._revMinor, header.properties, variables, )
Parses the xml element of Scenario
Parameters
element (xml.etree.ElementTree.Element): A Scenario element (same as generated by the class itself)
Returns
scenario (Scenario): a Scenario object
Methods
def get_element(self)
-
Expand source code
def get_element(self): """returns the elementTree of the Scenario""" element = ET.Element( "OpenSCENARIO", attrib={ "xmlns:xsi": self._XMLNS, "xsi:noNamespaceSchemaLocation": self._XSI, }, ) element.append(self.header.get_element()) if self.parameters.get_element(): element.append(self.parameters.get_element()) element.append(self.catalog.get_element()) element.append(self.roadnetwork.get_element()) element.append(self.entities.get_element()) element.append(self.storyboard.get_element()) if self.variable_declaration: element.append(self.variable_declaration.get_element()) return element
returns the elementTree of the Scenario
def write_xml(self, filename, prettyprint=True, encoding='utf-8')
-
Expand source code
def write_xml(self, filename, prettyprint=True, encoding="utf-8"): """write_xml writes the OpenSCENARIO xml file Parameters ---------- filename (str): path and filename of the wanted xml file prettyprint (bool): pretty print or ugly print? Default: True encoding (str): specifies the output encoding Default: 'utf-8' """ printToFile(self.get_element(), filename, prettyprint, encoding)
write_xml writes the OpenSCENARIO xml file
Parameters
filename (str): path and filename of the wanted xml file prettyprint (bool): pretty print or ugly print? Default: True encoding (str): specifies the output encoding Default: 'utf-8'