<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 08 Sep 2008 04:24:19 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Location queries for a model</title>
      <link>http://snippets.dzone.com/posts/show/1906</link>
      <description>// These are class functions which are useful for finding locations in a database &lt;br /&gt;// when PostGIS is not available (when using MySQL for example)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def self.deg2rad(deg)&lt;br /&gt;	(deg * Math::PI / 180)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.rad2deg(rad)&lt;br /&gt;	(rad * 180 / Math::PI)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.acos(rad)&lt;br /&gt;	Math.atan2(Math.sqrt(1 - rad**2), rad)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.distance_in_miles(loc1, loc2)&lt;br /&gt;	lat1 = loc1.latitude&lt;br /&gt;	lon1 = loc1.longitude&lt;br /&gt;	lat2 = loc2.latitude&lt;br /&gt;	lon2 = loc2.longitude&lt;br /&gt;	theta = lon1 - lon2&lt;br /&gt;	&lt;br /&gt;	dist = Math.sin(self.deg2rad(lat1)) * Math.sin(deg2rad(lat2)) &lt;br /&gt;		+ Math.cos(self.deg2rad(lat1)) * Math.cos(self.deg2rad(lat2)) * Math.cos(deg2rad(theta))&lt;br /&gt;	&lt;br /&gt;	dist = self.rad2deg(self.acos(dist))&lt;br /&gt;	&lt;br /&gt;	(dist * 60 * 1.1515).round #distance in miles&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def miles_to(location)&lt;br /&gt;	Location.distance_in_miles(self, location)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.locationArea(location, miles)&lt;br /&gt;	radius = miles.to_f&lt;br /&gt;	latR = radius / ((6076 / 5280) * 60)&lt;br /&gt;	lonR = radius / (((Math.cos(location.latitude * Math::PI / 180) * 6076) / 5280) * 60)&lt;br /&gt;	&lt;br /&gt;	{&lt;br /&gt;		:min_latitude =&gt; location.latitude - latR,&lt;br /&gt;		:min_longitude =&gt; location.longitude - lonR,&lt;br /&gt;		:max_latitude =&gt; location.latitude + latR,&lt;br /&gt;		:max_longitude =&gt; location.longitude + lonR&lt;br /&gt;	}&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.location_ids_in_range(location, miles)&lt;br /&gt;	la = Location.locationArea(location, miles)&lt;br /&gt;	Location.find(:all,&lt;br /&gt;		:select =&gt; "locations.id",&lt;br /&gt;		:conditions =&gt; ["locations.latitude &lt;= ? and locations.latitude &gt;= ? " + \&lt;br /&gt;			" AND locations.longitude &gt;= ? and locations.longitude &lt;= ?",&lt;br /&gt;			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]&lt;br /&gt;		]&lt;br /&gt;	).collect { |l| l.id }&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def self.locations_in_range(location, miles)&lt;br /&gt;	la = Location.locationArea(location, miles)&lt;br /&gt;	Location.find(:all,&lt;br /&gt;		:conditions =&gt; ["locations.latitude &lt;= ? and locations.latitude &gt;= ? " + \&lt;br /&gt;			" AND locations.longitude &gt;= ? and locations.longitude &lt;= ?",&lt;br /&gt;			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]&lt;br /&gt;		]&lt;br /&gt;	)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Apr 2006 05:46:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1906</guid>
      <author>ajturner (Andrew Turner)</author>
    </item>
  </channel>
</rss>
