tags:

views:

46

answers:

2

I've got an sql query that pulls locations from a database based on coordinates within a certain range. Now I'm trying to order them by proximity with a bit of math - but no matter how many times im writing it out on paper I can't seem to figure out a working solution.

lets describe a few variables:

  • $plat (the latitude of the place)
  • $plong (the longitude of the place)
  • $slat (the latitude of the searched location)
  • $slong (the longitude of the searched location)

One big thing to note is that I've converted all coordinates to positive numbers and have another feild that determines positive or negative - but for this assume that I've already queried the nearby coordinates properly.

In order to order it this way I've seen people use "ORDER BY abs(coord-coords)" for something simple -but what I need is something more like:

[($slat - $plat) * ($slong - plong)] - but this causes problems because if the resulting calulation on one side is zero - then after multiplied the result would be zero as well - making it innaccurate.

Any ideas -

A: 

The solution for distance between latitude/longitude coordinates is called the haversine formula. It's complex because you need to take the curvature of the earth into account unless your distances are very short.

Here's an article about using PHP and MySQL to implement a locator app: Creating a Store Locator with PHP, MySQL & Google Maps

You can also find many other questions here on Stack Overflow regarding calculating distance between coordinates: http://stackoverflow.com/search?q=longitude+distance

If you only need to calculate distance within 5km, the curvature of the earth is probably not significant. You can use a plain distance formula. You can even skip the square-root calculation if you only need to use this value to sort which one is closer.

SELECT s.location_id,
  (s.lat-p.lat)*(s.lat-p.lat) + (s.long-p.long)*(s.long-p.long) AS distance_squared
FROM Locations s, Locations p
WHERE p.location_id = ?
ORDER BY distance_squared;

The table ('locations') I am selecting from has lat/long coordinates (lets call them $plat and $plong).

No, those are PHP variables. You need to calculate the distance from the lat/long coordinates on each row of your database table.

The problem is that SQL normally only calculates things from one row at a time. So you need to have some way of combining two rows into one row, so a calculation can use the coordinates of two locations. You do this with a self-join. This is basically pairing each row with another row from the same table. And that's why I list Locations twice, and give them two different aliases, s and p (the technical term is correlation name).

If you're accustomed to PHP, think of this self-join as analogous to a nested loop:

foreach ($locations as &$s) {
    foreach ($locations as &$p) {
        // calculate the distance between $s and $p
    }
}

The WHERE clause restricts the rows of p to just the place you start from (you would substitute a single value for the ? placeholder), so it's just one row that the query pairs with all the rows of the table.

Another tip: I skipped using SQRT() because it's not necessary just to sort by distance. That is, if three locations are 10km, 15km, and 20km from me, then I can sort by 100, 225, and 400 and get the same ordering as if I sort by 10, 15, and 20. The advantage is that I've eliminated calculating the square root which reduces the cost of my query somewhat.

Bill Karwin
There are also different models/coordinate systems because the Earth isn't a sphere. As stated, simple math doesn't work.
vol7ron
@vol7ron: Right, if you need that last 0.1% precision, use Vincenty's formulae.
Bill Karwin
Hey Bill - I think I can see where it's going but that code is a little above my head and not sure how to put it together.Here's my current query:SELECT * FROM locations WHERE ..(lots here).. ORDER BY (heres what i need)
Kris
@Kris: I included an example above. What part do you need help on?
Bill Karwin
I'm a bit confused by all the separate s's and p's to be honest - I'm not the most knowledgable on sql. The table ('locations') I am selecting from has lat/long coordinates (lets call them $plat and $plong).I think I'm just thrown off a bit by the decimals (eg. p.location_id) -Thanks again for all of your help, this is better than school :P
Kris
SELECT id,SQRT(($slat-$plat)*($slat-$plat) + ($slong-$plong)*($slong-$plong)) AS distance_squaredFROM locations WHERE (my other query)ORDER BY distance_squared;I'm trying to put it together with the distance forumla and your method - does the above query make sense?
Kris
I assume each row of your database table stores *one* location. You need to calculate the distance between *two* locations at a time, so you need a self-join. See my extra explanation above.
Bill Karwin
@Bill - It's working great! I'm using your first method without the square root (I see your point) and it seems to be doing everything I need with quite a bit of precision.Thank you so much for your help and time today.
Kris
A: 

You could use the haversine formula to calculate the distances between pairs of lat/long if you want to be precise about it. Failing that you could fall back to either the Pythagorean formula to calculate an approximation, or use the square of the distance (to eliminate the square root) if you're just using it for ordering.

Donnie