Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do GIS queries on Heroku using the shared database?

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:

  • Postgresql's earthdistance contrib is probably closest to my needs; however, I've found no indication that this is installed on heroku's db server.
  • PostGIS is the most often prescribed solution for GIS, but heroku does not have it installed, and heroku support confirmed that they have no intention of doing so in the near future.

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?

like image 234
Jason S Avatar asked Aug 05 '10 10:08

Jason S


2 Answers

I would now (July 2011) recommend using geocoder

gem 'geocoder'

http://www.rubygeocoder.com/

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

like image 109
swrobel Avatar answered Sep 18 '22 22:09

swrobel


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)
like image 44
Jesse Wolgamott Avatar answered Sep 18 '22 22:09

Jesse Wolgamott