]> git.openstreetmap.org Git - rails.git/blob - app/models/node.rb
autocomplete (not compiled for now) and start of tile support
[rails.git] / app / models / node.rb
1 class Node < GeoRecord
2   require 'xml/libxml'
3
4   set_table_name 'current_nodes'
5   
6   validates_presence_of :user_id, :timestamp
7   validates_inclusion_of :visible, :in => [ true, false ]
8   validates_numericality_of :latitude, :longitude
9   validate :validate_position
10
11   has_many :old_nodes, :foreign_key => :id
12   belongs_to :user
13  
14   def validate_position
15     errors.add_to_base("Node is not in the world") unless in_world?
16   end
17
18   def in_world?
19     return false if self.lat < -90 or self.lat > 90
20     return false if self.lon < -180 or self.lon > 180
21     return true
22   end
23
24   def self.from_xml(xml, create=false)
25     begin
26       p = XML::Parser.new
27       p.string = xml
28       doc = p.parse
29   
30       node = Node.new
31
32       doc.find('//osm/node').each do |pt|
33         node.lat = pt['lat'].to_f
34         node.lon = pt['lon'].to_f
35
36         return nil unless node.in_world?
37
38         unless create
39           if pt['id'] != '0'
40             node.id = pt['id'].to_i
41           end
42         end
43
44         node.visible = pt['visible'] and pt['visible'] == 'true'
45
46         if create
47           node.timestamp = Time.now
48         else
49           if pt['timestamp']
50             node.timestamp = Time.parse(pt['timestamp'])
51           end
52         end
53
54         tags = []
55
56         pt.find('tag').each do |tag|
57           tags << [tag['k'],tag['v']]
58         end
59
60         node.tags = Tags.join(tags)
61       end
62     rescue
63       node = nil
64     end
65
66     return node
67   end
68
69   def save_with_history!
70     Node.transaction do
71       self.timestamp = Time.now
72       self.save!
73       old_node = OldNode.from_node(self)
74       old_node.save!
75     end
76   end
77
78   def to_xml
79     doc = OSM::API.new.get_xml_doc
80     doc.root << to_xml_node()
81     return doc
82   end
83
84   def to_xml_node(user_display_name_cache = nil)
85     el1 = XML::Node.new 'node'
86     el1['id'] = self.id.to_s
87     el1['lat'] = self.lat.to_s
88     el1['lon'] = self.lon.to_s
89
90     user_display_name_cache = {} if user_display_name_cache.nil?
91
92     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
93       # use the cache if available
94     elsif self.user.data_public?
95       user_display_name_cache[self.user_id] = self.user.display_name
96     else
97       user_display_name_cache[self.user_id] = nil
98     end
99
100     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
101
102     Tags.split(self.tags) do |k,v|
103       el2 = XML::Node.new('tag')
104       el2['k'] = k.to_s
105       el2['v'] = v.to_s
106       el1 << el2
107     end
108
109     el1['visible'] = self.visible.to_s
110     el1['timestamp'] = self.timestamp.xmlschema
111     return el1
112   end
113 end