tags:

views:

874

answers:

6

What is the formula for calculating the distance between 2 geocodes? I have seen some of the answers on this site but they basically say to rely on SQL Server 08 fucntions, I'm not on 08 yet. ANy help would be appreciated. Thanks.

A: 

the pythagorean theorem?

happythenewsad
Alas, the earth is not flat.
Edward Kmett
It depends on the projection you are using. There is a state plane projection that is very accurate over relatively short distances. With a state plane projection, pythagorean theorem is fine. If you're using latitude/longitude coordinates, which are already expressed in angles (not distances), you cannot use pythogorean theorem.
G Mastros
+3  A: 

Use an approximation of the earth and the Haversine formula. You can get a javascript version on the following url, which you can translate to your language of choice:

http://www.movable-type.co.uk/scripts/latlong.html

Here is another way: http://escience.anu.edu.au/project/04S2/SE/3DVOT/3DVOT/pHatTrack_Application/Source_code/pHatTrack/Converter.java

JMD
+2  A: 

Take a look here for a SQL server 2000 version SQL Server Zipcode Latitude/Longitude proximity distance search

SQLMenace
A: 

You're looking for the length of the Great Circle Path between two points on a sphere. Try looking up "Great Circle Path" or "Great Circle Distance" on Google.

taserian
A: 

Sorry, I don't know what country you are in even. Are we talking about Easting and Northings (UK, Ordance Survey system) or Lat/Long or some other system? If we are talking Easting and Northing then you can use
sqr((x1-x2)^2 + (y1-y2)^2)
This does not allow for the fact that the earth is a sphere, but for short distances you won't notice. We use it at work for distances between points within the county.
Be carful about how longer grid reference you use. I think an 8 figure reference will give you a distance in metres. I'll be able to get a definate answer at work next week if no one else has supplied it.

pipTheGeek
A: 

The pythagorean theorem as offered up by others here doesn't work so well.

The best, simple answer is to approximate the earth as a sphere (its actually a slightly flattened sphere, but this is very close). In Haskell, for instance you might use the following, but the math can be transcribed into pretty much anything:

distRadians (lat1,lon1) (lat2,lon2) = 
    radius_of_earth * 
    acos (cos lat1 * cos lon1 * cos lat2 * cos lon2 + 
          cos lat1 * sin lon1 * cos lat2 * sin lon2 + 
          sin lat1 * sin lat2) where
    radius_of_earth = 6378 -- kilometers


distDegrees a b = distRadians (coord2rad a) (coord2rad b) where
    deg2rad d = d * pi / 180
    coord2rad (lat,lon) = (deg2rad lat, deg2rad lon)

distRadians requires your angles to be given in radians.

distDegrees is a helper function that can take lattitudes and longitudes in degrees.

See this series of posts for more information on the derivation of this formula.

If you really need the extra precision granted by assuming the earth is ellipsoidal, see this FAQ: http://www.movable-type.co.uk/scripts/gis-faq-5.1.html

Edward Kmett