Exemplo n.º 1
0
    def createLanesForConnectionRoad(self, connectionRoad: ExtendedRoad, 
                                    predRoad: ExtendedRoad, 
                                    sucRoad: ExtendedRoad, 
                                    strategy = LaneConfigurationStrategies.MERGE_EDGE, 
                                    countryCode=extensions.CountryCodes.US):
        """Assumes start of connection road is connected to predRoad and end to sucRoad and connection road's lanes are not connected to either of the roads.
        It can connect roads with two different lane configurations.

        Args:
            connectionRoad (ExtendedRoad): 
            predRoad (ExtendedRoad): Extended predecessor road of connectionRoad. That means connection road's start is connected to predRoad
            sucRoad (ExtendedRoad): Extended successor road of connectionRoad. That means connection road's end is connected to sucRoad
            strategy ([type], optional): [description]. Defaults to LaneConfigurationStrategies.MERGE_EDGE.
        """


        try:
            cp1, cp1Con = RoadLinker.getContactPoints(predRoad, connectionRoad)
            cp2, cp2Con = RoadLinker.getContactPoints(sucRoad, connectionRoad)

            laneSection1 = predRoad.getLaneSectionByCP(cp1)
            laneSection2 = sucRoad.getLaneSectionByCP(cp2)

            connectionRoad.clearLanes()

            leftConnections, rightConnections = LaneConfiguration.getLaneLinks(laneSection1, laneSection2, (cp1 == cp2), strategy)

            # now we need to workout the number of straight lanes, merge lanes, and turn lanes on each side.


            # switch lane sides if cp1 and cp1Con are the same, because the lane orientation is reversed

            if cp1 == cp1Con:
                leftNumStandard, leftNumMerge, leftNumTurn = LaneConfiguration.getNumberDifferentLanes(rightConnections)
                rightNumStandard, rightNumMerge, rightNumTurn = LaneConfiguration.getNumberDifferentLanes(leftConnections)
            else:
                leftNumStandard, leftNumMerge, leftNumTurn = LaneConfiguration.getNumberDifferentLanes(leftConnections)
                rightNumStandard, rightNumMerge, rightNumTurn = LaneConfiguration.getNumberDifferentLanes(rightConnections)


            connectionRoad.lanes = self.getLanes(n_lanes_left=leftNumStandard, n_lanes_right=rightNumStandard,
                                 roadLength=connectionRoad.length(),
                                 numLeftTurnsOnLeft=leftNumTurn, numLeftMergeOnLeft=leftNumMerge,
                                 numRightTurnsOnRight= rightNumTurn, numRightMergeOnRight=rightNumMerge)





        
        except Exception as e:
            raise e
Exemplo n.º 2
0
    def _create_links_connecting_road(self,
                                      connecting: ExtendedRoad,
                                      road: ExtendedRoad,
                                      ignoreMismatch=True):
        """ _create_links_connecting_road will create lane links between a connecting road and a normal road

            Parameters
            ----------
                connecting (Road): a road of type connecting road (not -1)

                road (Road): a that connects to the connecting road

        """
        linktype, sign, connecting_lanesec = self._get_related_lanesection(
            connecting, road)
        _, _, road_lanesection_id = self._get_related_lanesection(
            road, connecting)

        # invert lanes if contact points are the same
        try:
            roadCp, conCp = RoadLinker.getContactPoints(road, connecting)
        except:
            # lane linking not possible because they are not neighbours
            return

        if roadCp == conCp:
            logging.debug(
                f"{self.name}: switching lane sides for {connecting.id} and {road.id}"
            )

        if connecting_lanesec != None:
            laneSectionForConnection = connecting.lanes.lanesections[
                connecting_lanesec]
            laneSectionForRoad = road.lanes.lanesections[road_lanesection_id]
            if laneSectionForConnection.leftlanes:
                # do left lanes
                connectionLanes = laneSectionForConnection.leftlanes
                roadLanes = laneSectionForRoad.leftlanes

                if roadCp == conCp:
                    roadLanes = laneSectionForRoad.rightlanes

                if len(connectionLanes) == len(roadLanes):
                    for i in range(len(roadLanes)):
                        linkid = roadLanes[i].lane_id
                        connectionLanes[i].add_link(linktype, linkid)
                elif ignoreMismatch:
                    # raise NotImplementedError()
                    # logging.warn(f"number of left lanes are not the same for {connecting.id} and {road.id}")
                    self.connectMinLanesOnOneSide(connectionLanes, roadLanes,
                                                  linktype, None)

                else:
                    raise NotSameAmountOfLanesError(
                        'Connecting road ', connecting.id, ' and road ',
                        road.id, 'do not have the same number of left lanes.')
            if laneSectionForConnection.rightlanes:
                # do right lanes
                connectionLanes = laneSectionForConnection.rightlanes
                roadLanes = laneSectionForRoad.rightlanes

                if roadCp == conCp:
                    roadLanes = laneSectionForRoad.leftlanes

                if len(connectionLanes) == len(roadLanes):
                    for i in range(len(roadLanes)):
                        linkid = roadLanes[i].lane_id
                        connectionLanes[i].add_link(linktype, linkid)
                elif ignoreMismatch:
                    # raise NotImplementedError()
                    # logging.warn(f"number of left lanes are not the same for {connecting.id} and {road.id}")
                    self.connectMinLanesOnOneSide(connectionLanes, roadLanes,
                                                  linktype, None)
                else:
                    raise NotSameAmountOfLanesError(
                        'Connecting road ', connecting.id, ' and road ',
                        road.id, 'do not have the same number of right lanes.')
Exemplo n.º 3
0
    def _create_links_roads(self,
                            pre_road: ExtendedRoad,
                            suc_road: ExtendedRoad,
                            ignoreMismatch=True):
        """ _create_links_roads takes two roads and connect the lanes with links, if they have the same amount. 

            Parameters
            ----------
                pre_road (Road): the predecessor road 

                suc_road (Road): the successor road

        """
        # invert lanes if contact points are the same
        try:
            roadCp, conCp = RoadLinker.getContactPoints(pre_road, suc_road)
        except:
            # lane linking not possible because they are not neighbours
            return

        if roadCp == conCp:
            logging.debug(
                f"{self.name}: switching lane sides for {pre_road.id} and {suc_road.id}"
            )

        pre_linktype, pre_sign, pre_connecting_lanesec = self._get_related_lanesection(
            pre_road, suc_road)
        suc_linktype, suc_sign, suc_connecting_lanesec = self._get_related_lanesection(
            suc_road, pre_road)
        preLaneSection = pre_road.lanes.lanesections[pre_connecting_lanesec]
        # TODO it may be wrong. shouldn't it be suc_connecting_lanesec
        # sucLaneSection = suc_road.lanes.lanesections[-1]
        sucLaneSection = suc_road.lanes.lanesections[suc_connecting_lanesec]

        # left
        preLanes = preLaneSection.leftlanes
        sucLanes = sucLaneSection.leftlanes
        if roadCp == conCp:
            sucLanes = sucLaneSection.rightlanes

        if len(preLanes) == len(sucLanes):
            for i in range(len(preLanes)):
                preLanes[i].add_link(pre_linktype, sucLanes[i].lane_id)
                sucLanes[i].add_link(suc_linktype, preLanes[i].lane_id)

        elif ignoreMismatch:

            # logging.warn(f"number of left lanes are not the same for {pre_road.id} and {suc_road.id}")
            self.connectMinLanesOnOneSide(preLanes, sucLanes, pre_linktype,
                                          suc_linktype)

        else:
            raise NotSameAmountOfLanesError(
                'Road ' + str(pre_road.id) + ' and road ' + str(suc_road.id) +
                ' does not have the same number of right lanes.')

        #right
        preLanes = preLaneSection.rightlanes
        sucLanes = sucLaneSection.rightlanes
        if roadCp == conCp:
            sucLanes = sucLaneSection.leftlanes

        if len(preLanes) == len(sucLanes):
            for i in range(len(preLanes)):
                preLanes[i].add_link(pre_linktype, sucLanes[i].lane_id)
                sucLanes[i].add_link(suc_linktype, preLanes[i].lane_id)

        elif ignoreMismatch:

            # logging.warn(f"number of left lanes are not the same for {pre_road.id} and {suc_road.id}")

            self.connectMinLanesOnOneSide(preLanes, sucLanes, pre_linktype,
                                          suc_linktype)

        else:
            raise NotSameAmountOfLanesError(
                'Road ' + str(pre_road.id) + ' and road ' + str(suc_road.id) +
                ' does not have the same number of right lanes.')