Algorithm to determine distance between lat/long coordinate pairs?

notfred

Lifer
Feb 12, 2001
38,241
4
0
Basically, the question is in the title. I want to determine the distance between latitude/longitude coordinate pairs. Ideally, this distance is not a straight line, but an arc that follows the curvature of the earth, but the distances I'm dealing with are probably small enough that that would inconsequential.

Anyone know how to do this?

I have data like this:
lat=38.553428650 long= -121.788489819
Along with elevation data in meters above mean sea level.
 

amdskip

Lifer
Jan 6, 2001
22,530
13
81
I don't have an answer for you but I'm guessing this is work related for you. Actually sounds pretty cool and complicated.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
I may be wrong, but VS.NET has an example about this. I can't recall if it had an automated way to do it or not, or what I searched for to get it (since what I was looking for was really totally unrelated), but it's there somewhere. I'll look for it when I get a chance if you can't find it.
 

IntegraGSR

Senior member
Apr 24, 2005
246
0
0
the circumference of the earth around the equator is about 40,076.5 km and about 40,008.6 km around the poles.

40,076.5km/360 = ~111.3km per degree

sqroot(40,076.5/3.14) = ~113km average radius around equator

if you were to add, say, 10km to your average radius, thus adding 10km of elevation

123km^2 x 3.14 = 47,505.06km circumference at 10km above average height around equator

49,062.5km/360 = ~136.3km per degree



this may not be exactly what you were looking for, but i hope it gets the general idea across as to what you'd have to do in order to measure distances between latitude/longitude point and at different altitudes


if you haven't figured something out/found a forumla in a day or two, i could possibly work up a nice formula to just plug numbers into ;)
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
Convert the longitude and latitude in deg min sec. Every minute of arc of longitude is 1 nautical mile. Every minute of arc of latitude around the Equator is 1 nautical mile. This way you find the distance on the North-South direction
Now, at a certain latitude, the length of the circle parallel to the Equator is decreased (it is equal to the Equator multiplied by cosine of latitude). This way you find the distance on the East-West direction
You end up with an triangle with a 90* corner, so the distance is simply the square root of the sum of squares of distances on N-S and E-W axis
 

nwfsnake

Senior member
Feb 28, 2003
697
0
0
Just to be clear, do you want to ignore the elevation differences between the two points, or do you want to take them into consideration also?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Spherical Trig is the answer, assuming you can tolerate the error in assuming that the earth is actually round :p If you need to consider an oblate earth it gets messy.

If you can assume a round earth, then you have a right spherical triangle, where two of the sides are the difference in latitude, and the difference in longitude, respectively. Use the spherical trig identities to solve for the length of the hypotenuse. You can then plug this angle into the formula for the length of a circular arc, where the radius is the radius of the earth (approx 6378.14 Km at the equator).

What kind of distances are you considering? I can think of a few approximations that might help with the elevation & oblate earth issues if you think it will be important. To get a feel for how significant the oblate earth effect is, do the calcualtion with the equatorial radius (6378.14 Km) and again with the polar radius (6356.8 Km). These results will bound the actual distance.
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
Well, Armitage, I remember putting an highly technical end to other highly technical threads. :beer: to you :p

But I want to have the last word: Elevation data can be used again with the "on-sphere" distance, to compute the final value. Use Pithagora's theorem once more and you're set.
However, due to the flat appearance of the Earth, altitude difference does not add to very much. The highest mountain on Earth is like 9km tall (5 miles), a distance you can walk easily in two hours
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Calin
Convert the longitude and latitude in deg min sec. Every minute of arc of longitude is 1 nautical mile. Every minute of arc of latitude around the Equator is 1 nautical mile. This way you find the distance on the North-South direction
Now, at a certain latitude, the length of the circle parallel to the Equator is decreased (it is equal to the Equator multiplied by cosine of latitude). This way you find the distance on the East-West direction
You end up with an triangle with a 90* corner, so the distance is simply the square root of the sum of squares of distances on N-S and E-W axis

Except a^2 + b^2 = c^2 only works for planar trig

Take this case. point A (0, 0), point B (45, 90) where the coordinates are given as (latitude, longitude)

By your method,
In your method, a, b, c are lengths
a, b are found by the formula for the length of a circular arc
a = Re(Blon - Alon) = 6378.14*(90 - 0)*PI/180 = 10018.76 Km
b = Re(Blat - Alat) = 6378.14*(45 - 0)*PI/180 = 5009.38 Km
c = sqrt(a^2 + b^2) = 11201.31 Km

By the spherical trig method.
a, b, c are angles
a = Blon - Alon = 90
b = Blat - Alat = 45

cos(c) = cos(a)*cos(b)
c = acos(cos(a)*cos(b)) = acos(0*0.71) = acos(0) = 90

Use the circular arc formula to find the length of this arc
C = Re*c = 6378.14*90*PI/180 = 10018.76

So, the planar trig assumption produces an error of (11201.31 - 10018.76)/10018.76 = 11.8% for this case

For small distances, it may be adequate, but in general, its not.

FWIW, the answer using the polar radius is 9985.24 Km - a difference of 33.5 Km or 0.33% The true answer on the oblate earth will be somewhere between those numbers.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Calin
Well, Armitage, I remember putting an highly technical end to other highly technical threads. :beer: to you :p

But I want to have the last word: Elevation data can be used again with the "on-sphere" distance, to compute the final value. Use Pithagora's theorem once more and you're set.
However, due to the flat appearance of the Earth, altitude difference does not add to very much. The highest mountain on Earth is like 9km tall (5 miles), a distance you can walk easily in two hours

Yep - all depends on what kind of accuracy you need. For short distances, your planar approach will be pretty close, but as you can see above, for global distances, it's off quite a bit. The elevation & oblate earth corrections will be smaller corrections yet.

edit - I've bounded the oblate earth error for the example above at 33Km. As you've pointed out, the maximum possible error due to elevation is 5 Km. So that puts things in perspective a bit.
 

TStep

Platinum Member
Feb 16, 2003
2,460
10
81
Clark Geoid or GRS80 ( i think is the current), once you get through all that drivle, you can make some guesses about radius and elevation. State Plane Coordinates are close enough for me. :)

Edit: The Earth is flat, I tell ya. Once you accept that, the calculations are much easier.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: TStep
Clark Geoid or GRS80 ( i think is the current), once you get through all that dribble, you can make some guesses about radius and elevation. State Plane Coordinates are close enough for me. :)

Edit: The Earth is flat, I tell ya. Once you accept that, the calculations are much easier.

WGS84
 

TStep

Platinum Member
Feb 16, 2003
2,460
10
81
Hence the "I think". It's been a good 15+ years since I've had to do anything remotely related to the OP. Like I said, the world is now flat for me.;)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: TStep
Hence the "I think". It's been a good 15+ years since I've had to do anything remotely related to the OP. Like I said, the world is now flat for me.;)

Yep - I didn't actually think of WGS84 until you mentioned GRS80 - I think of it more as a gravitational model then a geodetic model, but then remembered that it had both.

I read an essay by Issaac Asimov once explaining how the earth really is flat ... to within the accuracy needed at the time that it was really thought to be flat.
 

TStep

Platinum Member
Feb 16, 2003
2,460
10
81
I work in the heavy and highway construction industry and after 20+ years I am convinced that 99.9999% of the educated people that walk the earth cannot measure a distance, let alone calculate one. For most applications, Asimov is right. For example, I spent years in the field taking compiled roadway coordinate design data and making it "work in the field". Though the end product may be geometrically incorrect, the end product is fully functional. Hence the level of accuracy was adequate. Now NORAD or NASA obviously need a higher level of accuracy.

So, I suppose the real answer to the OP lies in the level of accuracy needed. Planar if local, more advanced if the distances are of a much greater magnitude.
 

Calin

Diamond Member
Apr 9, 2001
3,112
0
0
I was thinking just at distances that I drove for - the order of half a day on a train. I doubt there is significant error by using planar geometry for distances in the 600km range. This would be a tenth of Earth radius, so you would end up with a difference of around 0.2%. For a 5000km distance the error would be 2% (if I can still calculate).
For a full semicircle, the error goes up to 50%, for half a semicircle it would be at 1.41R (no circle) or 1.57R on circle an 11% error. This is with a distance on Earth of 10000 km or so.

Calin
 

Mday

Lifer
Oct 14, 1999
18,647
1
81
you need a mathematical model of the varying radii of the earth, since it's not a sphere.
from the mathematical model you will be able to determine a parametric equation, based on angle, of the curvature (cross sectional perimeter) the two points lie on.
just take the line integral of that curvature to get the distance.

it's like taking a giant melon. put 2 dots on. cut the melon so that the exposed surface is completely flat and both dots lie on the perimeter of that surface. It's an easy proof that those 2 lines must lie on some perimeter of some surface, and that the surface is unique.
 

johnjbruin

Diamond Member
Jul 17, 2001
4,401
1
0
double dlon = pos2.getLongitude() - this.getLongitude();
double dlat = pos2.getLatitude() - this.getLatitude();
dlon = dlon * (PI_OVER_180);
dlat = dlat * (PI_OVER_180);
double a =
(Math.sin(dlat/2) * Math.sin(dlat/2))
+ Math.cos(this.getLatitude()*(PI_OVER_180)) *
Math.cos(pos2.getLatitude()*(PI_OVER_180)) *
(Math.sin(dlon/2)*Math.sin(dlon/2));
double c = 2 * Math.asin(Math.min(1,Math.sqrt(a)));
return EARTH_RADIUS * c;
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: Mday
you need a mathematical model of the varying radii of the earth, since it's not a sphere.
from the mathematical model you will be able to determine a parametric equation, based on angle, of the curvature (cross sectional perimeter) the two points lie on.
just take the line integral of that curvature to get the distance.

it's like taking a giant melon. put 2 dots on. cut the melon so that the exposed surface is completely flat and both dots lie on the perimeter of that surface. It's an easy proof that those 2 lines must lie on some perimeter of some surface, and that the surface is unique.

Exactly, but wouldn't you need to add the distance from point A to point B in small increments (i.e. take the integral), 'cos points A & B could be on top of (high) mountains and you'll need to go thru a valley to get to B from A.