knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

traviz is a package to meaningfully analyze and visualize trajectories in multiple ways. To showcase some of the features of traviz, we'll use enviroCar trajectory data in Muenster, Germany. To collect enviroCar data, the envirocar-py package was used. Keep in mind traviz is not limited to vehicular trajectory data only and can work with all types of trajectory data.

library(traviz)
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)

traviz approaches to trajectories: point analysis or overall line analysis

traviz offers two approaches to dealing with trajectory data. The trajectory can either be analyzed as a whole geometry with the complete line or the trajectory can be analyzed point by point. The point by point approach will be showcased in this vignette.

Dataset: enviroCar sampled data

To explore the basics of working with trajectory data in traviz, we'll use the enviroCar data included in the package. This is included as 'ec.trj'. The dataset consists of:

ec <- read.csv("/home/john/Desktop/traviz/data/tracks.csv",header = TRUE, check.names = TRUE)
ec[1,]

enviroCar trajectory data contains geographic data with geometry points in sf format, a unique identifier for each track, time stamps, and data measurements at those timestamps.

ec$time <- gsub("T", " ", ec$time)
ec$time = as.POSIXct(ec$time)
trajectories = geodata_to_sf(ec, identifier = "track.id")

Getting the enviroCar data ready for input

Approach two: Overall line analysis

To handle trajectory data, a model similar to trajectories was followed, extending aspects the data structure to support sf. The class is called sfTrack and it stores a single trajectory and contains the timestamps, data, and geometry of thetrajectory. sfTrack is a S4 class and contains the following slots:

Similar to the Tracks class in trajectories, multiple sfTrack objects are stored under sfTracks which is a list of sfTrack objects.

Creating a sfTrack object

To create a sfTrack, use the constructor with two parameters: df: the data frame of trajectories identifier: the unique identifier for each track. In the case of enviroCar data, it is track.id

track = sfTrack(df = trajectories[1,], identifier = "track.id")
track

Creating a sfTracks object

To create a sfTracks object, combine multiple sfTrack in a list.

track1 = sfTrack(df = trajectories[63,], identifier = "track.id")
track2 = sfTrack(df = trajectories[64,], identifier = "track.id")
track3 = sfTrack(df = trajectories[65,], identifier = "track.id")
tracks = sfTracks(c(track1, track2, track3))
tracks

sfTrack and sfTracks methods

We will now showcase the methods of sfTrack and sfTracks.

Coercion to data frame

track1_df = as(track1, 'data.frame')
tracks_df = as(tracks, 'data.frame')

track1_df[1,]
tracks_df[5,]

Plotting

sfTrack and sfTracks has basic plotting features:

plot(track)
plot(tracks)

Intersection plotting is also included extending sf intersections:

intersection.sfTrack(track1, track2)


JamMurz/traviz documentation built on Sept. 11, 2020, 6:56 a.m.