]> git.openstreetmap.org Git - rails.git/blob - app/models/geo_record.rb
Now all the unit tests work
[rails.git] / app / models / geo_record.rb
1 class GeoRecord < ActiveRecord::Base
2   before_save :update_tile
3
4   # This is a scaling factor for going between the lat and lon via the API
5   # and the longitude and latitude that is stored in the database
6   SCALE = 10000000
7
8   # Is this node within -90 <= latitude <= 90 and -180 <= longitude <= 180
9   # * returns true/false
10   def in_world?
11     return false if self.lat < -90 or self.lat > 90
12     return false if self.lon < -180 or self.lon > 180
13     return true
14   end
15
16   def self.find_by_area(minlat, minlon, maxlat, maxlon, options)
17     self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
18       return self.find(:all, options)
19     end
20   end
21
22   def update_tile
23     self.tile = QuadTile.tile_for_point(lat, lon)
24   end
25
26   def lat=(l)
27     self.latitude = (l * SCALE).round
28   end
29
30   def lon=(l)
31     self.longitude = (l * SCALE).round
32   end
33
34   # Return WGS84 latitude
35   def lat
36     return self.latitude.to_f / SCALE
37   end
38
39   # Return WGS84 longitude
40   def lon
41     return self.longitude.to_f / SCALE
42   end
43
44   # Potlatch projections
45   def lon_potlatch(baselong,masterscale)
46     (self.lon-baselong)*masterscale
47   end
48
49   def lat_potlatch(basey,masterscale)
50     -(lat2y(self.lat)-basey)*masterscale
51   end
52   
53   private
54   
55   def lat2y(a)
56     180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
57   end
58
59 end
60