DROP FUNCTION geodist(float8,float8,float8,float8);

CREATE FUNCTION geodist(float8,float8,float8,float8)
RETURNS float8 AS '
	DECLARE
		lat1 ALIAS FOR $1;
		lon1 ALIAS FOR $2;
		lat2 ALIAS FOR $3;
		lon2 ALIAS FOR $4;
		dist float8;
	BEGIN
		dist := 0.621 * 6371.2 * 2 * atan2( sqrt(abs(0 + pow(sin(radians(lat2)/2 - radians(lat1)/2),2) + cos(radians(lat1)) * cos(radians(lat2)) * pow(sin(radians(lon2)/2 - radians(lon1)/2),2))),sqrt(abs(1 - pow(sin(radians(lat2)/2 - radians(lat1)/2),2) + cos(radians(lat1)) * cos(radians(lat2)) * pow(sin(radians(lon2)/2 - radians(lon1)/2),2))));
		return dist;
	END;
' LANGUAGE 'plpgsql';