GeoRSS aggregator and Layar augmented reality server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

post.rb 1.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class Post < ActiveRecord::Base
  2. belongs_to :feed
  3. EARTH_RADIUS_METRES = 6378000
  4. def self.near(lat, lon, radius_metres)
  5. # Santize inputs. Is this necessary?
  6. lat = lat.to_f
  7. lon = lon.to_f
  8. radius_metres = radius_metres.to_i
  9. # Calculate distance using the Haversine formula
  10. self.find_by_sql(<<-ENDQUERY
  11. SELECT
  12. p.id,
  13. p.title,
  14. p.summary,
  15. p.url,
  16. p.lat,
  17. p.lon,
  18. p.published,
  19. f.title as feed_title,
  20. ( #{EARTH_RADIUS_METRES}
  21. * acos( cos( radians('#{lat}') )
  22. * cos( radians( p.lat ) )
  23. * cos( radians( p.lon )
  24. - radians('#{lon}') )
  25. + sin( radians('#{lat}') )
  26. * sin( radians( p.lat ) ) ) )
  27. As distance
  28. FROM posts p
  29. INNER JOIN feeds f
  30. ON p.feed_id = f.id
  31. WHERE
  32. ( #{EARTH_RADIUS_METRES}
  33. * acos( cos( radians('#{lat}') )
  34. * cos( radians( p.lat ) )
  35. * cos( radians( p.lon )
  36. - radians('#{lon}') )
  37. + sin( radians('#{lat}') )
  38. * sin( radians( p.lat ) ) ) )
  39. < #{radius_metres}
  40. ORDER BY distance
  41. ENDQUERY
  42. )
  43. end
  44. end