module GeoQuery::GeoQueryable

Attributes

lat[RW]

Accessors

lon[RW]

Accessors

Public Class Methods

near_lat_lon(lat, lon, radius=500) click to toggle source

Class methods

Returns other objects within the given radius of a point
# File lib/geo_query/geo_queryable.rb, line 49
def self.near_lat_lon(lat, lon, radius=500) #radius in metres
  select("#{self.table_name}.*, ST_Distance(#{self.point_column}, ST_GeographyFromText('POINT(#{lon} #{lat})')) AS distance")
  .where("ST_DWithin(#{self.table_name}.#{self.point_column} , ST_GeographyFromText('POINT(#{lon} #{lat})'), #{radius})")
  .order("distance ASC")
end
within_bounding_box(min_lat, min_lon, max_lat, max_lon) click to toggle source

Returns all objects within a bounding box

# File lib/geo_query/geo_queryable.rb, line 56
def self.within_bounding_box(min_lat, min_lon, max_lat, max_lon)
  select("#{self.table_name}.*").
  where("#{self.table_name}.#{self.point_column} && ST_MakeEnvelope(?, ?, ?, ?, 4326)",
    min_lon, min_lat, max_lon, max_lat)
end

Public Instance Methods

location_did_change?() click to toggle source
# File lib/geo_query/geo_queryable.rb, line 16
def location_did_change?
  method("#{self.point_column}_changed?").call
end
near(radius=500) click to toggle source
# File lib/geo_query/geo_queryable.rb, line 37
def near(radius=500) #radius in metres
  # x = longitude && y = latitude
  return self.class.near_lat_lon(st_coordinates.y, st_coordinates.x, radius) if st_coordinates
  self.class.none
end
rgeo_factory() click to toggle source

Instance Methods

# File lib/geo_query/geo_queryable.rb, line 33
def rgeo_factory
  RGeo::Geographic.spherical_factory(:srid => 4326)
end
save_coordinates() click to toggle source
# File lib/geo_query/geo_queryable.rb, line 24
def save_coordinates
  if lat.present? && lon.present?
    self.coordinates = "POINT(#{lon} #{lat})"
  elsif (lat.present? && lon.blank?) || (lon.present? && lat.blank?)
    errors.add(self.point_column, "requires both latitude and longitude")
  end
end
set_location_updated_at() click to toggle source
# File lib/geo_query/geo_queryable.rb, line 20
def set_location_updated_at
  self.location_updated_at = Time.now
end
st_coordinates() click to toggle source
# File lib/geo_query/geo_queryable.rb, line 43
def st_coordinates
  send("#{self.point_column}")
end