Tutorial 4: How to Read .traj Files

Purpose

This notebook demonstrates how to read a .traj file with Tracktable. Traj files are often used to store trajectories, as Tracktable is able to read them more quickly than delimited files. To learn how to create a .traj file, see See Tutorial 3.

Step 1: Start with a .traj file.

We will use the provided example data \(^1\) for this tutorial, which has already been compressed into a .traj file for us.

In [1]:
from tracktable_data.data import retrieve

traj_filename = retrieve(filename='NYHarbor_2020_06_30_first_hour.traj')

Step 2: Read trajectories from a file.

We will use the provided example data \(^1\) for this tutorial. As with the point reader and trajectory builder, the .traj file reader provides its output as an iterable. We pull the contents of that iterable into a list for our convenience.

In [2]:
from tracktable.domain.terrestrial import TrajectoryReader

with open(traj_filename, 'r') as traj_file:
    # create a Tracktable TrajectoryReader object
    reader = TrajectoryReader()
    # tell it where to find the traj file
    reader.input = traj_file
    # import the list of trajectories
    trajectories = list(reader)

Did it work?

Let’s print the number of trajectories, and the info for the first trajectory in our list, just to be sure.

In [3]:
len(trajectories)
Out[3]:
279
In [4]:
trajectory = trajectories[0]

object_id      = trajectory.object_id
trajectory_id  = trajectory.trajectory_id

print(f'Object ID: {object_id}')
print(f'Trajectory ID: {trajectory_id}\n')
Object ID: 367109000
Trajectory ID: 367109000_20200630000104_20200630002505

\(^1\) Bureau of Ocean Energy Management (BOEM) and National Oceanic and Atmospheric Administration (NOAA). MarineCadastre.gov. AIS Data for 2020. Retrieved February 2021 from marinecadastre.gov/data. Trimmed down to the first hour of June 30, 2020, restricted to in NY Harbor.