How to calculate distance from a GPX file?
Asked Answered
T

5

25

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?

Thirst answered 20/2, 2009 at 15:28 Comment(0)
D
26

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

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.

Edit: 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).

Derek answered 20/2, 2009 at 15:41 Comment(4)
Thanks a lot. I will port it to java and post it here. What does @DtoR means? Distance to Radius?Thirst
That's the factor that converts degrees into Radians, pi/180.Derek
Thanks for the link. I will post my Java version soonThirst
The method is great for simple calculations, but it assumes the Earth is spherical. A more accurate calculation should use the WGS 84 ellipsoid standard or similar.Bagasse
S
1

Mike Gavaghan has an algorithm for distance calculations on his site. There is a C# and also a JAVA version of the code.

Standpoint answered 15/3, 2010 at 9:54 Comment(0)
K
1

Delphi implementation of the Vincenty formulae can be found here.

Klehm answered 4/12, 2011 at 16:28 Comment(1)
@Adam Carter: I've checked the URL and it's still OK.Klehm
O
1

Here's a Scala implementation.

3958.761 is the mean radius of the Earth in miles. To get a result in km (or some other unit) just alter this number.

// The Haversine formula
def haversineDistance(pointA: (Double, Double), pointB: (Double, Double)): Double = {
  val deltaLat = math.toRadians(pointB._1 - pointA._1)
  val deltaLong = math.toRadians(pointB._2 - pointA._2)
  val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
  val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  3958.761 * greatCircleDistance
}

// A sequence of gpx trackpoint lat,long pairs parsed from the track GPX data
val trkpts: Seq[(Double, Double)] = {
  val x = scala.xml.XML.loadString(track)
  (x \\ "trkpt").map(trkpt => ((trkpt \ "@lat").text.toDouble, (trkpt \ "@lon").text.toDouble))
}

// Distance of track in miles using Haversine formula
val trackDistance: Double = {
  trkpts match {
    case head :: tail => tail.foldLeft(head, 0.0)((accum, elem) => (elem, accum._2 + haversineDistance(accum._1, elem)))._2
    case Nil => 0.0
  }
}
Oystercatcher answered 28/10, 2013 at 10:58 Comment(0)
W
0

This question is rather old, but I would like to add a python option for completeness. GeoPy has both great-circle distance and Vincenty distance.

Wilber answered 18/8, 2017 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.