问题
I'm new to R programming but I'm calculating the great circle distance flown by a long haul airliner and I've tried rdist.earth()
from the Fields package, soDistsN1()
from the sp
package and other commands. Plus I've pretty much exhausted Google in my search. It's easy to do this in Matlab but I can't seem to find a way in R.
The problem is when I increase the data resolution (amount of waypoints calculated) my total distance goes haywire. I'm guessing it is because of how I summarize the total distance between waypoints. Any ideas?
An example of what I've tried:
data <- read.csv("FDM_test_Flight.csv")
library(fields)
fix <- cbind(data$LON, data$LAT)
fix_2 <- window(fix, deltat=500) # every 500th position I have 25,540 position readings
gcd <- rdist.earth(fix_2, miles=FALSE, R=6371)
sum(gcd)
回答1:
You can calculate them yourself using the formula for Great Circle distances like this...
r <- 6371 # radius of the Earth
data <- read.csv("FDM_test_Flight.csv")
# Convert to radians and make two vectors of point A and point B
x <- length(data$LON)
lon <- data$LON[1:(x-1)] * pi/180
lat <- data$LAT[1:(x-1)] * pi/180
lon2 <- data$LON[2:x] * pi/180
lat2 <- data$LAT[2:x] * pi/180
#Calculate distances
dist <- sum(acos(sin( lat ) * sin( lat2 ) + cos( lat ) * cos( lat2 ) * cos( lon2 -lon ) ) * r )
Since r
is given in Km your great circle distances will be Km.
回答2:
rdist.earth
computes the distance between all pairs, so by summing up the entire matrix you're not just getting the sum of the distances between successive observations.
library(fields)
lonlat <- matrix(-92:-80,c(1:7,6:1))
out<- rdist.earth ( ozone2$lon.lat)
sum(out[row(out)==col(out)+1]) ## add first off-diagonal
But @SimonO101's solution is more efficient: if you look at the guts of rdist.earth
you'll see that's more or less what it's doing anyway.
回答3:
if you only sum the off diagonal matrix values (size one) from your gcd matrix you should get what you need. As Ben Bolker put above, rdis.earth() calculates the distance between "all pairing". Now how it works: If you have, say, three locations: 1, 2 and 3. You need dist(1,2) + dist(2,3) the same pattern for any n points will hold. So, these info are already in the rdis.earth. Just extract what you need and sum them up. Here- (sorry for the for loop)
sum(gcd) #but this is too large, it's summing all the pairings!
limit <- dim(gcd)[1] - 1
for(i in 1 :limit ){
sum_off_dia[i]<- gcd_data[i,i+1]
}
sum(sum_off_dia)
来源:https://stackoverflow.com/questions/15569849/calculating-great-circle-distance-in-r-programming-with-high-data-resolution