Track-class {trajectories}R Documentation

Classes "Track", "Tracks", and "TracksCollection"

Description

Classes for representing sets of trajectory data, with attributes, for different IDs (persons, objects, etc)

Usage

  Track(track, df = fn(track), fn = TrackStats)
  Tracks(tracks, tracksData = data.frame(row.names=names(tracks)), 
  		fn = TrackSummary)
  TracksCollection(tracksCollection, tracksCollectionData,
  		fn = TracksSummary)
  TrackStats(track)
  TrackSummary(track)
  TracksSummary(tracksCollection)
  ## S4 method for signature 'Track'
x[i, j, ..., drop = TRUE]
  ## S4 method for signature 'TracksCollection'
x[i, j, ..., drop = TRUE]
  ## S4 method for signature 'Track,data.frame'
coerce(from, to)
  ## S4 method for signature 'Tracks,data.frame'
coerce(from, to)
  ## S4 method for signature 'TracksCollection,data.frame'
coerce(from, to)

Arguments

track

object of class STIDF-class, representing a single trip

df

optional data.frame with information between track points

tracks

named list with Track objects

tracksData

data.frame with summary data for each Track

tracksCollection

list, with Tracks objects

tracksCollectionData

data.frame, with summary data on tracksCollection

fn

function;

x

object of class Track etc

i

selection of spatial entities

j

selection of temporal entities (see syntax in package xts)

...

selection of attribute(s)

drop

logical

from

from

to

target class

Value

Functions Track, Tracks and TracksCollection are constructor functions that take the slots as arguments, check object validity, and compute summary statistics on the track and tracks sets.

TrackStats returns a data.frame with for each track segment the distance, duration, speed, and direction. In case data are geographical coordinates (long/lat), distance is in m, and direction is initial bearing.

TrackSummary reports for each track xmin, xmax, ymin, ymax, tmin, tmax, (number of points) n, (total) distance, and medspeed (median speed).

TracksSummary reports for each Tracks of a TracksCollection (number of tracks) n, xmin, xmax, ymin, ymax, tmin, tmin, tmax.

Objects from the Class

Objects of class Track extend STIDF-class and contain single trips or tracks, objects of class Tracks contain multiple Track objects for a single ID (person, object or tracking device), objects of class TracksCollection contain multiple Tracks objects for different IDs.

Slots of class "Track"

sp:

spatial locations of the track points, with length n

time:

time stamps of the track points

endTime:

end time stamps of the track points

data:

data.frame with n rows, containing attributes of the track points

connections:

data.frame, with n-1 rows, containing attributes between the track points such as distance and speed

Slots of class "Tracks"

tracks:

list with Track objects, of length m

tracksData:

data.frame with m rows, containing summary data for each Track object

Slots of class "TracksCollection"

tracksCollection:

list Tracks objects, of length p

tracksCollectionData:

data.frame with p rows, containing summary data for each Tracks object

Methods

[[

signature(obj = "Track"): retrieves the attribute element

[[

signature(obj = "Tracks"): retrieves the attribute element

[[

signature(obj = "TracksCollection"): retrieves the attribute element

[[<-

signature(obj = "Track"): sets or replaces the attribute element

[[<-

signature(obj = "Tracks"): sets or replaces the attribute element

[[<-

signature(obj = "TracksCollection"): sets or replaces the attribute element

$

signature(obj = "Track"): retrieves the attribute element

$

signature(obj = "Tracks"): retrieves the attribute element

$

signature(obj = "TracksCollection"): retrieves the attribute element

$<-

signature(obj = "Track"): sets or replaces the attribute element

$<-

signature(obj = "Tracks"): sets or replaces the attribute element

$<-

signature(obj = "TracksCollection"): sets or replaces the attribute element

coerce

signature(from = Track, to = data.frame) coerce to data.frame

coerce

signature(from = Tracks, to = data.frame) coerce to data.frame

coerce

signature(form = TracksCollection, to = data.frame) coerce to data.frame

plot

signature(x = "TracksCollection", y = "missing"): plots sets of sets of tracks

stplot

signature(obj = "TracksCollection"): plots sets of sets of tracks

Note

segments is a data.frame form in which track segments instead of track points form a record, with x0, y0, x1 and y1 the start and end coordinates

Author(s)

Edzer Pebesma, edzer.pebesma@uni-muenster.de

References

http://www.jstatsoft.org/v51/i07/

Examples

library(sp)
library(spacetime)
# t0 = as.POSIXct(as.Date("2013-09-30",tz="CET"))
t0 = as.POSIXct("2013-09-30 02:00:00", tz = "Europe/Berlin")
# person A, track 1:
x = c(7,6,5,5,4,3,3)
y = c(7,7,6,5,5,6,7)
n = length(x)
set.seed(131)
t = t0 + cumsum(runif(n) * 60)
crs = CRS("+proj=longlat +datum=WGS84") # longlat
stidf = STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n)))
A1 = Track(stidf)
# person A, track 2:
x = c(7,6,6,7,7)
y = c(6,5,4,4,3)
n = length(x)
t = max(t) + cumsum(runif(n) * 60)
stidf = STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n)))
A2 = Track(stidf)
# Tracks for person A:
A = Tracks(list(A1=A1,A2=A2))
# person B, track 1:
x = c(2,2,1,1,2,3)
y = c(5,4,3,2,2,3)
n = length(x)
t = max(t) + cumsum(runif(n) * 60)
stidf = STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n)))
B1 = Track(stidf)
# person B, track 2:
x = c(3,3,4,3,3,4)
y = c(5,4,3,2,1,1)
n = length(x)
t = max(t) + cumsum(runif(n) * 60)
stidf = STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n)))
B2 = Track(stidf)
# Tracks for person A:
B = Tracks(list(B1=B1,B2=B2))
Tr = TracksCollection(list(A=A,B=B))
stplot(Tr, scales = list(draw=TRUE))
stplot(Tr, attr = "direction", arrows=TRUE, lwd = 3, by = "direction")
stplot(Tr, attr = "direction", arrows=TRUE, lwd = 3, by = "IDs")
plot(Tr, col=2, axes=TRUE)
dim(Tr)
dim(Tr[2])
dim(Tr[2][1])
u = stack(Tr) # four IDs
dim(u)
dim(unstack(u, c(1,1,2,2))) # regroups to original
dim(unstack(u, c(1,1,2,3))) # regroups to three IDs
dim(unstack(u, c(1,2,2,1))) # regroups differently
as(Tr, "data.frame")[1:10,] # tracks separated by NA rows
as(Tr, "segments")[1:10,]   # track segments as records
Tr[["distance"]] = Tr[["distance"]] * 1000
Tr$distance = Tr$distance / 1000
Tr$distance
# work with custum TrackStats function:
MyStats = function(track) {
	df = apply(coordinates(track@sp), 2, diff) # requires sp
	data.frame(distance = apply(df, 1, function(x) sqrt(sum(x^2))))
}
crs = CRS(as.character(NA))
stidf = STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n)))
B2 = Track(stidf) # no longer longlat;
B3 = Track(stidf, fn = MyStats)
all.equal(B3$distance, B2$distance)

# approxTrack:
opar = par()
par(mfrow = c(1, 2))
plot(B2, ylim = c(.5, 6))
plot(B2, pch = 16, add = TRUE)
title("irregular time steps")
i = index(B2)
B3 = approxTrack(B2, seq(min(i), max(i), length.out = 50))
plot(B3, col = 'red', type = 'p', add = TRUE)
B4 = approxTrack(B2, seq(min(i), max(i), length.out = 50), FUN = spline)
plot(B4, col = 'blue', type = 'b', add = TRUE)
# regular time steps:
t = max(t) + (1:n) * 60 # regular
B2 = Track(STIDF(SpatialPoints(cbind(x,y),crs), t, data.frame(co2 = rnorm(n))))
plot(B2, ylim = c(.5, 6))
plot(B2, pch = 16, add = TRUE)
title("constant time steps")
i = index(B2)
B3 = approxTrack(B2)
plot(B3, type = 'p', col = 'red', add = TRUE)
B4 = approxTrack(B2, FUN = spline)
plot(B4, type = 'p', col = 'blue', add = TRUE)

# par(opar) # good to do, but would generate warnings
smth = function(x,y,xout,...) predict(smooth.spline(as.numeric(x), y), as.numeric(xout))
data(storms)
plot(storms, type = 'p')
storms.smooth = approxTracksCollection(storms, FUN = smth, n = 200)
plot(storms.smooth, add = TRUE, col = 'red')


[Package trajectories version 0.2-8 Index]