Calculating angle from latitude and longitude

angle, latitude-longitude, r

Solution

Using the function `trackAzimuth` in `maptools`:

library(maptools)

trackAngle <- function(xy) {
    angles <- abs(c(trackAzimuth(xy), 0) -
                  c(0, rev(trackAzimuth(xy[nrow(xy):1, ]))))
    angles <- ifelse(angles > 180, 360 - angles, angles)
    angles[is.na(angles)] <- 180
    angles[-c(1, length(angles))]
}

The `trackAzimuth` function is a simple loop wrapper around `gzAzimuth`. See `?gzAzimuth` for references on calculating directions on the sphere.

Using your data:

x <- read.table(text = "longitude                       latitude
36.89379547                0.290166977
36.89384037                0.290194109
36.88999724                0.286821044
36.88708721                0.288339411
36.88650313                0.29010232
36.88563203                0.289939416
36.88545224                0.290924863", header = TRUE)

trackAngle(as.matrix(x))
[1]  10.12946 111.17211 135.88514  97.73801  89.74684

EDIT: I had to remove first/last angles from the function, something I was doing after the fact with this function elsewhere. Should be right now. :)

Also, the packages `adehabitatLT` and `argosfilter` contain functions to calculate track directions and angles.

Problem

I have a set of latitudes and longitudes , so this is the data for an animal as it moves in time. what i want to do is to calculate turning angle, that is by what angle it turns between every movement. so say i have point 1, point 2 and point 3 with latitude and longitude value corresponding to each point(animal moves from point 1 to point 2 to point 3 and so on) and i want to calculate the angle between these 3 points, point 2 being the middle point. what should i do? my OS is windows and i am using R for analysis. so here is my sample data: ``` longitude latitude 36.89379547 0.290166977 36.89384037 0.290194109 36.88999724 0.286821044 36.88708721 0.288339411 36.88650313 0.29010232 36.88563203 0.289939416 36.88545224 0.290924863 ``` they are in decimal degrees

Original source