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)

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("../data/tracks.csv", header = 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.

Approach one: point data analysis

We begin by making sure the imported data is in a readable format for traviz. To do this we make first make some minor modifications to the csv and using the 'geodata_to_sf' function to convert the data to sf.

ec$time <- gsub("T", " ", ec$time)
trajectories <- geodata_to_sf(ec, "track.id")
trajectories = trajectories[43:63,]
trajectories_unnested = trajectories %>% unnest #remove nesting for value analysis

For speed we will only use 20 trajectories. The data is now ready to use and we can begin expirementing with traviz's methods for individual point analysis. Clear ggplot visualization of the trajectories (with normal lonlat projection) is now possible:

ggplot(trajectories) + geom_sf(aes(color = track.id)) + theme(legend.position = "none")
#Note: legend is hidden to reduce clutter

Point data analysis is now possible

Raster data

traviz can rasterize data to a desired resolution and provide visualization of this data for a value. We will take a look at the speed values of cars in this subset of trajectories:

library(raster)
rastered_trajectories = sf_to_rasterize(trajectories_unnested, data = 'Speed.value', resolution = .0007)
rastered_trajectories
plot(rastered_trajectories)

Heatmap of trajectory values

density_heatmap(trajectories_unnested, 'Speed.value', resolution = .0007)

It appears that cars seem to move faster at the bottom left and bottom right of this map of Muenster based off the heatmap density.

k-means clustering

traviz supports k-mean clustering. In future development, there will be point by point clustering.

clusters <- cluster_traj(trajectories, num_clusters = 4)
plot(clusters)

Aggregation tools for point data

traviz supports multiple forms of trajectory aggregation. Below is a showcase of a couple of aggregation methods.

Raster aggregation:

plot(aggregate_raster_region(rastered_trajectories, xmin = 7.62, xmax = 7.63, ymin = 51.93, ymax = 51.94))

Trajectories by day and by hour:

plot_day(trajectories_unnested)
plot_hour(trajectories_unnested)

Trajectory density by day and by hour:

plot_day_density(df = trajectories_unnested)
plot_hour_density(df = trajectories_unnested)


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