Module: GeoFoo::ClassMethods

Defined in:
lib/geo_foo/active_record.rb

Instance Method Summary collapse

Instance Method Details

#add_geo_fooObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/geo_foo/active_record.rb', line 10

def add_geo_foo
  self.class_eval do
    def self.within_radius lat, lon, radius
      scoped(
        :conditions => [
          "ST_DWithin(#{GeoFoo.as_point(lat,lon)}, "\
          "point, #{bbox_size(lat, radius)}) AND ST_Distance_Sphere(" \
          "point, #{GeoFoo.as_point(lat,lon)}) < #{radius}"
        ]
      )
    end
  end
end

#bbox_size(latitude, radius) ⇒ Object

XXX handle case where lat is (close to) +-90deg (poles)



33
34
35
# File 'lib/geo_foo/active_record.rb', line 33

def bbox_size latitude, radius
  (radius.to_f / (GeoFoo::EarthRadius * Math.cos(latitude.to_rad))).to_deg
end

#find_coords_by_id(id) ⇒ Object

return latitude, longitude for a given id



25
26
27
28
29
30
# File 'lib/geo_foo/active_record.rb', line 25

def find_coords_by_id id
  result = connection.execute(
    "SELECT ST_Y(point), ST_X(point) FROM #{table_name} WHERE id = #{id}")[0]
  # Intentionally return (y,x) which corresponds to (lat,lon). See as_point().
  [result["st_y"].to_f, result["st_x"].to_f]
end