]> git.openstreetmap.org Git - rails.git/blob - lib/geo_record.rb
Refactor to remove some code duplication.
[rails.git] / lib / geo_record.rb
1 module GeoRecord
2   def self.included(base)
3     base.extend(ClassMethods)
4   end
5
6   def before_save
7     self.update_tile
8   end
9
10   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
11   # * returns true/false
12   def in_world?
13     return false if self.lat < -90 or self.lat > 90
14     return false if self.lon < -180 or self.lon > 180
15     return true
16   end
17
18   def update_tile
19     self.tile = QuadTile.tile_for_point(lat, lon)
20   end
21
22   def lat=(l)
23     self.latitude = (l * 10000000).round
24   end
25
26   def lon=(l)
27     self.longitude = (l * 10000000).round
28   end
29
30   # Return WGS84 latitude
31   def lat
32     return self.latitude.to_f / 10000000
33   end
34
35   # Return WGS84 longitude
36   def lon
37     return self.longitude.to_f / 10000000
38   end
39
40   # Potlatch projections
41   def lon_potlatch(baselong,masterscale)
42     (self.lon-baselong)*masterscale
43   end
44
45   def lat_potlatch(basey,masterscale)
46     -(lat2y(self.lat)-basey)*masterscale
47   end
48   
49 private
50   
51   def lat2y(a)
52     180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
53   end
54
55   module ClassMethods
56     def find_by_area(minlat, minlon, maxlat, maxlon, options)
57       self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
58         return self.find(:all, options)
59       end
60     end
61   end
62 end
63