Пример #1
0
 def length(self):
     if self._length is None:
         length = 0
         for i in range(0, len(self.object) - 1, 1):
             p1 = self.point_n(i)
             p2 = self.point_n(i + 1)
             dist = spherical_distance(p1, p2)
             length += dist
         self._length = length
     return self._length
Пример #2
0
def get_sub_trajectory_keep_end(traj, length):
    """
    Cut initial part of traj such that the length is respected.
    """
    if length >= traj.length():
        return traj

    id_sub = traj.id
    object_sub = [traj.end_point()]
    vehicle_sub = traj.vehicle

    tmp_length = 0
    for i in range(len(traj) - 1, 0, -1):
        p = traj.point_n(i)
        q = object_sub[len(object_sub) - 1]
        tmp_length += spherical_distance(q, p)
        if tmp_length >= length:
            break
        object_sub.insert(0, p)

    sub_trajectory = Trajectory(id=id_sub,
                                object=object_sub,
                                vehicle=vehicle_sub)
    return sub_trajectory
Пример #3
0
def calculate_traj_approximation(traj1,
                                 traj2,
                                 pred_thr,
                                 last_prop,
                                 time_mod=86400):
    """
    Calculate the approximation between the traejctory and the routine.
    """
    res = {
        'id_t1': traj1.id,
        'id_t2': traj2.id,
        'head': None,
        'tail': None,
        'dist': float('infinity')
    }

    # lool for the closest point on routine to the last point of traj
    t_last = traj1.end_point()
    min_dist = float('infinity')
    id_min = None
    for i in range(0, len(traj2) - 1):
        p = traj2.point_n(i)
        dist = spherical_distance(p, t_last)
        if dist < min_dist:
            min_dist = dist
            id_min = i

    cp = closest_point_on_segment(traj2.point_n(id_min),
                                  traj2.point_n(id_min + 1), t_last)

    # calcualte the distance between the two closest points
    dist = spherical_distance(cp, t_last)
    if last_prop == 0.0 and dist >= pred_thr:
        return res

    # cut the routine temporally from the beginning of traj to the time of the closest point
    t2 = cp[2] / 1000 % time_mod
    t1 = traj2.start_point()[2] / 1000 % time_mod
    traj2_cut = get_sub_trajectory(traj2, t1, t2)
    if traj2_cut is None or len(traj2_cut) < 3:
        return res

    # if the trajectory is shorter than the routine cut remove the initial part of the routine_cut
    if traj1.length() < traj2_cut.length():
        traj2_cut = get_sub_trajectory_keep_end(traj2_cut, traj1.length())

    # calculate the tail
    traj2_head = traj2_cut

    traj2_tail = get_sub_trajectory(traj2, t2,
                                    traj2.end_point()[2] / 1000 % time_mod)

    if traj2_tail is None:
        traj2_tail = Trajectory(id=traj2.id,
                                object=[traj2.end_point()],
                                vehicle=traj2.vehicle)

    traj2_tail.object.insert(0, traj2_cut.end_point())

    if last_prop > 0.0:
        last_traj = get_sub_trajectory_keep_end(traj1,
                                                traj1.length() * last_prop)
        last_routine_head = get_sub_trajectory_keep_end(
            traj2_head,
            traj2_head.length() * last_prop)
        if len(last_traj) >= 2 and len(last_routine_head) >= 2:
            dist = trajectory_distance(last_traj, last_routine_head)
            if dist >= pred_thr:
                return res

    res['head'] = traj2_head
    res['tail'] = traj2_tail
    res['dist'] = dist

    return res
Пример #4
0
def segment_trajectories(alltraj,
                         uid,
                         temporal_thr=120,
                         spatial_thr=50,
                         max_speed=0.07):
    # temporal_thr = 120 # seconds
    # spatial_thr = 50 # meters
    # max_speed = 0.07 # km/s
    spatial_thr = spatial_thr / 1000

    #traj_list = list()
    traj_list = dict()

    tid = 0
    traj = list()
    is_a_new_traj = True
    p = None
    length = 0.0
    ref_p = None  # for stop detection
    first_iteration = True

    p_index = 0
    next_p_index = 0
    ref_p_index = 0

    for i in range(0, len(alltraj)):

        next_p = alltraj[i]
        next_p_index = i

        if first_iteration:  # first iteration
            p = next_p
            p_index = next_p_index
            ref_p = p  # for stop detection
            traj = [p]
            length = 0.0
            is_a_new_traj = True
            first_iteration = False
        else:  # all the others
            spatial_dist = spherical_distance(p, next_p)
            temporal_dist = next_p[2] - p[2]

            ref_distance = spherical_distance(ref_p,
                                              next_p)  # for stop detection
            ref_time_diff = next_p[2] - ref_p[2]  # for stop detection

            # Ignore extreme jump (with speed > 250km/h = 0.07km/s)
            if ref_distance > max_speed * ref_time_diff:
                continue

            if temporal_dist > temporal_thr or (ref_time_diff > temporal_thr
                                                and
                                                ref_distance < spatial_thr):
                # ended trajectory (includes case with long time gap)
                if len(traj) > 1 and not is_a_new_traj:
                    start_time = traj[0][2]
                    end_time = traj[-1][2]
                    duration = end_time - start_time
                    trajToInsert = Trajectory(id=tid,
                                              object=traj,
                                              vehicle=uid,
                                              length=length,
                                              duration=duration,
                                              start_time=start_time,
                                              end_time=end_time)
                    #traj_list.append(trajToInsert)
                    traj_list[tid] = trajToInsert

                # Create a new trajectory
                traj = [
                    p[:2] + [next_p[2]]
                ]  # 1st fake point with last position previous traj and new timestamp
                p = next_p
                p_index = next_p_index
                ref_p = p  # for stop detection
                ref_p_index = p_index
                traj.append(p)
                length = 0.0
                tid += 1
                is_a_new_traj = True
            else:
                if is_a_new_traj and len(traj) > 1:
                    traj[1] = [
                        traj[1][0], traj[1][1], traj[0][2] + int(
                            (next_p[2] - traj[0][2]) / 2)
                    ]
                is_a_new_traj = False

                p = next_p
                p_index = next_p_index
                if ref_distance > spatial_thr:
                    ref_p = p  # for stop detection
                    ref_p_index = p_index
                traj.append(p)
                length += spatial_dist

    if len(traj) > 1 and not is_a_new_traj:
        start_time = traj[0][2]
        end_time = traj[-1][2]
        duration = end_time - start_time
        trajToInsert = Trajectory(id=tid,
                                  object=traj,
                                  vehicle=uid,
                                  length=length,
                                  duration=duration,
                                  start_time=start_time,
                                  end_time=end_time)
        #traj_list.append(trajToInsert)
        traj_list[tid] = trajToInsert

    return traj_list
Пример #5
0
def segment_trajectories_random(alltraj,
                                uid,
                                nbr_traj_min=None,
                                nbr_traj_max=None,
                                nbr_traj=None):
    nbr_traj_min = 2 if nbr_traj_min is None else nbr_traj_min
    nbr_traj_max = int(len(alltraj) /
                       2) if nbr_traj_max is None else nbr_traj_max

    nbr_traj = np.random.randint(nbr_traj_min, nbr_traj_max +
                                 1) if nbr_traj is None else nbr_traj
    new_traj_marker = int(len(alltraj) / nbr_traj)

    traj_list = list()

    tid = 0
    traj = list()
    is_a_new_traj = True
    p = None
    first_iteration = True
    length = 0.0

    for i in range(0, len(alltraj)):

        next_p = alltraj[i]

        if first_iteration:  # first iteration
            p = next_p
            traj = [p]
            length = 0.0
            is_a_new_traj = True
            first_iteration = False
        else:  # all the others
            spatial_dist = spherical_distance(p, next_p)

            if i % new_traj_marker == 0:
                if len(traj) > 1 and not is_a_new_traj:
                    start_time = traj[0][2]
                    end_time = traj[-1][2]
                    duration = end_time - start_time
                    traj_list.append(
                        Trajectory(id=tid,
                                   object=traj,
                                   vehicle=uid,
                                   length=length,
                                   duration=duration,
                                   start_time=start_time,
                                   end_time=end_time))

                # Create a new trajectory
                p = next_p
                traj = [p]
                length = 0.0
                tid += 1
                is_a_new_traj = True
            else:
                is_a_new_traj = False
                p = next_p
                traj.append(p)
                length += spatial_dist

    if len(traj) > 1 and not is_a_new_traj:
        start_time = traj[0][2]
        end_time = traj[-1][2]
        duration = end_time - start_time
        traj_list.append(
            Trajectory(id=tid,
                       object=traj,
                       vehicle=uid,
                       length=length,
                       duration=duration,
                       start_time=start_time,
                       end_time=end_time))

    return traj_list