1 class Node < ActiveRecord::Base
3 set_table_name 'current_nodes'
5 validates_presence_of :user_id, :timestamp
6 validates_inclusion_of :visible, :in => [ true, false ]
7 validates_numericality_of :latitude, :longitude
8 validate :validate_position
10 has_many :old_nodes, :foreign_key => :id
14 errors.add_to_base("Node is not in the world") unless in_world?
18 return false if self.latitude < -90 or self.latitude > 90
19 return false if self.longitude < -180 or self.longitude > 180
23 def self.from_xml(xml, create=false)
31 doc.find('//osm/node').each do |pt|
32 node.latitude = pt['lat'].to_f
33 node.longitude = pt['lon'].to_f
35 return nil unless node.in_world?
39 node.id = pt['id'].to_i
43 node.visible = pt['visible'] and pt['visible'] == 'true'
46 node.timestamp = Time.now
49 node.timestamp = Time.parse(pt['timestamp'])
55 pt.find('tag').each do |tag|
56 tags << [tag['k'],tag['v']]
59 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
60 tags = '' if tags.nil?
71 def save_with_history!
73 self.timestamp = Time.now
75 old_node = OldNode.from_node(self)
81 doc = OSM::API.new.get_xml_doc
82 doc.root << to_xml_node()
86 def to_xml_node(user_display_name_cache = nil)
87 el1 = XML::Node.new 'node'
88 el1['id'] = self.id.to_s
89 el1['lat'] = self.latitude.to_s
90 el1['lon'] = self.longitude.to_s
92 user_display_name_cache = {} if user_display_name_cache.nil?
94 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
95 # use the cache if available
96 elsif self.user.data_public?
97 user_display_name_cache[self.user_id] = self.user.display_name
99 user_display_name_cache[self.user_id] = nil
102 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
104 Node.split_tags(el1, self.tags)
105 el1['visible'] = self.visible.to_s
106 el1['timestamp'] = self.timestamp.xmlschema
110 def self.split_tags(el, tags)
111 tags.split(';').each do |tag|
112 parts = tag.split('=')
115 key = parts[0].strip unless parts[0].nil?
116 val = parts[1].strip unless parts[1].nil?
117 if key != '' && val != ''
118 el2 = XML::Node.new('tag')