views:

143

answers:

4

Hi,

I need to calculate the distance between two points, but not in the regular way. I need to know 'the east to west distance' + 'the north to south distance'. I guess this is more simple then the regular 'as the crow flies' calculation but i still can't figure out how to do it.

I want to do this using a MySQL query and preferably have the result returned in km. One of the points will be a constant in the query and the other point is a point from the DB so something like SELECT abs(longitude-39.12345)...+abs(latitude... AS Distance FROM shops WHERE shopname='Bingo'.

Thanks in advance!

A: 

Use simple trig. The normal "as the crow flies" distance is the hypotenuse of the right triangle formed with the two points you have in mind at either ends of the hypotenuse.

Just look at this http://en.wikipedia.org/wiki/Hypotenuse and the solution should become clear.

Sushisource
+3  A: 

The north-to-south distance is proportional to the difference in the latitudes. It's about 1 nautical mile per minute of arc (the circumference of the earth is about 21600 nautical miles).

The east-to-west distance is proportional to the difference in the longitudes, but it also varies with the latitude (e.g. it's zero at the poles): I think it's proportional to the cosine of latitude.

ChrisW
You're correct, the east-west distance for a given delta_longitude is proportional to cos(latitude).
Jim Lewis
A: 

You can determine the distance between any two geocode points using the Great Circle distance. Here is another decent article on the subject.

The following is an example in C# which shows how to do this:

var radius = Constants.EarthRadius; // 6377.8 km

var dLat = ToRadians(fromLatitude - toLatitude);
var dLon = ToRadians(fromLongitude - toLongitude);

var a = 
    Math.Sin(dLat / 2) * 
    Math.Sin(dLat / 2) +
    Math.Cos(ToRadians(fromLatitude)) *
    Math.Cos(ToRadians(fromLatitude)) *
    Math.Sin(dLon / 2) * 
    Math.Sin(dLon / 2);

var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));

var distanceInKilometers = radius * c;
CaptainTom
Nice calculation - but it isn't the answer to the question.
Jonathan Leffler
A: 

Your answer depends on the accuracy required in your answer. If you don't need an answer more accurate than a spherical earth model, you can use a solution similar to the one given by Captain Tom. If you require more accuracy, you'll need to assume the earth is an oblate spheroid. See http://en.wikipedia.org/wiki/Vincenty%27s_formulae for a couple of solutions.

John Gordon