Created
May 17, 2012 22:33
-
-
Save DfKimera/2722007 to your computer and use it in GitHub Desktop.
GeoSearch Stored Procedure
This file contains hidden or 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
| CREATE DEFINER=`root`@`localhost` PROCEDURE `get_nearby_clusters`(IN `mylat` FLOAT, IN `mylng` FLOAT, IN `dist` FLOAT) | |
| LANGUAGE SQL | |
| NOT DETERMINISTIC | |
| READS SQL DATA | |
| SQL SECURITY INVOKER | |
| COMMENT '' | |
| BEGIN | |
| declare lng1 float; declare lng2 float; | |
| declare lat1 float; declare lat2 float; | |
| -- Define a rectangle around the origin, delimiting the max range for possible clusters | |
| set lng1 = mylng-dist/abs(cos(radians(mylat))*69); | |
| set lng2 = mylng+dist/abs(cos(radians(mylat))*69); | |
| set lat1 = mylat-(dist/69); | |
| set lat2 = mylat+(dist/69); | |
| -- Run the search query | |
| -- This will perform a Harvesine distance calculation between the origin and the tested point within the tolerable distance | |
| SELECT | |
| d.*, | |
| 3956 * 2 * ASIN(SQRT( POWER(SIN((mylat - d.lat) * pi()/180 / 2), 2) + | |
| COS(mylat * pi()/180) * COS(d.lat * pi()/180) * POWER(SIN((mylng - d.lng) * pi()/180 / 2), 2) )) | |
| as distance | |
| FROM clusters d WHERE | |
| d.lng between lng1 and lng2 and | |
| d.lat between lat1 and lat2 | |
| HAVING distance < dist | |
| ORDER BY distance | |
| LIMIT 64; | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment