如何计算与 GPX 文件的距离? [英] How to calculate distance from a GPX file?

查看:17
本文介绍了如何计算与 GPX 文件的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 GPS 轨迹的 GPX 文件.现在我想计算我用这条赛道走过的距离.

I have a GPX file with a GPS track. Now I want to calculate the distance I covered with this track.

最好的计算方法是什么?

What's the best way to calculate this?

推荐答案

计算两点(GPX 文件中的每一对航点)之间距离的传统方法是使用 Haversine 公式.

The traditional way of calculating the distance between two points (each pair of waypoints in your GPX file) is with the Haversine formula.

我有一个实现该算法的 SQL Server 函数.这应该很容易翻译成其他语言:

I have a SQL Server function that implements the algorithm. This should be easy to translate into other languages:

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

这将返回以英里为单位的距离.对于千米,将地球半径替换为等效千米.

This returns the distance in Miles. For kilometers, replace the earth radius with it's km equivalent.

这里是更深入的解释.

此功能足够快且足够准确,可以使用邮政编码数据库进行半径搜索.多年来,它在 这个网站 上一直做得很好(但现在已经不行了,因为链接现在断开了).

This function is fast enough and accurate enough for doing radius searches with a ZIP code database. It has been doing a great job on this site for years (but it no longer does, as the link is broken now).

这篇关于如何计算与 GPX 文件的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆