Pointer Reader Example

Purpose: Demonstrate how to create and use a terrestrial point reader.
The basic object in tracktable is the point reader. This data structure reads tabular data from a file and saves it as points containing, at the least, an object id, timestamp, longitude, and latitude.

Imports

In [1]:
from tracktable.domain.terrestrial import TrajectoryPointReader
from tracktable.core import data_directory
import os.path
To create a point, we create a generic TrajectoryPointReader object and give it the following:
 input file - File stream connected to a data file
 delimiter - The character separating fields in the file, i.e. a csv will have ‘,’ as a delimiter
 comment character - The character marking comments in the file and will be ignored by the point reader
Note: The domain will default to terrestrial, which is what we typically use for real data.
In [2]:
data_filename = os.path.join(data_directory(), 'SampleASDI.csv')
inFile = open(data_filename, 'r')
reader = TrajectoryPointReader()
reader.input = inFile
reader.comment_character = '#'
reader.field_delimiter = ','
In order to view the points the reader has read, we iterate over the reader.
Note: Data is not actually read from file until accessed. This means that even though we have configured the reader, the reader is not finished with the input file until the points have been read below.
In [3]:
i = 10              # Used to limit how many results we see
for x in reader:    # Only need this line and the next to see all the data
    print(x)
    i-=1
    if i <=0:
        break
inFile.close()
[DAL54@ 2013-07-01 05:53:50: (-66.75, 28.25) Properties: ( )]
[UAL48@ 2013-07-01 05:53:53: (-42.1, 58.4167) Properties: ( )]
[KAL86@ 2013-07-01 05:53:53: (-74.0833, 47.4) Properties: ( )]
[BAW288@ 2013-07-01 05:53:54: (-82.9333, 45.7167) Properties: ( )]
[DAL248@ 2013-07-01 05:53:55: (-49.1667, 59.0167) Properties: ( )]
[ANZ2@ 2013-07-01 05:53:55: (-53.0667, 52.2833) Properties: ( )]
[DAL61@ 2013-07-01 05:53:56: (-66.35, 17.55) Properties: ( )]
[JBU1103@ 2013-07-01 05:53:56: (-66.55, 19.5667) Properties: ( )]
[CFG234@ 2013-07-01 05:53:56: (-67.9167, 20) Properties: ( )]
[AZA62J@ 2013-07-01 05:53:57: (-51.1, 48.7667) Properties: ( )]
So what happens in the background?
The reader has several attributes that can be set. Some of these attributes are:
object_id_column - Column in dataset holding the object id
timestamp_column - Column in dataset holding the timestamp
coordinate0 - Column in dataset holding the longitude
coordinate1 - Column in dataset holding the latitude

These four columns are required.

Note: Coordinates are referenced like a list and there are three, coordinates[0], coordinates[1], and coordinates[2] representing longitude, latitude, and z-order respectively.
In addition to these attributes, custom columns can be set such as ‘altitude’, ‘speed’, ‘airline’, etc so long as it is numeric, timestamp, or string. Any columns not given values will be assigned the default, or ‘None’.

In the next example, we set a numeric field (speed) and a string field(status) and see the results.

In [4]:
data_filename = os.path.join(data_directory(), 'SampleASDI.csv')
inFile = open(data_filename, 'r')
reader.input = inFile
reader.object_id_column = 0
reader.timestamp_column = 1
reader.coordinates[0] = 2
reader.coordinates[1] = 3
reader.set_real_field_column('speed', 4)     # The column name is 'speed' and it is located in column 4
reader.set_string_field_column('status', 9)
In [5]:
i=10
for x in reader:
    print(x)
    i-=1
    if i <= 0:
        break
[DAL54@ 2013-07-01 05:53:50: (-66.75, 28.25) Properties: ( {speed [null]: (null real)}, {status [null]: (null string)})]
[UAL48@ 2013-07-01 05:53:53: (-42.1, 58.4167) Properties: ( {speed [real]: 530}, {status [null]: (null string)})]
[KAL86@ 2013-07-01 05:53:53: (-74.0833, 47.4) Properties: ( {speed [real]: 510}, {status [null]: (null string)})]
[BAW288@ 2013-07-01 05:53:54: (-82.9333, 45.7167) Properties: ( {speed [real]: 510}, {status [null]: (null string)})]
[DAL248@ 2013-07-01 05:53:55: (-49.1667, 59.0167) Properties: ( {speed [real]: 560}, {status [null]: (null string)})]
[ANZ2@ 2013-07-01 05:53:55: (-53.0667, 52.2833) Properties: ( {speed [real]: 610}, {status [null]: (null string)})]
[DAL61@ 2013-07-01 05:53:56: (-66.35, 17.55) Properties: ( {speed [real]: 480}, {status [null]: (null string)})]
[JBU1103@ 2013-07-01 05:53:56: (-66.55, 19.5667) Properties: ( {speed [real]: 440}, {status [null]: (null string)})]
[CFG234@ 2013-07-01 05:53:56: (-67.9167, 20) Properties: ( {speed [real]: 390}, {status [null]: (null string)})]
[AZA62J@ 2013-07-01 05:53:57: (-51.1, 48.7667) Properties: ( {speed [real]: 550}, {status [null]: (null string)})]