Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the bearing between more than two data points

I have some tracking data and I want to calculate the bearing over the course of the track. For two points we can use function from the fossil package:

# earth.bear(long1, lat1, long2, lat2)
earth.bear(-10.54427, 52.11112, -10.55493, 52.10944)
# 255.6118

However, this won't work for more than two points. Here's some sample data:

tracks <- read.table(text = 
"latitude,  longitude
52.111122,  -10.544271
52.10944,   -10.554933
52.108898,  -10.558025
52.108871,  -10.560946
52.113991,  -10.582005
52.157223,  -10.626506
52.194977,  -10.652878
52.240215,  -10.678817
52.26421,   -10.720366
52.264015,  -10.720642", header = TRUE, sep = ",")
like image 738
adkane Avatar asked Sep 26 '22 08:09

adkane


1 Answers

Try this:

sum(
  sapply(1:(nrow(tracks) - 1), function(i){
    earth.bear(tracks$longitude[i], tracks$latitude[i],
               tracks$longitude[i+1], tracks$latitude[i+1] )

  })
)

# 2609.871
like image 133
zx8754 Avatar answered Sep 28 '22 07:09

zx8754