Trajectory Reader Example

Copyright (c) 2014-2020 National Technology and Engineering Solutions of Sandia, LLC. Under the terms of Contract DE-NA0003525 with National Technology and Engineering Solutions of Sandia, LLC, the U.S. Government retains certain rights in this software.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Purpose: Demonstrate how to create and use a Trajectory Reader
If you have a file of already formed trajectories, you can use the Trajectory Reader to read them from file. Like the point reader, you can iterate over the trajectories you have and access the properties of the points in each trajectory.
[1]:
from tracktable.domain.terrestrial import TrajectoryReader
from tracktable.core import data_directory
import os.path

The trajectory reader is set up in a similar way as a point reader. In the example below, we give it a file containing trajectories and we use the default which is the terrestrial point reader. Terrestrial is typically used on real data. In order to access the trajectories, iterate over the reader.

[2]:
data_filename = os.path.join(data_directory(), 'SampleTrajectories.traj')
inFile = open(data_filename, 'r')
reader = TrajectoryReader()
reader.input = inFile
i = 1                                            # Used to limit the number of results we see
for x in reader:                                 # Each object in the reader is an iterator pointer here
    print(*x)
    print("\n")
    i -= 1
    if i <= 0:
        break
[TSTBOSEWR1@ 2014-07-01 15:45:43: (-71.0052, 42.3643) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:46:48: (-71.1853, 42.2738) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:47:52: (-71.3648, 42.1829) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:48:57: (-71.5439, 42.0918) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:50:01: (-71.7224, 42.0004) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:51:05: (-71.9004, 41.9087) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:52:10: (-72.0779, 41.8168) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:53:14: (-72.2549, 41.7245) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:54:19: (-72.4314, 41.632) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:55:23: (-72.6074, 41.5393) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:56:28: (-72.7828, 41.4462) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:57:32: (-72.9578, 41.3529) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:58:36: (-73.1323, 41.2594) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 15:59:41: (-73.3063, 41.1655) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 16:00:45: (-73.4797, 41.0714) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 16:01:50: (-73.6527, 40.9771) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 16:02:54: (-73.8252, 40.8825) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 16:03:59: (-73.9972, 40.7876) Properties: ( )] [TSTBOSEWR1@ 2014-07-01 16:05:03: (-74.1687, 40.6925) Properties: ( )]


Note that by iterating over the reader, you get a collection of points together as trajectories. Just like the point reader, you can edit the delimiting character and comment character as well as the column properties.