Search code examples
ruby-on-railssearchgeokit

Searching Post by Origin & Destination locations using Geokit-Rails


Ok what i have is a Trucking Load board where Truckers come to post there available Trucks. I have the Trucks posting. But am having issues setting up the search functions and the way i need to associate different tables.

Rails 3
postgres

gem 'geokit-rails'

The way i have it now is I have a locations table setup like:

 class Location < ActiveRecord::Base

  attr_accessible :cs, :lat, :lon, :city, :state

  acts_as_mappable :default_units => :miles,
                   :default_formula => :sphere,
                   :distance_field_name => :distance,
                   :lat_column_name => :lat,
                   :lng_column_name => :lon

When someone post a truck it has and Origin and a Destination. so it has 2 locations and have set it up like this:

class Truck < ActiveRecord::Base
  attr_accessible :available, :company_id, :dest_id, :origin_id, :equipment, :origin, :dest
  belongs_to :origin, class_name: "Location", foreign_key: :origin_id
  belongs_to :dest, class_name: "Location", foreign_key: :dest_id
  belongs_to :company

With the way i have it set up i can get the location information from:

Truck.find(1).origin  || Truck.find(1).dest

It will return the Location record associated with it

Now my issue is that i want to be able to write a search function to find any Trucks within a "given" amount of miles from origin || dest || origin & Destination

I know i can do Location.within(25, :origin => "Springfield, Mo") and it will search all the locations and return the ones that are within 25 miles of Springfield Mo

But how would i use this on Trucks where there is 2 locations (origin & dest) and they are associated with location id.

I currently have some other search Params already coded in and working just not sure how i could incorporate this into it:

def search(search)
    where = []
    where << PrepareSearch.states('dest', search.dest_states) unless search.dest_states.blank?
    where << PrepareSearch.states('origin', search.origin_states) unless search.origin_states.blank?
    where << PrepareSearch.location('origin', search.origin_id, search.radius) unless search.origin.blank?
    where << PrepareSearch.location('dest', search.dest_id, search.radius) unless search.dest.blank?
    where << PrepareSearch.equipment(search.equipment) unless search.equipment.blank?
    where << PrepareSearch.date('available', search.available, '<') unless search.available.blank?
    where = where.join(' AND ')
    Truck.where(where)
  end


module PrepareSearch
    def PrepareSearch.location(type, location, radius)
      loc = Location.find(location)

   *type will be origin/destination Location active record
   *location will be Location id
   *radius will be a given mileage

**This is where i need to figure out what to put here**

    end
end

Would it be better just to incorporate the equation:

 def sphere_distance_sql(origin, units)
    lat = deg2rad(origin.lat)
    lng = deg2rad(origin.lng)
    multiplier = 3963.1899999999996 # for miles

    sphere_distance_sql(lat, lng, multiplier)
  end
  def sphere_distance_sql(lat, lng, multiplier)
    %|
      (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
      COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
      SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
     |
  end

Solution

  • Ok Well I have figured out a solution to my problem... if there is a better one i would love to know.

    where << PrepareSearch.location_ids("origin", search.origin, search.radius) unless search.origin_id.blank?
    where << PrepareSearch.location_ids("dest", search.dest, search.radius) unless search.dest_id.blank?
    
    
    def PrepareSearch.location_ids(type, location, radius)
      if location.nil?
        return nil
      else
        loc = Location.find(location)
        location(type, loc, radius)
      end
    
    end
    
    def PrepareSearch.location(type, location, radius)
      locs = Location.within(radius, :origin => location.cs).pluck(:id)
      st = ""
      locs.each {|s| st += "'#{s}'," }
      st = st[0..-2]
      "#{type}_id IN (#{st})"
    end