tags:

views:

475

answers:

3

I'd like to create a function that calculates the distance between two pairs of lat/longs using the pythag theorem instead of the haversine great-circle formula. Since this will be over relative short distances (3km), I think this version that assumes a flat earth should be OK. How can I do this? I asked the internet and didn't come up with anything useful. :)

Thanks.

Tom

EDIT:

Here's what I came up with (seems to be working):

def get_dist(lat0, lng0, lat1, lng1)
  begin
    d_ew = (lng1.to_f - lng0.to_f) * Math.cos(lat0.to_f)
    d_ns = (lat1.to_f - lat0.to_f)
    d_lu = Math.sqrt(d_ew.to_f * d_ew.to_f + d_ns.to_f * d_ns.to_f)
    d_mi = ((2*Math::PI*3961.3)/360)*d_lu
    return d_mi
  rescue Exception => ex
    logger.debug "[get_dist] An exception occurred: #{ex.message}"
    return -1
  end
end

Thanks Evan!

+5  A: 

You can use a simple pythagoras triangle if you expect the distances involved to be small compared with the size of the Earth.

Suppose you are at (lat0, long0) and you want to know the distance to a point (lat1, long1) in "latitude units".

Horizontal (EW) distance is roughly

d_ew = (long1 - long0) * cos(lat0)

This is multiplied by cos(lat0) to account for longitude lines getting closer together at high latitude.

Vertical (NS) distance is easier

d_ns = (lat1 - lat0)

So the distance between the two points is

d = sqrt(d_ew * d_ew + d_ns * d_ns)

You can refine this method for more exacting tasks, but this should be good enough for comparing distances.

In fact, for comparing distances, it will be fine to compare d squared, which means you can omit the sqrt operation.

Ewan Todd
++1 - yours is simpler than mine, so I deleted mine.
James Black
perfect, thanks :D
cakeforcerberus
out of curiosity, if I get horizontal distance, is there a way to convert that to km's or miles?
cakeforcerberus
cakeforcerberus, you are a harsh task master. There are a few reasons why that is not so straightforward. One is that the Earth is not a sphere. You can get a crude estimate by pretending that it is a sphere. You can figure then that a "latitude unit" is the distance that corresponds to one degree latitude. That's 2 * pi * R / 360.0, where R is the radius of the Earth. To get a better estimate than that, the model gets complicated quickly.
Ewan Todd
damn oblate spheroids! :D Can you spell it out for me please? If I assume a perfect sphere and have a distance .0023 in latitude units, I can get miles by doing what?
cakeforcerberus
OK. Let's try this in google: `2 * pi * 3959 / 360` Looks like it's 69.0975851 miles. Keep as many significant figures as you can until the very last step. And you won't find the term "latitude units" anywhere else, I made the term up. So, if your lat and long are in degrees, multiply d from above by this number to *approximate* the distance in miles. 0.0023 * 69.0975851 miles = 0.158924445 miles. Round it to the same precision as the 0.0023 and it's 0.16 miles. Don't sue me if it differs a bit from what you expected.
Ewan Todd
+2  A: 

Well, since your points are near each other, the surface of the sphere is almost flat, so just find the coordinates of the points in 3D space, so find (x,y,z) for each of the points, where

x = r*sin(lat)*cos(long)
y = r*sin(lat)*sin(long)
z = r*cos(lat)

where r is the radius of the sphere. or something like that depending on how you define lat/long. Once you have the two xyz coords, just use sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2). You really can't just use a 2D Pythagorean theoreom since you would need to get reasonable 2D coordinates, which is hard.

Victor Liu
It would certainly be worth comparing the result of this approach with my 2D pythagoras with cos(lat). You may well get more acceptable results like this.
Ewan Todd
A: 

You will commonly see this notation 'dy, dx' which stands for difference y and difference x. You simply work out the differences on both axises, the get the square root of both differences squared as per the theorum.(the sum of the hype is equal to the square of the other two sides).

var dx:Number = x1-x2;
var dy:Number = y1-y2;
var distance:Number = Math.sqrt(dx*dx + dy*dy);

Hope this is clear enough