]> git.openstreetmap.org Git - rails.git/blob - lib/geo_record.rb
Rearrange layer switcher assets
[rails.git] / lib / geo_record.rb
1 module GeoRecord
2   # This scaling factor is used to convert between the float lat/lon that is 
3   # returned by the API, and the integer lat/lon equivalent that is stored in
4   # the database.
5   SCALE = 10000000
6   
7   def self.included(base)
8     base.scope :bbox, lambda { |bbox| base.where(OSM.sql_for_area(bbox)) }
9     base.before_save :update_tile
10   end
11
12   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
13   # * returns true/false
14   def in_world?
15     return false if self.lat < -90 or self.lat > 90
16     return false if self.lon < -180 or self.lon > 180
17     return true
18   end
19
20   def update_tile
21     self.tile = QuadTile.tile_for_point(lat, lon)
22   end
23
24   def lat=(l)
25     self.latitude = (l * SCALE).round
26   end
27
28   def lon=(l)
29     self.longitude = (l * SCALE).round
30   end
31
32   # Return WGS84 latitude
33   def lat
34     return self.latitude.to_f / SCALE
35   end
36
37   # Return WGS84 longitude
38   def lon
39     return self.longitude.to_f / SCALE
40   end
41
42 private
43   
44   def lat2y(a)
45     180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
46   end
47 end
48