Skip to content
Prev 2591 / 29559 Next

LL from bearing and distance

The following function does what Eric describes, if the earth's surface 
is close enough to a sphere.
Roger, I'll try to document it properly for inclusion in Maptools.

Cheers, Arien




# compute the place where you end up, if you travel
# a certain distance along a great circle,
# which is uniquely defined by a point (your starting point)
# and an angle with the meridian at that point (your direction).
# the travelvector is a dataframe with at least the columns
# magnitude and direction.
# n.b. earth radius is the "ellipsoidal quadratic mean radius"
# of the earth, in m.

vectordestination <- function(lonlatpoint, travelvector) {
      Rearth <- 6372795
      Dd <- travelvector$magnitude / Rearth
      Cc <- travelvector$direction

      if (class(lonlatpoint) == "SpatialPoints") {
          lata <- coordinates(lonlatpoint)[1,2] * (pi/180)
          lona <- coordinates(lonlatpoint)[1,1] * (pi/180)
      }
      else {
          lata <- lonlatpoint[2] * (pi/180)
          lona <- lonlatpoint[1] * (pi/180)
      }
      latb <- asin(cos(Cc) * cos(lata) * sin(Dd) + sin(lata) * cos(Dd))
      dlon <- atan2(cos(Dd) - sin(lata) * sin(latb), sin(Cc) * sin(Dd) * 
cos(lata))
      lonb <- lona - dlon + pi/2

      lonb[lonb >  pi] <- lonb[lonb >  pi] - 2 * pi
      lonb[lonb < -pi] <- lonb[lonb < -pi] + 2 * pi

      latb <- latb * (180 / pi)
      lonb <- lonb * (180 / pi)

      cbind(longitude = lonb, latitude = latb)
}
Roger Bivand wrote: