Assuming I have the following PostgresSQL table locations name/longitude/latitude:
name | longitude | latitude ----------------------------------------- A | 14.02023923239 | 13.020239232393 B | 23.29328403231 | 20.203923847782 C | 8.02392784729 | 50.302398462732 D | 28.23828482848 | 29.845102045853 E | 32.20328328849 | 39.923828328782
How can I select the rows that are in a radius of (for example) 10 kilometers from a starting point longitude 13.99999999999 and latitude 12.99999999999?
I accept any answer that gives me a concrete SQL statement.
Advertisement
Answer
Thanks guys. I found this stored procedure on some website (forgot the link). It works really well implementing the Haversine formula:
CREATE OR REPLACE FUNCTION calculate_distance(lat1 float, lon1 float, lat2 float, lon2 float, units varchar) RETURNS float AS $dist$ DECLARE dist float = 0; radlat1 float; radlat2 float; theta float; radtheta float; BEGIN IF lat1 = lat2 OR lon1 = lon2 THEN RETURN dist; ELSE radlat1 = pi() * lat1 / 180; radlat2 = pi() * lat2 / 180; theta = lon1 - lon2; radtheta = pi() * theta / 180; dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta); IF dist > 1 THEN dist = 1; END IF; dist = acos(dist); dist = dist * 180 / pi(); dist = dist * 60 * 1.1515; IF units = 'K' THEN dist = dist * 1.609344; END IF; IF units = 'N' THEN dist = dist * 0.8684; END IF; RETURN dist; END IF; END; $dist$ LANGUAGE plpgsql;
With that stored procedure, I was able to build a SQL select:
select * from locations where calculate_distance(latitude, longitude, 13.99999999999, 12.99999999999, 'K') < 2