Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locations within a radius #46

Open
oyeanuj opened this issue Sep 4, 2018 · 1 comment
Open

Locations within a radius #46

oyeanuj opened this issue Sep 4, 2018 · 1 comment

Comments

@oyeanuj
Copy link

oyeanuj commented Sep 4, 2018

Hi @teeparham, I was wondering if there is method or recommended approach to queries where you are looking for records in a certain radius of a point? I tried looking thru the docs but couldn't find the right solution.

I assume this is a common case, so if there is a recommended solution that is not documented, I'm happy to PR the docs.

Thank you!

@dazuma
Copy link
Member

dazuma commented Sep 5, 2018

I covered this briefly in my RailsConf 2012 talk on the subject. My recommendation (assuming you have records in a PostGIS database, suitably indexed) is to create a st_buffer polygon of the desired radius, and then query for records whose geometry st_intersects that polygon. This should allow the query optimizer to apply the index. Sample code is on slide 63. (It uses Squeel syntax which is somewhat defunct, but it should communicate the concept well enough.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants