0

I have calculated distance between 2 points on geolife dataset by using 2 methods and both are giving a different value.

for the 1st method, I have used the harvesine distance formula

below is the code:

for(i in 905:921){
geodistt[i] <- distm (c(lon1=filename$Longitude[i-1], lat1=filename$Latitude[i-1]), c(lon2=filename$Longitude[i], lat2=filename$Latitude[i]), fun = distHaversine)
}
filename2 <- data.frame(filename,tdiff,geodist)

for the 2nd method, I have used below code and considered Altitude,

for(i in 905:921){
Vincenty <- distance(lat1=filename$Latitude[i-1],lon1=filename$Longitude[i-1],lat2=filename$Latitude[i],lon2=filename$Longitude[i])
DirectDistance <- as.numeric(Vincenty[5]) # the fifth element of the output frame is the distance between the points.

#To be more accurate, we also take into account difference in altitude between the points
AltitudeChange <- abs(filename$Altitude[i]-filename$Altitude[i-1])
if(AltitudeChange!=0)
  geodistt[i] <- sqrt(DirectDistance^2+AltitudeChange^2)
else 
  geodistt[i] <- DirectDistance
}
filename1 <- data.frame(filename,tdiff,geodistt)
 print(filename1)

Output for filename2 output for filename1

Filename2

both the outputs are different.

Outputs are attached.

Could you please suggest which method I should use. Do I need to consider Altitude. Please suggest

  • Welcome to SO! Here's how you can share a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Karthik Arumugham Feb 20 '17 at 02:15
  • The `distm()` operation is vectorised. So you don't need forloops. You may want to try posting it on [Geographic Information Systems](http://gis.stackexchange.com) forum to get a recommendation on your approach. – Karthik Arumugham Feb 20 '17 at 02:15

1 Answers1

0

It depends on why you are doing what you are doing. If the straight line distance from a position on a one map spot to the other as a bird flies matters, then you want the one that includes the change in elevation.

It is basically giving you the hypotenuse of a right triangle from the lower point to the spot on the map of the second, then using the altitude to do some fancy math and give you the straight line path from point to point, including the rise.

If all you are interested is the approximate sea-level distance from point to point as if the were drawn on a textureless ball, use the other method.

Each has its strengths and weaknesses. You need to decide what works best for your use.

sconfluentus
  • 4,693
  • 1
  • 21
  • 40