Last active
August 29, 2015 14:18
-
-
Save danielpclark/7efbf51cf6d3e710564e to your computer and use it in GitHub Desktop.
Solving geolocation
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
Address.near("Winchester, VA 22601", 300, {order: ""}).pluck(:state) | |
SELECT "addresses"."state" FROM "addresses" | |
WHERE ( | |
addresses.latitude BETWEEN 34.84204350667456 AND 43.52595049332544 AND | |
addresses.longitude BETWEEN -83.76569414770746 AND -72.56240185229252 AND | |
( | |
3958.755864232 * 2 * ASIN( | |
SQRT( | |
POWER( | |
SIN( | |
(39.183997 - addresses.latitude) * PI() / 180 / 2 | |
), 2 | |
) + COS(39.183997 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER( | |
SIN( | |
(-78.164048 - addresses.longitude) * PI() / 180 / 2 | |
), 2 | |
) | |
) | |
) | |
) BETWEEN 0.0 AND 300 | |
) | |
Address.near("Winchester, VA 22601", 300, {order: ""}) | |
SELECT addresses.*, 3958.755864232 * 2 * ASIN( | |
SQRT( | |
POWER( | |
SIN( | |
(39.183997 - addresses.latitude) * PI() / 180 / 2 | |
), 2 | |
) + COS(39.183997 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER( | |
SIN( | |
(-78.164048 - addresses.longitude) * PI() / 180 / 2 | |
), 2 | |
) | |
) | |
) AS distance, MOD( | |
CAST( | |
( | |
ATAN2( | |
( | |
(addresses.longitude - -78.164048) / 57.2957795 | |
), | |
( | |
(addresses.latitude - 39.183997) / 57.2957795 | |
) | |
) * 57.2957795 | |
) + 360 AS decimal | |
), 360 | |
) AS bearing FROM "addresses" WHERE ( | |
addresses.latitude BETWEEN 34.84204350667456 AND 43.52595049332544 AND | |
addresses.longitude BETWEEN -83.76569414770746 AND -72.56240185229252 AND | |
( | |
3958.755864232 * 2 * ASIN( | |
SQRT( | |
POWER( | |
SIN( | |
(39.183997 - addresses.latitude) * PI() / 180 / 2 | |
), 2 | |
) + COS(39.183997 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER( | |
SIN( | |
(-78.164048 - addresses.longitude) * PI() / 180 / 2 | |
), 2 | |
) | |
) | |
) | |
) BETWEEN 0.0 AND 300 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pluck destroys the first half of the query