Summer is halfway over, unbelievable.
Time has been flying by. Between the nice weather, shore house, and work being extemely busy I haven’t spent much time on here or on ybg. At work we are wrapping up footballfanatics.com, the big asp.net/c# ecommerce site mentioned before. It has been a lot of work, but I have learned a ton and theres some cool stuff done which I will blog about soon.
Lately I have been burnt out on ybg and other side projects, after a long day of coding at work its tough to come home and get back at it. I’m trying to get over that though.
I have made some progress on the Flickr integration, not going to make any promises when that will be up though. In other areas I have been messing with Pl/SQL and finally got my distance calculations going.
I ended up using the “Haversine” forumula and created simple Pl/SQL function that calculates the distance between two sets of longitude and latitude. Could probably be tidied up a bit but heres my first go at it that works.
CREATE OR REPLACE FUNCTION distance
(
lat1 decimal,
long1 decimal,
lat2 decimal,
long2 decimal
)
RETURNS decimal AS $$
DECLARE
r decimal := 6371; -- earth’s radius (mean radius = 6,371km)
rLat1 decimal := degreesToRadians(lat1);
rLat2 decimal := degreesToRadians(lat2);
rLong1 decimal := degreesToRadians(long1);
rLong2 decimal := degreesToRadians(long2);
dLat decimal := rLat2 - rLat1;
dLong decimal := rLong2 - rLong1;
a decimal;
distance decimal;
BEGIN
-- "Haversine" Formula
a := sin(dLat/2)^2 + cos(rLat1)*cos(rLat2)*sin(dLong / 2)^2;
distance := r * 2 * atan(sqrt(a) / sqrt(1-a));
-- Spherical Law of Cosines
-- distance := acos(sin(rLat1)*sin(rLat2)+cos(rLat1)*cos(rLat2)*cos(rLong2 - rLong1)) * r;
RETURN distance * .62; -- convert to miles
END;
$$ LANGUAGE plpgsql;
That code uses a simple degree to radian conversion function
CREATE OR REPLACE FUNCTION degreesToRadians
(
degrees decimal
)
RETURNS decimal AS $$
BEGIN
RETURN degrees * (pi() / 180);
END;
$$ LANGUAGE plpgsql;
First use of this will be to show “Other bars in the neighborhood” when looking at a bar detail page.