I have a database of geocoded entries. I need to determine which two entries are the furthest apart from a subset of the total entries. For example, I select a list of 10 entries then, from that list, determine which two places represent the greatest distance within that list.
I cannot wrap my head around how to approach this. I've considered using radians even, but nothing seems to meet the requirement.
FYI, LAMP stack going here...
The following query will calculate the distance between all your points and return the two with the biggest distance:
SELECT coor1.longitude as lon1,
coor1.latitude as lat1,
coor2.longitude as lon2,
coor2.latitude as lat2,
(ACOS(
COS(RADIANS(coor1.latitude)) *
COS(RADIANS(coor1.longitude)) *
COS(RADIANS(coor2.latitude)) *
COS(RADIANS(coor2.longitude)) +
COS(RADIANS(coor1.latitude)) *
SIN(RADIANS(coor1.longitude)) *
COS(RADIANS(coor2.latitude)) *
SIN(RADIANS(coor2.longitude)) +
SIN(RADIANS(coor1.latitude)) *
SIN(RADIANS(coor2.latitude))
) * 6378 --- Use 3963.1 for miles
)
AS DistanceKM
FROM coordinates coor1,
coordinates coor2
WHERE NOT (coor1.longitude = coor2.longitude AND coor1.latitude = coor2.latitude)
ORDER BY DistanceKM DESC
LIMIT 1; --- Only the biggest
Now I recommending doing those calculations before hand and storing the result in a separate table.