简体   繁体   English

R中2个地理点之间的距离计算

[英]Distance calculation between 2 geopoints in R

I have calculated distance between 2 points on geolife dataset by using 2 methods and both are giving a different value. 我已经使用2种方法计算了Geolife数据集上2个点之间的距离,并且两者都给出了不同的值。

for the 1st method, I have used the harvesine distance formula 对于第一种方法,我使用了harvesine距离公式

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的输出 filename1的输出

Filename2 文件名2

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 请建议

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. 您需要确定最适合您的使用方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM