-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.py
58 lines (46 loc) · 1.87 KB
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import math
def find(pedestrian_df, vehicle_df):
# Calculate distance between current and last x, y coordinates for each track_id
last_x, last_y, last_track_id = {}, {}, None
distances = []
for index, row in vehicle_df.iterrows():
track_id = row['track_id']
x = row['x']
y = row['y']
# Check if track_id has a previous row
if last_track_id is None or last_track_id != track_id:
last_x[track_id] = x
last_y[track_id] = y
distances.append(np.nan)
else:
# Calculate distance
distance = math.sqrt((x - last_x[track_id])**2 + (y - last_y[track_id])**2)
distances.append(distance)
last_x[track_id] = x
last_y[track_id] = y
last_track_id = track_id
# Add distances to DataFrame
vehicle_df['distance'] = distances
# Calculate distance between current and last x, y coordinates for each track_id
last_x, last_y, last_track_id = {}, {}, None
distances = []
for index, row in pedestrian_df.iterrows():
track_id = row['track_id']
x = row['x']
y = row['y']
# Check if track_id has a previous row
if last_track_id is None or last_track_id != track_id:
last_x[track_id] = x
last_y[track_id] = y
distances.append(np.nan)
else:
# Calculate distance
distance = math.sqrt((x - last_x[track_id])**2 + (y - last_y[track_id])**2)
distances.append(distance)
last_x[track_id] = x
last_y[track_id] = y
last_track_id = track_id
# Add distances to DataFrame
pedestrian_df['distance'] = distances
return pedestrian_df, vehicle_df