# Measuring distance between two coordinate pairs taken from NMEA file?

In order to measure momentary speed, first I need to measure the distance between two pairs of coordinates. That's where my lack of knowledge stops me.

I have example data in NMEA format:

``````\$GPGGA,002246,3918.5986,N,11954.1140,W,1,08,2.3,2697.9,M,-22.4,M,,*4C
(...)
\$GPGGA,002248,3918.5887,N,11954.1395,W,1,08,2.4,2696.5,M,-22.4,M,,*42
``````

I figured out that data above gives me following information, needed to measure the distance:

• Coordinates: `39 deg 18.5986' N`, `119 deg 54.1140' W`
• Altitude (above sea level): `2697.9 m`
• Height of geoid above WGS84 elipsoid: `-22.4 m`
• Coordinates: `39 deg 18.5887' N, 119 deg 54.1395' W`
• Altitude (above sea level): `2696.5 m`
• Height of geoid above WGS84 elipsoid: `-22.4 m`

Angular distance between this two points is: latitude `-0,0099'`, longitude `0.0255'`. The difference between altitudes is `-1,4 m`.

And there I'm stuck. And my question is - how to measure the distance between those two points in (kilo)meters?

I think it's worth repeating that the distance would be very small, because it's needed to calculate a momentary speed. So I think the data emphasized above (the height of a geoide) could improve precision.

• possible duplicate of Distance between GPS coordinates Commented Jul 20, 2015 at 18:38
• Commented Jul 20, 2015 at 18:45
• Depending on how you frame the question, there are dozens of similar ones. The only difference is which key words you use for NMEA coords: GPS coords, geographic coords, geodetic coords, lat-long coords. Also consider "great circle distance" or "geodesic distance" in your search. Commented Jul 20, 2015 at 18:54
• Are you suggesting to leave out the difference in altitudes? I need the distance to calculate the momentary speed. And what about the Earth being an elipsoid, not a sphere? In data I have the cryptic "height of geoid..." which I just don't know how to use. And what about problems with `atan`s accuracy?
– Luke
Commented Jul 20, 2015 at 19:16
• What is your application? What is your expected accuracy? You can calculate speed based on two positions but remember the accuracy of GPS is potentially 30m (probably closer to 10m). Your results could be incorrect. Is your device able to output other NMEA sentences? Some sentences include speed (GPRMC, I think). Commented Jul 20, 2015 at 19:44

If your data is in a cartesian/rectangular coordinate system you could simply do 2 pythagorean calculations... where the first one would calculate the distance in XY plane, where a would be x1-x2 and b would be y1-y2. The second calculation would use the result from the first calculation as a and b would be z1-z2.

perhaps something like this

``````sqrt((sqrt((deltaX)^2+(deltaY)^2)))^2+(deltaZ)^2)
``````

This site will calculate 3D GPS positions very accurately.

I couldnt tell if you were just asking how to calculate a couple points or how to automate the process. To automate i would just make a CURL script to hit this site with your point data.

• I'm asking for the algorithm. I'm writing a program in C++ but it shouldn't matter.
– Luke
Commented Jul 20, 2015 at 19:59
• if you just want the algorithm i believe this is it. gis.stackexchange.com/questions/108547/… Commented Jul 20, 2015 at 20:05
• The Fwd/invers3d calculations are based upon Vincenty formula. There are many resources documenting the code in multiple languages, and the original formula. I also believe there may be several public library functions available. I have seen the code in C#, Fortran, and Python, as well as Excel. There are many sites with this, Haversine, and Great Circle Calculation formula available, you will have to sort out which one works best for you. A Google Search for Vincenty Formula will yield you a great deal of information. Commented May 12, 2016 at 4:07