]> git.openstreetmap.org Git - rails.git/blob - lib/geo_record.rb
Fix to_xml method of old_relation model.
[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.extend(ClassMethods)
9   end
10
11   def before_save
12     self.update_tile
13   end
14
15   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
16   # * returns true/false
17   def in_world?
18     return false if self.lat < -90 or self.lat > 90
19     return false if self.lon < -180 or self.lon > 180
20     return true
21   end
22
23   def update_tile
24     self.tile = QuadTile.tile_for_point(lat, lon)
25   end
26
27   def lat=(l)
28     self.latitude = (l * SCALE).round
29   end
30
31   def lon=(l)
32     self.longitude = (l * SCALE).round
33   end
34
35   # Return WGS84 latitude
36   def lat
37     return self.latitude.to_f / SCALE
38   end
39
40   # Return WGS84 longitude
41   def lon
42     return self.longitude.to_f / SCALE
43   end
44
45   # Generic checks that are run for the updates and deletes of
46   # node, ways and relations. This code is here to avoid duplication, 
47   # and allow the extention of the checks without having to modify the
48   # code in 6 places for all the updates and deletes. Some of these tests are 
49   # needed for creates, but are currently not run :-( 
50   # This will throw an exception if there is an inconsistency
51   def check_consistency(old, new, user)
52     if new.version != old.version
53       raise OSM::APIVersionMismatchError.new(new.version, old.version)
54     elsif new.changeset.nil?
55       raise OSM::APIChangesetMissingError.new
56     elsif new.changeset.user_id != user.id
57       raise OSM::APIUserChangesetMismatchError.new
58     elsif not new.changeset.is_open?
59       raise OSM::APIChangesetAlreadyClosedError.new
60     end
61   end
62 private
63   
64   def lat2y(a)
65     180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
66   end
67
68   module ClassMethods
69     def find_by_area(minlat, minlon, maxlat, maxlon, options)
70       self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
71         return self.find(:all, options)
72       end
73     end
74   end
75 end
76