Maddy
Maddy

Reputation: 363

Calculating angle from latitude and longitude

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

Upvotes: 3

Views: 6667

Answers (2)

mdsumner
mdsumner

Reputation: 29525

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.

Upvotes: 12

DarenW
DarenW

Reputation: 16926

Your data points vary over only a small range. We can look at one small patch of Earth's surface and pretend it's flat, two dimensional. You have to figure out the scale of how many km, meters, miles, whatever your favorite unit is, corresponds to one degree of latitude, and for one degree of longitude. The latter depends on latitude - it'll be the same as the scale for latitude when near the equator, but if you are standing within arm's length of the north pole, one step will take you through fifty degrees. Set up x,y coordinates where x=0 is at longitude 36.88000, and y=0 is latitude 0.29000.

So, now you have a series of (x,y) points. Take the differences from each point to the next: P2-P1, P3-P2, etc. These could be called "displacement vectors" but other terms may be used in other fields than where i'm from. Call them V1, V2, etc. Use dot products and norms: dot(V1,V2) = magnitude(V1)*magnitude(V2)*cos(a) where a is the angle by which V2 deviates from the direction of V1. Repeat for V3 and V2, and so on.

R has all the tools to do this, but I don't know enough syntax of R to give examples.

Upvotes: 0

Related Questions