Skip to content

Instantly share code, notes, and snippets.

@julitroalves
Created July 29, 2022 13:43
Show Gist options
  • Select an option

  • Save julitroalves/ae32a0ef1071b13cc6938bdf187d6f8e to your computer and use it in GitHub Desktop.

Select an option

Save julitroalves/ae32a0ef1071b13cc6938bdf187d6f8e to your computer and use it in GitHub Desktop.
SQL function to calculate distance between two given coordinates
CREATE OR REPLACE FUNCTION distance(
lat1 double precision,
lon1 double precision,
lat2 double precision,
lon2 double precision)
RETURNS double precision AS
$BODY$
DECLARE
R integer = 6371e3; -- Meters
rad double precision = 0.01745329252;
φ1 double precision = lat1 * rad;
φ2 double precision = lat2 * rad;
Δφ double precision = (lat2-lat1) * rad;
Δλ double precision = (lon2-lon1) * rad;
a double precision = sin(Δφ/2) * sin(Δφ/2) + cos(φ1) * cos(φ2) * sin(Δλ/2) * sin(Δλ/2);
c double precision = 2 * atan2(sqrt(a), sqrt(1-a));
BEGIN
RETURN R * c;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment