R/filter_triangulations.R

Defines functions filter.triangs.tRackIT

Documented in filter.triangs.tRackIT

#' filter triangulations
#'
#' @description filters triangulations based on different filter options
#'
#'
#' @author Jannis Gottwald
#'
#'
#'
#' @param animal list, list generated by initAnimal functiom
#' @param projList list, list generated by initProject functiom
#' @param tw numeric, time window in which bearings are used for triangulation
#' @param rerange numeric, max accepted reception range of single stations, points that are farer than rerange from loudest station will be filtered out
#' @param nbi numeric, number of minimum succesul biangulations (intersections) in group
#' @param max.speed numeric, maximum accepted speed between consecutive points
#' @param tz string, timezone of data time
#' @param tw numeric, time window of triangulations
#' @param filter_speed logical, if TRUE speed filter will be applied
#' @param filter_distance,  if TRUE distance filter will be applied
#' @param filter_biang,  if TRUE biang filter will be applied
#' 
#' @export
#'



filter.triangs.tRackIT <- function(animal, projList, rerange = 400, nbi = 1, max.speed = 20, tz = "UTC", filter_speed = TRUE, filter_distance = TRUE, filter_biang = TRUE, tw = 5) {
  epsg <- projList$epsg
  trilst <- list.files(animal$path$triangulations, pattern = paste0("time_window_", tw), full.names = TRUE)

  mof <- data.table::fread(trilst[1])

  mof$timestamp <- mof$time

  mof <- as.data.frame(mof)

  # print(nrow(mof))

  ## filter
  if (filter_distance) {
    print("dist")
    mof <- mof[mof$dist_to_max_station <= rerange, ]
  }
  # print(nrow(mof))
  if (filter_biang) {
    print("biang")

    mof <- mof[mof$number_of_successfull_biangulations >= nbi, ]
  }
  # print(nrow(mof))
  if (filter_speed) {
    methods <- unique(mof$method)
    methods <- methods[!is.na(methods)]

    mof <- plyr::ldply(methods, function(m) {
      print(m)
      trp <- mof[mof$method == m, ]
      trp$id <- trp$method
      trp <- trp[!is.na(trp$x), ]
      trp$time <- as.POSIXct(trp$time, tz = tz)

      sp::coordinates(trp) <- c("x", "y")
      sp::proj4string(trp) <- sp::CRS(paste0("+init=epsg:", epsg))

      tp <- trip::trip(trp, c("time", "id"))

      summary(trp)

      tp$spd <- trip::speedfilter(tp, max.speed = max.speed)

      tp <- as.data.frame(tp)

      tp.filtered <- tp[tp$spd == TRUE, ]

      return(tp.filtered)
    })
  }

  data.table::fwrite(mof, paste0(animal$path$triangulations, "triangulations_time_window_", tw, "_filtered.csv"))
}
Nature40/tRackIT documentation built on Nov. 21, 2023, 3:43 a.m.