Class: SdbDal::Geo::Location
- Inherits:
-
Object
- Object
- SdbDal::Geo::Location
- Defined in:
- lib/sdb_dal/geo.rb
Instance Attribute Summary collapse
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
Class Method Summary collapse
Instance Method Summary collapse
- #distance_in_miles(loc2) ⇒ Object
- #get_nearby_addresses(zoom_level) ⇒ Object
-
#initialize(lat, lon) ⇒ Location
constructor
A new instance of Location.
- #to_address ⇒ Object
Constructor Details
#initialize(lat, lon) ⇒ Location
Returns a new instance of Location.
11 12 13 14 |
# File 'lib/sdb_dal/geo.rb', line 11 def initialize(lat,lon) self.latitude=lat self.longitude=lon end |
Instance Attribute Details
#latitude ⇒ Object
Returns the value of attribute latitude.
8 9 10 |
# File 'lib/sdb_dal/geo.rb', line 8 def latitude @latitude end |
#longitude ⇒ Object
Returns the value of attribute longitude.
9 10 11 |
# File 'lib/sdb_dal/geo.rb', line 9 def longitude @longitude end |
Class Method Details
.acos(rad) ⇒ Object
136 137 138 |
# File 'lib/sdb_dal/geo.rb', line 136 def self.acos(rad) Math.atan2(Math.sqrt(1 - rad**2), rad) end |
.deg2rad(deg) ⇒ Object
128 129 130 |
# File 'lib/sdb_dal/geo.rb', line 128 def self.deg2rad(deg) (deg * Math::PI / 180) end |
.rad2deg(rad) ⇒ Object
132 133 134 |
# File 'lib/sdb_dal/geo.rb', line 132 def self.rad2deg(rad) (rad * 180 / Math::PI) end |
.to_location(address) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sdb_dal/geo.rb', line 91 def Location.to_location(address) width=90.0 height=45.0 index=0 x=0 y=0 length=address.length-1 length.times do index+=1 puts address[index].chr case address[index].chr when 'r' x+=width y+=height when 'q' x-=width y+=height when 't' x-=width y-=height when 's' x+=width y-=height end width/=2 height/=2 end loc=Location.new loc.latitude=y loc.longitude=x return loc end |
Instance Method Details
#distance_in_miles(loc2) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/sdb_dal/geo.rb', line 140 def distance_in_miles( loc2) lat2 = loc2.latitude lon2 = loc2.longitude theta = self.longitude - lon2 dist = Math.sin(self.deg2rad(self.latitude)) * Math.sin(deg2rad(lat2)) + Math.cos(self.deg2rad(self.latitude)) * Math.cos(self.deg2rad(lat2)) * Math.cos(deg2rad(theta)) dist = self.rad2deg(self.acos(dist)) (dist * 60 * 1.1515).round #distance in miles end |
#get_nearby_addresses(zoom_level) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sdb_dal/geo.rb', line 53 def get_nearby_addresses(zoom_level) address=self.to_address if address.length>(zoom_level+1) address=address.slice(0,zoom_level+1) end result=[address] tileWidth=360.0/(2**zoom_level) tileHeight=180.0/(2**zoom_level) other=Geo::Location.new(self.latitude+tileHeight,self.longitude+tileWidth) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude+tileHeight,self.longitude) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude+tileHeight,self.longitude-tileWidth) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude-tileHeight,self.longitude+tileWidth) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude-tileHeight,self.longitude) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude-tileHeight,self.longitude-tileWidth) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude,self.longitude+tileWidth) result << other.to_address.slice(0,zoom_level+1) other=Geo::Location.new(self.latitude,self.longitude-tileWidth) result << other.to_address.slice(0,zoom_level+1) end |
#to_address ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sdb_dal/geo.rb', line 16 def to_address if latitude==nil || longitude==nil return nil end address='t' right=0 top=0 width=180.0 height=90.0 16.times do if latitude>top top=top+height/2.0 if longitude<right right=right-width/2.0 address<<'q' else right=right+width/2.0 address << 'r' end else top=top-height/2.0 if longitude<right right=right-width/2.0 address<<'t' else right=right+width/2.0 address << 's' end end width=width/2.0 height=height/2.0 # puts "#{address} w=#{width} h=#{height} r=#{right} t=#{top}" end return address end |