Calculate the distances between subsequent 2-D coordinates using Euclidean or Great Circle distance (WGS84 ellipsoid) methods.
trackDistance(x1, y1, x2, y2, longlat = TRUE, prev = FALSE)
| x1 | trip object, matrix of 2-columns, with x/y coordinates OR a vector of x start coordinates |
|---|---|
| y1 | vector of y start coordinates, if x1 is not a matrix |
| x2 | vector of x end coordinates, if x1 is not a matrix |
| y2 | vector of y end coordinates, if x1 is not a matrix |
| longlat | if FALSE, Euclidean distance, if TRUE Great Circle distance |
| prev | if TRUE and x1 is a trip, the return value has a padded end value (\"prev\"ious), rather than start (\"next\") |
Vector of distances between coordinates.
If x1 is a trip object, arguments x2, x3, y2 are
ignored and the return result has an extra element for the start point of
each individual trip, with value 0.0.
The prev argument is ignore unless x1 is a trip.
Distance values are in the units of the input coordinate system when longlat is FALSE, and in kilometres when longlat is TRUE.
This originally used spDistsN1, then implemented the sp
gcdist source directly in R, and now uses geodist.
Original source taken from sp package, but now using Helmert from Karney (2013) see the geodist package.
d <- data.frame(x=1:10, y=rnorm(10), tms=Sys.time() + 1:10, id=gl(2, 5)) sp::coordinates(d) <- ~x+y ## this avoids complaints later, but these are not real track data (!) sp::proj4string(d) <- sp::CRS("+proj=laea +ellps=sphere", doCheckCRSArgs = FALSE) tr <- trip(d, c("tms", "id")) ## the method knows this is a trip, so there is a distance for every ## point, including 0s as the start and at transitions between ## individual trips trackDistance(tr)#> 11 1.2 1.3 1.4 1.5 21 2.7 2.8 #> 0.000000 1.807158 3.199252 4.543217 2.088737 0.000000 1.062335 1.675079 #> 2.9 2.10 #> 1.036348 1.178340## the default method does not know about the trips, so this is ##(n-1) distances between all points ## trackDistance(coordinates(tr), longlat = FALSE) ## we get NA at the start, end and at transitions between trips if (FALSE) { angles <- trackAngle(walrus818) }