The trip package

Michael Sumner

2016-10-18

Introduction

Basic use of the trip package.

Data input and validation

library(trip)
## Loading required package: sp
d <- data.frame(x=1:10,y=rnorm(10), tms=Sys.time() + 1:10, id=gl(2, 5))
coordinates(d) <- ~x+y
## a projection should always be set, is it WGS84 or NAD83 . . .
proj4string(d) <- CRS("+proj=laea +ellps=sphere")
tr <- trip(d, c("tms", "id"))
summary(tr)
## 
## Object of class trip
##   tripID ("id") No.Records   startTime ("tms")     endTime ("tms")
## 1             1          5 2016-10-18 06:23:58 2016-10-18 06:24:02
## 2             2          5 2016-10-18 06:24:03 2016-10-18 06:24:07
##   tripDuration tripDistance meanSpeed maxSpeed meanRMSspeed maxRMSspeed
## 1       4 secs     5.602185  5041.967 7517.821     1051.342    4205.366
## 2       4 secs     7.342218  6607.996 7878.971     1533.334    6133.336
## 
## Total trip duration: 8 seconds (0 hours, 8 seconds)
## 
## Derived from Spatial data:
## 
## Object of class SpatialPointsDataFrame
## Coordinates:
##         min       max
## x  1.000000 10.000000
## y -1.871084  1.624625
## Is projected: TRUE 
## proj4string : [+proj=laea +ellps=sphere]
## Number of points: 10
## Data attributes:
##       tms                      id   
##  Min.   :2016-10-18 17:23:58   1:5  
##  1st Qu.:2016-10-18 17:24:00   2:5  
##  Median :2016-10-18 17:24:03        
##  Mean   :2016-10-18 17:24:03        
##  3rd Qu.:2016-10-18 17:24:05        
##  Max.   :2016-10-18 17:24:07

Simple plotting

plot(tr)
lines(tr)

Plot of a very simple trip object.

Gridding for time spent

tg <- rasterize(tr)
plot(tg, col = c("transparent", heat.colors(25)))

Plot of a rasterized version of the trip, by default this is a map of time-spent.

Walrus tracks

Plot the walrus tracks on a map.

data("walrus818")

plot(walrus818, pch = ".")
lines(walrus818)

library(maptools)
## Checking rgeos availability: TRUE
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
##  Path to GDAL shared files: /usr/share/gdal/1.11
##  Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.2-3
data(wrld_simpl)
world <- spTransform(subset(wrld_simpl, coordinates(wrld_simpl)[,2] > 0), proj4string(walrus818))
p <- par(xpd = NA, mar = rep(0.5, 4))
plot(walrus818, pch = ".")
plot(world, add = TRUE, col = "grey")
lines(walrus818)
llgridlines(walrus818); par(p)

Combine multiple sets of track data

library(diveMove)
## Loading required package: stats4
## This is diveMove 1.4.1. For overview type vignette("diveMove")
fname <- system.file(file.path("data", "sealLocs.csv"),
                       package="diveMove")
    dat <- read.table(fname, sep=";", header = TRUE, stringsAsFactors = FALSE)
    dat$class <- ordered(dat$class, c("Z", "B", "A", "0", "1", "2", "3"))
    dat$time <- as.POSIXct(strptime(dat$time, "%Y-%m-%d %H:%M:%S"), tz = "GMT")

locs <- subset(dat, !is.na(dat$lon))
trip(locs) <- c("lon", "lat", "time", "id")
proj4string(locs) <- " +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"

## merge with data from argosfilter
library(argosfilter)
data(seal)
seal$id <- "ringy2"
seal[["time"]] <- seal$dtime
seal$dtime <- NULL
## reconstruct the Argos labels
seal[["class"]] <- ordered(levels(dat$class)[factor(seal$lc, sort(unique(seal$lc)))], levels(dat$class))
seal$lc <- NULL


## what are we supposed to do with duplicated times?
##  which(!diff(seal$time) > 0)
##[1]   17  116  122 1008 1158 1231 1293 1300
##plot(seal[which(!diff(seal$time) > 0),c("lon", "lat") ])
##points(seal[1 + which(!diff(seal$time) > 0),c("lon", "lat") ], col = "red")

seal <- seal[!duplicated(seal$time), ]

## drop missing data and combine
dat <- rbind(dat[!is.na(dat$lon), ], seal[,names(dat)])
##coordinates(dat) <- c("lon", "lat")
##proj4string(dat) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")

tr <- dat
trip(dat) <- c("lon", "lat", "time", "id")
proj4string(dat) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")

library(raster)
## 
## Attaching package: 'raster'
## The following object is masked from 'package:argosfilter':
## 
##     distance
d2 <- spTransform(dat, projection(walrus818))
plot(union(extent(d2), extent(walrus818)))
lines(d2)
lines(walrus818)
plot(spTransform(subset(wrld_simpl, coordinates(wrld_simpl)[,2] > 0), projection(walrus818)), add = T, col = "grey")