xml - 如何从 GPX 文件计算距离?

标签 xml algorithm gps gpx

我有一个 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/

相关文章:

javascript - 在javascript中连接4算法

algorithm - 冒泡排序和 gnome 排序的区别

xml - 使用 xsl 模板参数作为选择中的节点(即元素的值)

c - 将 XML 数据导入 Access 时出现问题

python - 在 Django 中序列化 ModelForm 对象的最佳方法是什么?

oop - 是否有通用/标准/接受的方式来建模 GPS 实体(航点、轨迹)?

google-maps - 从谷歌地图或其他 map 提供商获取移动应用程序中给定路线的收费数据

xml - 在 HTTP 查询中为 XML 文档指定自定义 XSL 文件

algorithm - 人工智能 : selecting immediate acceleration/rotation to get to a final point

blackberry - LocationUpdate 方法在 Blackberry 上调用过于频繁