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?
74 self.timestamp = Time.now
76 old_node = OldNode.from_node(self)
87 doc = OSM::API.new.get_xml_doc
88 doc.root << to_xml_node()
92 def to_xml_node(user_display_name_cache = nil)
93 el1 = XML::Node.new 'node'
94 el1['id'] = self.id.to_s
95 el1['lat'] = self.latitude.to_s
96 el1['lon'] = self.longitude.to_s
98 user_display_name_cache = {} if user_display_name_cache.nil?
100 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
101 # use the cache if available
102 elsif self.user.data_public?
103 user_display_name_cache[self.user_id] = self.user.display_name
105 user_display_name_cache[self.user_id] = nil
108 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
110 Node.split_tags(el1, self.tags)
111 el1['visible'] = self.visible.to_s
112 el1['timestamp'] = self.timestamp.xmlschema
116 def self.split_tags(el, tags)
117 tags.split(';').each do |tag|
118 parts = tag.split('=')
121 key = parts[0].strip unless parts[0].nil?
122 val = parts[1].strip unless parts[1].nil?
123 if key != '' && val != ''
124 el2 = XML::Node.new('tag')