Reputation: 186
I have a table of geocoded locations with latitude and longitude. I'd like my database query to return a filtered list of them, ordered by distance from a fixed point.
There are a few options which I've explored:
I'll need a solution which works with Rails3.
If there are no better options, i'll have to implement my own haversine function, which sure seems like reinventing the wheel. Any better options?
Upvotes: 5
Views: 2831
Reputation: 4230
I would now (July 2011) recommend using geocoder
gem 'geocoder'
You could also use geokit-rails3 as I previously mentioned but it isn't complete and isn't maintained.
gem 'geokit-rails3'
http://github.com/jlecour/geokit-rails3
Upvotes: 8
Reputation: 2652
Heroku now (Feb 2013) has beta level support for PostGIS. Full details are available on their site (login required): https://devcenter.heroku.com/articles/heroku-postgres-extensions-postgis-full-text-search#postgis
PostGIS:
adds support for geographic objects to the PostgreSQL object-relational database. In effect, PostGIS “spatially enables” the PostgreSQL server, allowing it to be used as a backend spatial database for geographic information systems (GIS)
PostGIS v1.5 support on Heroku Postgres is in beta and is subject to change in the future.
Upvotes: 0
Reputation: 2812
Postgis is currently available in beta (March 2012) https://addons.heroku.com/spacialdb
Upvotes: 0
Reputation: 40277
Geokit will handle all of the distance calculations, using haversine, in the database. It also works great with heroku and postgres. Highly recommend.
Geokit Gem Rails Geokit Integration
all code taken from the github
class Location < ActiveRecord::Base
acts_as_mappable :default_units => :miles,
:default_formula => :sphere,
:distance_field_name => :distance,
:lat_column_name => :lat,
:lng_column_name => :lng
end
Store.find :all, :bounds=>[sw_point,ne_point]
bounds=Bounds.from_point_and_radius(home,5)
stores=Store.find :all, :include=>[:reviews,:cities] :bounds=>bounds
stores.sort_by_distance_from(home)
Upvotes: 3