Bigquery根据时间/位置数据(当前行上方/下方的行)获取速度

时间:2019-03-09 00:51:50

标签: python sql pandas google-bigquery geospatial

我在Bigquery中有一个表,其中包含Nascar驱动程序的跟踪数据(我正在从事的项目的虚拟数据)。 x和y坐标每秒获取10次。 capture_frame表示当前帧,每个连续的capture_frame应该相隔100毫秒,因为数据是每100毫秒获取一次。

我想计算每位驾驶员的每圈速度。我知道如何在熊猫中做到这一点,但我认为这在bigquery中是可能的。为了计算速度,我正在查看capture_frame之前的2行和之后的2行,然后除以时间间隔(应为400毫秒)。

这里是一个示例,其中一个车手在第一圈比赛中有几个捕捉帧。每圈有几百个捕获帧,然后还混入了20个驾驶员,但是如果我们只看一个驾驶员/赛车/圈,就更容易理解。

+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| Race | Capture | Lap | Driver | …  | X    | Y   | Epoch_time | Delta_dist  | Curr_speed  |
|      | _frame  |     |        |    |      |     |            |             |             |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 1       | 1   | Logano | …. | 2.1  | 1   | 1552089720 | NULL        | Null        |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 2       | 1   | Logano | …  | 2.2  | 1.1 | 1552089820 | NULL        | Null        |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 3       | 1   | Logano | …  | 2.22 | 1.2 | 1552089920 | 2.265921446 | 0.005664804 |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 4       | 1   | Logano | .. | 3.22 | 1.5 | 1552090020 | 3.124163888 | 0.00781041  |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 5       | 1   | Logano | .. | 4.22 | 1.8 | 1552090120 | NULL        | null        |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+
| I500 | 6       | 1   | Logano | .. | 5.22 | 1.9 | 1552090220 | NULL        | null        |
+------+---------+-----+--------+----+------+-----+------------+-------------+-------------+

第3帧的delta_distsqrt((4.22-2.1)^2 + (1.8-1)^2)/1计算,而curr_speed是该数字除以400。比赛的前/后2个距离和速度将为空,因为之前没有x或y坐标是可以的,因为从开始或停止到0.1秒钟时实际上没有任何速度。

在大熊猫中我会做(这不是很棒的代码,因为我只是让每个车手和赛车自己参加):

#laps_per_race dictionary with num laps per race
for driver in driver_list:
    for race in race_list:
        driver_race_query = “SELECT * from nascar_xyz where driver={driver} and Race={race}”.format(driver=driver, race=race)
        df_entire_race = client.query(driver_race_query).to_dataframe()
        num_laps = laps_per_race[race]
        for lap in num_laps: 
            #get subset of dataframe just for this lap 
            df = df_entire_race.loc[df_entire_race['Lap'] == lap]
            df.sort_values(‘Epoch_time’, inplace=True)
            df[‘prev_x’] = df[‘X’].shift(2)
            df[‘next_x’] = df[‘X’].shift(-2)
            df[‘prev_y’] = df[‘Y’].shift(2)
            df[‘next_y’] = df[‘Y’].shift(-2)
            #this is just distance function sqrt((x2-x1)^2 + (y2-y1)^2)
            df['delta_dist'] = np.sqrt((df[‘X’].shift(-2) - df[‘X’].shift(2))**2 + (df[‘Y’].shift(-2) - df[‘Y’].shift(2))**2))

            #400.0 is the time actual difference
            df['Curr_speed'] = df['delta_dist']/400.0

我认为在我的sql查询中,我要么必须进行分组依据,要么要按分区进行分组,因为我想按driver_id然后按圈数查看每个种族(如果抽象水平有意义)。也许是为了提高速度并提前捕获帧,我可以使用开窗(https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts)来做某事,也可以做一个叫做“滞后”的事,这似乎相当于大熊猫中的.shift()

1 个答案:

答案 0 :(得分:1)

您走对了路。我将收集在史坦顿岛周围行驶的公共汽车的公共数据集-并通过查看纬度来使用地理距离:

WITH data AS (
  SELECT bus, ST_GeogPoint(longitude, latitude) point
    , PARSE_TIMESTAMP('%Y%m%d %H%M%S',FORMAT('%i %06d', day, time)) ts
  FROM `fh-bigquery.mta_nyc_si.201410_bustime`
  WHERE day=20141014
  AND bus IN (7043, 7086, 7076, 2421, 7052, 7071)
)


SELECT * 
FROM (
  SELECT bus, ts, distance/time speed
  FROM (
    SELECT bus, ts
      , ST_DISTANCE(point, LAG(point, 3) OVER(PARTITION BY bus ORDER BY ts)) distance
      , TIMESTAMP_DIFF(ts, LAG(ts, 3) OVER(PARTITION BY bus ORDER BY ts), SECOND) time
    FROM data
  )
  WHERE time IS NOT null 
)
WHERE speed < 500

enter image description here