我有一个 GPX file带有 GPS 轨迹。现在我想计算我走完这条赛道的距离。
计算这个的最佳方法是什么?
最佳答案
计算两点(GPX 文件中的每对航点)之间距离的传统方法是使用 Haversine 公式。
我有一个实现该算法的 SQL Server 函数。这应该很容易翻译成其他语言:
create function dbo.udf_Haversine(@lat1 float, @long1 float,
@lat2 float, @long2 float) returns float begin
declare @dlon float, @dlat float, @rlat1 float,
@rlat2 float, @rlong1 float, @rlong2 float,
@a float, @c float, @R float, @d float, @DtoR float
select @DtoR = 0.017453293
select @R = 3959 -- Earth radius
select
@rlat1 = @lat1 * @DtoR,
@rlong1 = @long1 * @DtoR,
@rlat2 = @lat2 * @DtoR,
@rlong2 = @long2 * @DtoR
select
@dlon = @rlong1 - @rlong2,
@dlat = @rlat1 - @rlat2
select @a = power(sin(@dlat/2), 2) + cos(@rlat1) *
cos(@rlat2) * power(sin(@dlon/2), 2)
select @c = 2 * atn2(sqrt(@a), sqrt(1-@a))
select @d = @R * @c
return @d
end
这将返回以英里为单位的距离。对于千米,将地球半径替换为等效的千米。
Here是更深入的解释。
编辑:此函数足够快且足够准确,可以使用邮政编码数据库进行半径搜索。它在 this site 上做得很好多年来(但现在不再如此,因为链接现在已断开)。
关于xml - 如何从 GPX 文件计算距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/569980/