Created
September 21, 2012 15:11
-
-
Save jonsherrard/3762079 to your computer and use it in GitHub Desktop.
query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rad2deg (angle) { | |
return angle * 57.29577951308232; // angle / Math.PI * 180 | |
} | |
function deg2rad (angle) { | |
return angle * .017453292519943295; // (angle / 180) * Math.PI; | |
} | |
function get_events (input_lat, input_lon) { | |
lat = input_lat; | |
lon = input_lon; | |
rad = 3; | |
earth_radius = 6371; | |
max_lat = lat + rad2deg(rad/earth_radius); | |
min_lat = lat - rad2deg(rad/earth_radius); | |
max_lon = lon + rad2deg(rad/earth_radius/cos(deg2rad(lat))); | |
min_lon = lon - rad2deg(rad/earth_radius/cos(deg2rad(lat))); | |
lat = deg2rad(lat); | |
lon = deg2rad(lon); | |
sql = "SELECT *, acos(sin(lat)*sin(radians(business_latitude)) + cos(lat)*cos(radians(business_latitude))*cos(radians(business_longitude)-lon))*earth_radius As distance | |
FROM ( | |
SELECT * | |
FROM businesses | |
WHERE business_latitude>min_lat AND business_latitude< max_lat | |
AND business_longitude>min_lon AND business_longitude<max_lon | |
) As FirstCut | |
Where acos(sin(lat)*sin(radians(business_latitude)) + cos(lat)*cos(radians(business_latitude))*cos(radians(business_longitude)-lon))*earth_radius < rad | |
GROUP BY business_id;"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment