示例#1
0
    def _identify_pattern(self, previous, current, next, cell):

        """
        Given three consecutive locations(from the same trace) identify a pair of numbers
        indicating the source_cell and the destination_cell relative the the following diagram
        where current would be 'x'.
        |---|---|---|  
        | 0 | 1 | 2 |
        |---|---|---|
        | 3 | x | 4 |
        |---|---|---|
        | 5 | 6 | 7 |
        |---|---|---|

        If a pattern is found then the current position is added to a list in the patterns dictionary
        """

        #get the total distance traveled
        total_distance = Trajectory.distance(previous.latitude, previous.longitude, current.latitude, current.longitude) + Trajectory.distance(next.latitude, next.longitude, current.latitude, current.longitude)
        in_speed = Trajectory.velocity(previous.latitude, previous.longitude, current.latitude, current.longitude, previous.time, current.time)
        out_speed = Trajectory.velocity(current.latitude, current.longitude, next.latitude, next.longitude, current.time, next.time)
        # If the distance traveled between the three points is moer than 15 mts
        if total_distance > 3 and in_speed < 30 and out_speed < 30:
            in_bearing = Trajectory.initial_heading(previous.latitude, previous.longitude, current.latitude, current.longitude)
            out_bearing = Trajectory.initial_heading(current.latitude, current.longitude, next.latitude, next.longitude)

            #Get a cardinal representation of the bearing
            in_direction = Trajectory.direction((in_bearing + 180) % 360) #reverse the bearing to get the incoming direction
            out_direction = Trajectory.direction(out_bearing)

            #Create a new key on the dictionary if the pattern hasn't been encountered before
            if  (in_direction, out_direction) not in cell.patterns:
                cell.patterns[in_direction,out_direction] = []

            cell.patterns[in_direction,out_direction].append(current)