]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Use quadtiling for the node tables.
[rails.git] / app / models / old_node.rb
1 class OldNode < ActiveRecord::Base
2   set_table_name 'nodes'
3   
4   validates_presence_of :user_id, :timestamp
5   validates_inclusion_of :visible, :in => [ true, false ]
6   validates_numericality_of :latitude, :longitude
7   validate :validate_position
8
9   belongs_to :user
10  
11   before_save :update_tile
12
13   def self.find_by_area(minlat, minlon, maxlat, maxlon, options)
14     self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
15       return self.find(:all, options)
16     end
17   end
18
19   def update_tile
20     self.tile = QuadTile.tile_for_point(lat, lon)
21   end
22
23   def lat=(l)
24     self.latitude = (l * 10000000).round
25   end
26
27   def lon=(l)
28     self.longitude = (l * 10000000).round
29   end
30
31   def lat
32     return self.latitude.to_f / 10000000
33   end
34
35   def lon
36     return self.longitude.to_f / 10000000
37   end
38
39   def validate_position
40     errors.add_to_base("Node is not in the world") unless in_world?
41   end
42
43   def in_world?
44     return false if self.lat < -90 or self.lat > 90
45     return false if self.lon < -180 or self.lon > 180
46     return true
47   end
48
49   def self.from_node(node)
50     old_node = OldNode.new
51     old_node.latitude = node.latitude
52     old_node.longitude = node.longitude
53     old_node.visible = node.visible
54     old_node.tags = node.tags
55     old_node.timestamp = node.timestamp
56     old_node.user_id = node.user_id
57     old_node.id = node.id
58     return old_node
59   end
60
61   def to_xml_node
62     el1 = XML::Node.new 'node'
63     el1['id'] = self.id.to_s
64     el1['lat'] = self.lat.to_s
65     el1['lon'] = self.lon.to_s
66     el1['user'] = self.user.display_name if self.user.data_public?
67     Node.split_tags(el1, self.tags)
68     el1['visible'] = self.visible.to_s
69     el1['timestamp'] = self.timestamp.xmlschema
70     return el1
71   end
72 end