rgeo / rgeo-activerecord

RGeo ActiveRecord extensions and tools for spatial connection adapters
Other
89 stars 64 forks source link

Locations within a radius #46

Open oyeanuj opened 6 years ago

oyeanuj commented 6 years ago

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 commented 6 years ago

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.)