4 set_table_name 'current_nodes'
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
11 has_many :old_nodes, :foreign_key => :id
15 errors.add_to_base("Node is not in the world") unless in_world?
19 return false if self.lat < -90 or self.lat > 90
20 return false if self.lon < -180 or self.lon > 180
24 def self.from_xml(xml, create=false)
32 doc.find('//osm/node').each do |pt|
33 node.lat = pt['lat'].to_f
34 node.lon = pt['lon'].to_f
36 return nil unless node.in_world?
40 node.id = pt['id'].to_i
44 node.visible = pt['visible'] and pt['visible'] == 'true'
47 node.timestamp = Time.now
50 node.timestamp = Time.parse(pt['timestamp'])
56 pt.find('tag').each do |tag|
57 tags << [tag['k'],tag['v']]
60 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
61 tags = '' if tags.nil?
72 def save_with_history!
74 self.timestamp = Time.now
76 old_node = OldNode.from_node(self)
82 doc = OSM::API.new.get_xml_doc
83 doc.root << to_xml_node()
87 def to_xml_node(user_display_name_cache = nil)
88 el1 = XML::Node.new 'node'
89 el1['id'] = self.id.to_s
90 el1['lat'] = self.lat.to_s
91 el1['lon'] = self.lon.to_s
93 user_display_name_cache = {} if user_display_name_cache.nil?
95 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
96 # use the cache if available
97 elsif self.user.data_public?
98 user_display_name_cache[self.user_id] = self.user.display_name
100 user_display_name_cache[self.user_id] = nil
103 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
105 Node.split_tags(el1, self.tags)
106 el1['visible'] = self.visible.to_s
107 el1['timestamp'] = self.timestamp.xmlschema
111 def self.split_tags(el, tags)
112 tags.split(';').each do |tag|
113 parts = tag.split('=')
116 key = parts[0].strip unless parts[0].nil?
117 val = parts[1].strip unless parts[1].nil?
118 if key != '' && val != ''
119 el2 = XML::Node.new('tag')