|
| 1 | +from pytrack.graph import distance |
| 2 | + |
| 3 | + |
| 4 | +def veldist_filter(traj, th_dist=5, th_vel=3): |
| 5 | + """ It filters the GPS trajectory combining speed and distance between adjacent points. |
| 6 | + If the adjacent point distance does not exceed the threshold and the speed is less than th_vel (m/s), the current |
| 7 | + trajectory point is ignored. |
| 8 | +
|
| 9 | + Parameters |
| 10 | + ---------- |
| 11 | + traj: pandas.DataFrame |
| 12 | + Dataframe containing 3 columns [timestamp, latitude, longitude]. |
| 13 | + th_dist: float, optional, default: 5 meters. |
| 14 | + Threshold for the distance of adjacent points. |
| 15 | + th_vel: float, optional, default: 3 m/s. |
| 16 | + Threshold for the velocity. |
| 17 | +
|
| 18 | + Returns |
| 19 | + ------- |
| 20 | + df: pandas.DataFrame |
| 21 | + Filtered version of the input dataframe. |
| 22 | + """ |
| 23 | + |
| 24 | + df = traj.copy() |
| 25 | + |
| 26 | + i = 0 |
| 27 | + while True: |
| 28 | + if i == df.shape[0]-1: |
| 29 | + break |
| 30 | + deltat = (df["datetime"][i+1]-df["datetime"][i]).total_seconds() |
| 31 | + dist = distance.haversine_dist(*tuple(df.iloc[i, [1, 2]]), *tuple(df.iloc[i+1, [1, 2]])) |
| 32 | + |
| 33 | + if dist < th_dist and dist/deltat < th_vel: |
| 34 | + df.drop([i+1], inplace=True) |
| 35 | + df.reset_index(drop=True, inplace=True) |
| 36 | + else: |
| 37 | + i += 1 |
| 38 | + |
| 39 | + return df |
| 40 | + |
| 41 | + |
| 42 | +def park_filter(traj, th_dist=50, th_time=30): |
| 43 | + """ It removes parking behaviour by eliminating those points that remain in a certain area |
| 44 | + for a given amount of time. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + traj: pandas.DataFrame |
| 49 | + Dataframe containing 3 columns [timestamp, latitude, longitude]. |
| 50 | + th_dist: float, optional, default: 50 meters. |
| 51 | + Threshold for the distance of adjacent points. |
| 52 | + th_time: float, optional, default: 30 min. |
| 53 | + Threshold for the delta time. |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + df: pandas.DataFrame |
| 58 | + Filtered version of the input dataframe. |
| 59 | + """ |
| 60 | + |
| 61 | + df = traj.copy() |
| 62 | + |
| 63 | + i = 0 |
| 64 | + while True: |
| 65 | + if i == df.shape[0]-1: |
| 66 | + break |
| 67 | + deltat = (df["datetime"][i+1]-df["datetime"][i]).total_seconds() |
| 68 | + deltad = distance.haversine_dist(*tuple(df.iloc[i, [1, 2]]), *tuple(df.iloc[i+1, [1, 2]])) |
| 69 | + |
| 70 | + if deltad < th_dist and deltat > th_time: |
| 71 | + df.drop([i+1], inplace=True) |
| 72 | + df.reset_index(drop=True, inplace=True) |
| 73 | + else: |
| 74 | + i += 1 |
| 75 | + |
| 76 | + return df |
| 77 | + |
0 commit comments