Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Turner http://highearthorbit.com

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Location queries for a model

// These are class functions which are useful for finding locations in a database
// when PostGIS is not available (when using MySQL for example)

   1  
   2  def self.deg2rad(deg)
   3  	(deg * Math::PI / 180)
   4  end
   5  
   6  def self.rad2deg(rad)
   7  	(rad * 180 / Math::PI)
   8  end
   9  
  10  def self.acos(rad)
  11  	Math.atan2(Math.sqrt(1 - rad**2), rad)
  12  end
  13  
  14  def self.distance_in_miles(loc1, loc2)
  15  	lat1 = loc1.latitude
  16  	lon1 = loc1.longitude
  17  	lat2 = loc2.latitude
  18  	lon2 = loc2.longitude
  19  	theta = lon1 - lon2
  20  	
  21  	dist = Math.sin(self.deg2rad(lat1)) * Math.sin(deg2rad(lat2)) 
  22  		+ Math.cos(self.deg2rad(lat1)) * Math.cos(self.deg2rad(lat2)) * Math.cos(deg2rad(theta))
  23  	
  24  	dist = self.rad2deg(self.acos(dist))
  25  	
  26  	(dist * 60 * 1.1515).round #distance in miles
  27  end
  28  
  29  def miles_to(location)
  30  	Location.distance_in_miles(self, location)
  31  end
  32  
  33  def self.locationArea(location, miles)
  34  	radius = miles.to_f
  35  	latR = radius / ((6076 / 5280) * 60)
  36  	lonR = radius / (((Math.cos(location.latitude * Math::PI / 180) * 6076) / 5280) * 60)
  37  	
  38  	{
  39  		:min_latitude => location.latitude - latR,
  40  		:min_longitude => location.longitude - lonR,
  41  		:max_latitude => location.latitude + latR,
  42  		:max_longitude => location.longitude + lonR
  43  	}
  44  end
  45  
  46  def self.location_ids_in_range(location, miles)
  47  	la = Location.locationArea(location, miles)
  48  	Location.find(:all,
  49  		:select => "locations.id",
  50  		:conditions => ["locations.latitude <= ? and locations.latitude >= ? " + \
  51  			" AND locations.longitude >= ? and locations.longitude <= ?",
  52  			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]
  53  		]
  54  	).collect { |l| l.id }
  55  end
  56  
  57  def self.locations_in_range(location, miles)
  58  	la = Location.locationArea(location, miles)
  59  	Location.find(:all,
  60  		:conditions => ["locations.latitude <= ? and locations.latitude >= ? " + \
  61  			" AND locations.longitude >= ? and locations.longitude <= ?",
  62  			la[:max_latitude], la[:min_latitude], la[:min_longitude], la[:max_longitude]
  63  		]
  64  	)
  65  end

US State Abbreviations to Full name

# US State abbreviations to full name
# state_name = state_abbr.[]("MI")

   1  
   2  state_abbr = {
   3    'AL' => 'Alabama',
   4    'AK' => 'Alaska',
   5    'AS' => 'America Samoa',
   6    'AZ' => 'Arizona',
   7    'AR' => 'Arkansas',
   8    'CA' => 'California',
   9    'CO' => 'Colorado',
  10    'CT' => 'Connecticut',
  11    'DE' => 'Delaware',
  12    'DC' => 'District of Columbia',
  13    'FM' => 'Micronesia1',
  14    'FL' => 'Florida',
  15    'GA' => 'Georgia',
  16    'GU' => 'Guam',
  17    'HI' => 'Hawaii',
  18    'ID' => 'Idaho',
  19    'IL' => 'Illinois',
  20    'IN' => 'Indiana',
  21    'IA' => 'Iowa',
  22    'KS' => 'Kansas',
  23    'KY' => 'Kentucky',
  24    'LA' => 'Louisiana',
  25    'ME' => 'Maine',
  26    'MH' => 'Islands1',
  27    'MD' => 'Maryland',
  28    'MA' => 'Massachusetts',
  29    'MI' => 'Michigan',
  30    'MN' => 'Minnesota',
  31    'MS' => 'Mississippi',
  32    'MO' => 'Missouri',
  33    'MT' => 'Montana',
  34    'NE' => 'Nebraska',
  35    'NV' => 'Nevada',
  36    'NH' => 'New Hampshire',
  37    'NJ' => 'New Jersey',
  38    'NM' => 'New Mexico',
  39    'NY' => 'New York',
  40    'NC' => 'North Carolina',
  41    'ND' => 'North Dakota',
  42    'OH' => 'Ohio',
  43    'OK' => 'Oklahoma',
  44    'OR' => 'Oregon',
  45    'PW' => 'Palau',
  46    'PA' => 'Pennsylvania',
  47    'PR' => 'Puerto Rico',
  48    'RI' => 'Rhode Island',
  49    'SC' => 'South Carolina',
  50    'SD' => 'South Dakota',
  51    'TN' => 'Tennessee',
  52    'TX' => 'Texas',
  53    'UT' => 'Utah',
  54    'VT' => 'Vermont',
  55    'VI' => 'Virgin Island',
  56    'VA' => 'Virginia',
  57    'WA' => 'Washington',
  58    'WV' => 'West Virginia',
  59    'WI' => 'Wisconsin',
  60    'WY' => 'Wyoming'
  61  }

Average US State Locations

# A hash table of average US State Locations (latitude, longitude)
# MI_Location = state_locations.[]("MI")

   1  
   2  state_locations = {
   3    'AK' => [61.3850,-152.2683],
   4    'AL' => [32.7990,-86.8073],
   5    'AR' => [34.9513,-92.3809],
   6    'AS' => [14.2417,-170.7197],
   7    'AZ' => [33.7712,-111.3877],
   8    'CA' => [36.1700,-119.7462],
   9    'CO' => [39.0646,-105.3272],
  10    'CT' => [41.5834,-72.7622],
  11    'DC' => [38.8964,-77.0262],
  12    'DE' => [39.3498,-75.5148],
  13    'FL' => [27.8333,-81.7170],
  14    'GA' => [32.9866,-83.6487],
  15    'HI' => [21.1098,-157.5311],
  16    'IA' => [42.0046,-93.2140],
  17    'ID' => [44.2394,-114.5103],
  18    'IL' => [40.3363,-89.0022],
  19    'IN' => [39.8647,-86.2604],
  20    'KS' => [38.5111,-96.8005],
  21    'KY' => [37.6690,-84.6514],
  22    'LA' => [31.1801,-91.8749],
  23    'MA' => [42.2373,-71.5314],
  24    'MD' => [39.0724,-76.7902],
  25    'ME' => [44.6074,-69.3977],
  26    'MI' => [43.3504,-84.5603],
  27    'MN' => [45.7326,-93.9196],
  28    'MO' => [38.4623,-92.3020],
  29    'MP' => [14.8058,145.5505],
  30    'MS' => [32.7673,-89.6812],
  31    'MT' => [46.9048,-110.3261],
  32    'NC' => [35.6411,-79.8431],
  33    'ND' => [47.5362,-99.7930],
  34    'NE' => [41.1289,-98.2883],
  35    'NH' => [43.4108,-71.5653],
  36    'NJ' => [40.3140,-74.5089],
  37    'NM' => [34.8375,-106.2371],
  38    'NV' => [38.4199,-117.1219],
  39    'NY' => [42.1497,-74.9384],
  40    'OH' => [40.3736,-82.7755],
  41    'OK' => [35.5376,-96.9247],
  42    'OR' => [44.5672,-122.1269],
  43    'PA' => [40.5773,-77.2640],
  44    'PR' => [18.2766,-66.3350],
  45    'RI' => [41.6772,-71.5101],
  46    'SC' => [33.8191,-80.9066],
  47    'SD' => [44.2853,-99.4632],
  48    'TN' => [35.7449,-86.7489],
  49    'TX' => [31.1060,-97.6475],
  50    'UT' => [40.1135,-111.8535],
  51    'VA' => [37.7680,-78.2057],
  52    'VI' => [18.0001,-64.8199],
  53    'VT' => [44.0407,-72.7093],
  54    'WA' => [47.3917,-121.5708],
  55    'WI' => [44.2563,-89.6385],
  56    'WV' => [38.4680,-80.9696],
  57    'WY' => [42.7475,-107.2085]
  58  }
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS