Learn R Programming

trajr (version 1.0.0)

TrajFromCoords: Create a Trajectory Object

Description

TrajFromCoords creates a new trajectory object from a set of 2-dimensional cartesian coordinates, times and some metadata. The coordinates are sometimes referred to as "relocations".

Usage

TrajFromCoords(track, xCol = 1, yCol = 2, timeCol = NULL, fps = 50,
  spatialUnits = "m", timeUnits = "s")

Arguments

track

data frame containing cartesian coordinates and optionally times for the points in the trajectory.

xCol

Name or index of the x column in track (default 1).

yCol

Name or index of the y column in track (default 2).

timeCol

optional name or index of the column which contains coordinate times.

fps

Frames per second - used to calculate relative coordinate times if track does not contain a time column. Time intervals between coordinate are assumed to be constant throught the entire track.

spatialUnits

Abbreviation for the x and y units.

timeUnits

Abbreviation for the units that time is recorded in.

Value

An object with class "Trajectory", which is a data.frame with the following components:

x

X coordinates of trajectory points.

y

Y coordinates of trajectory points.

time

Time (in timeUnits) for each point. if timeCol is specified, values are trj[,timeCol], otherwise values are calculated from fps.

displacementTime

Frame times, with frame 1 at time 0.

polar

Coordinates represented as complex numbers, to simplify working with segment angles.

displacement

Displacements between each pair of consecutive points.

Details

If timeCol is specified, track[,timeCol] is expected to contain the time (in seconds) of each coordinate. Otherwise, times are calculated for each point as (coord - 1) / fps where coord is the index of the point; in other words, sampling at constant time intervals is assumed.

x and y must be square units. Longitude and latitude are not suitable for use as x and y values, since in general, 1<U+00B0> lat != 1<U+00B0> lon. To create a trajectory from positions in latitude and longitude, it is first necessary to transform the positions to a suitable spatial projection such as UTM (possibly by using spTransform from the rgdal package).

Examples

Run this code
# NOT RUN {
coords <- data.frame(x = c(1, 1.5, 2, 2.5, 3, 4),
                     y = c(0, 0, 1, 1, 2, 1),
                     times = c(0, 1, 2, 3, 4, 5))
trj <- TrajFromCoords(coords)

par(mar = c(4, 4, 0.5, 0.5) + 0.1)
plot(trj)

# }

Run the code above in your browser using DataLab