Skip to content

Instantly share code, notes, and snippets.

@marcoblos
Last active September 5, 2017 17:11
Show Gist options
  • Save marcoblos/c868119e0b7fea48a79a8fd015852054 to your computer and use it in GitHub Desktop.
Save marcoblos/c868119e0b7fea48a79a8fd015852054 to your computer and use it in GitHub Desktop.
proximity SQL algorithm

Based on:

SELECT rua,
truncate((degrees(acos(
    sin(radians(latitude))
    * sin( radians('-29.6395590'))
    + cos(radians(latitude))
    * cos( radians('-29.6395590'))
    * cos( radians(longitude - '-50.9884770') ) 
    ) ) * 111.045),1) as distance
FROM endereco
WHERE latitude BETWEEN '-29.6395590' - (50.0 / 111.045)
             AND '-29.6395590' + (50.0 / 111.045)
	AND longitude BETWEEN '-50.9884770' - (50.0 / (111.045 * COS(RADIANS('-29.6395590'))))
              AND '-50.9884770' + (50.0 / (111.045 * COS(RADIANS('-29.6395590'))))
HAVING distance <= 2
ORDER BY distance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment