R/aggregation.R

#' Add track data to ping data.
#'
#' For an input SP dataframe containing tracking data, adds the following
#' columns:
#' * _TimeAtNextPoint_ - POSIX column representing the time at the next point
#' in the track.
#' * _DistToNextPoint_ - The distance in meters to next point in the track.
#' * _TimeAtPoint_ - the time between the current point and the next point
#' in the track.
#' * _TrackID_ - An integer value identifying all points within the same track.
#' * _PointInTrack_ - An integer value incrementing per point in each track
#' * _CalcSpeed_ - The calculated speed at the point in meters/second, based
#' on the distance to the next point divided by the time to the next point.
#'
#' @rdname add_track_data_old
#' @author Bill DeVoe, \email{William.DeVoe@@maine.gov}
#' @param data **SpatialPointsDataFrame** - input data.
#' @param id **String** - column name that contains the tracking device ID,
#' used to group tracks.
#' @param timestamp **String** - POSIXct column name that contains the timestamp
#'  of tracking data for each point.
#' @param daysplit **Boolean** - If True, tracks be split at the end of each
#' calendar day.
#' @param timesplit **Optional** **Integer** - Split tracks if more than this
#' many minutes between pings.
#' @param distsplit **Optional** **Integer** - Split tracks if more than this
#' many meters between pings.
#' @return SpatialPointsDataframe of tracking data.
#' @importFrom geosphere distGeo
add_track_data_old <- function(data, id="deviceId", timestamp="updateTime",
                           daysplit=F, timesplit=NULL, distsplit=NULL) {
  if (!(id %in% names(data))) {
    stop(paste0(id," is not a column in input data."))
    }
  if (!(timestamp %in% names(data))) {
    stop(paste0(timestamp," is not a column in input data."))
    }
  if (class(data[[timestamp]]) != "POSIXct") {
    stop(paste0(timestamp," is not a POSIXct column."))
    }
  # add a column for the time at the next point
  data$TimeAtNextPoint <- strptime("01/01/2000 00:00:00",
                                   format='%m/%d/%Y %H:%M:%S')
  # add a column for the distance to the next point
  data$DistToNextPoint <- 0.0
  # add column for time in seconds until the next point
  data$TimeAtPoint <- 0.0
  # add column for trackID
  data$trackID <- 0
  # add column for point in track
  data$PointInTrack <- 0
  # Iterate through df, breaking apart trip tracks by device and day.
  # Probably an easier way to do this
  track <- 1 # Track is 1 to start
  trackpoint <- 1 # Point in track is 1 to start
  for (i in 1:nrow(data)) {
    if (identical(i,nrow) == F) { # If not the last row
      device <-data[[id]][i]
      nextdevice <- data[[id]][i+1]
      time <- data[[timestamp]][i]
      nexttime <- data[[timestamp]][i+1]
      point <- c(data$longitude[i], data$latitude[i])
      nextpoint <- c(data$longitude[i+1],data$latitude[i+1])
      dist <- geosphere::distGeo(point, nextpoint)
      day <- as.Date(data[[timestamp]][i], format="%m/%d/%Y")
      nextday <- as.Date(data[[timestamp]][i+1], format="%m/%d/%Y")
      tpoint <- difftime(nexttime, time, units="secs")
      # Check if a new track will be started
      newtrack = F
      # If the next point is a different device, new track
      if (identical(device, nextdevice) == F) {newtrack = T}
      # If splitting by day
      if (daysplit == T & identical(day, nextday) == F) {newtrack = T}
      # If splitting by time
      if (!(is.null(timesplit))) {
        if (tpoint/60 > timesplit) {newtrack = T}
      }
      # If splitting by distance
      if (!(is.null(distsplit))) {
        if (dist > distsplit) {newtrack = T}
      }
      # If continuing on the same track
      if (newtrack == F) {
        data$TimeAtNextPoint[i] <- nexttime
        data$DistToNextPoint[i] <- dist
        data$TimeAtPoint[i] <- as.double(tpoint)
        data$trackID[i] <- track
        data$PointInTrack[i] <- trackpoint
        trackpoint <- trackpoint + 1 # Increment to next point in track
      }
      else { # It is the last point in the track
        data$TimeAtNextPoint[i] <- data[[timestamp]][i]
        data$DistToNextPoint[i] <- 0
        data$TimeAtPoint[i] <- 0
        data$trackID[i] <- track
        data$PointInTrack[i] <- trackpoint
        track <- track + 1 # Increment to next trackID
        trackpoint <- 1 # Reset point in track to 1
      }
    }
    else { # The last point in the dataframe
      data$TimeAtNextPoint[i] <- data[[timestamp]][i]
      data$DistToNextPoint[i] <- 0
      data$TimeAtPoint[i] <- 0
      data$trackID[i] <- track
      data$PointInTrack[i] <- trackpoint
    }
  }
  # Add column for calculated speed (meters/second)
  data$CalcSpeed <- data$DistToNextPoint / data$TimeAtPoint
  return(data)
}
bdevoe/VesselTrackeR documentation built on June 1, 2019, 4:58 a.m.