R/distance.R

Defines functions distance set_affichage `+.distance` print.distance

#' @importFrom geosphere distm distHaversine
distance <- function(lon1, lat1, lon2, lat2) {
  d <-  geosphere::distm(c(lon1, lat1), c(lon2, lat2), fun = geosphere::distHaversine)
  
  list(affichage = set_affichage(d),valeur = d)
}


set_affichage <- function(d) {
  class(d) <- "distance"
  if (d > 500) {
    attr(d, "affichage") <- paste(round(d / 1000, 1), "km")
  } else {
    attr(d, "affichage") <- paste(round(d, 1), "m")
  }
  d
}

`+.distance` <- function(x, y) {
  set_affichage(unclass(x) + unclass(y))
}

print.distance <- function(x, ...) {
  print(attributes(x)$affichage)
}
VincentGuyader/locate documentation built on Dec. 18, 2019, 2:54 a.m.