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

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

问题描述

我有一个 GPX文件与GPS轨迹。现在我想要计算我覆盖着这条赛道的距离。

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文件航点)之间的距离的传统方法是使用半正矢公式。

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.

这里是一个更深入的解释。

Here is a more in-depth explanation.

编辑:此功能是足够快,足够精确的做半径搜索与ZIP code数据库。它一直在为本网站年(但它不再存在,作为链接现破裂)。

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天全站免登陆