1 class Node < ActiveRecord::Base
3 set_table_name 'current_nodes'
6 validates_numericality_of :latitude
7 validates_numericality_of :longitude
8 # FIXME validate lat and lon within the world
10 has_many :old_nodes, :foreign_key => :id
13 def self.from_xml(xml, create=false)
20 doc.find('//osm/node').each do |pt|
23 node.latitude = pt['lat'].to_f
24 node.longitude = pt['lon'].to_f
26 if node.latitude > 90 or node.latitude < -90 or node.longitude > 180 or node.longitude < -180
32 node.id = pt['id'].to_i
36 node.visible = pt['visible'] and pt['visible'] == 'true'
39 node.timestamp = Time.now
42 node.timestamp = Time.parse(pt['timestamp'])
48 pt.find('tag').each do |tag|
49 tags << [tag['k'],tag['v']]
52 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
53 tags = '' if tags.nil?
64 self.timestamp = Time.now
66 old_node = OldNode.from_node(self)
70 rescue Exception => ex
76 doc = OSM::API.new.get_xml_doc
77 doc.root << to_xml_node()
81 def to_xml_node(user_display_name_cache = nil)
82 el1 = XML::Node.new 'node'
83 el1['id'] = self.id.to_s
84 el1['lat'] = self.latitude.to_s
85 el1['lon'] = self.longitude.to_s
87 user_display_name_cache = {} if user_display_name_cache.nil?
89 if user_display_name_cache and user_display_name_cache[self.user_id]
90 # use the cache if available
91 elsif self.user.data_public?
92 user_display_name_cache[self.user_id] = self.user.display_name
94 user_display_name_cache[self.user_id] = nil
97 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
99 Node.split_tags(el1, self.tags)
100 el1['visible'] = self.visible.to_s
101 el1['timestamp'] = self.timestamp.xmlschema
105 def self.split_tags(el, tags)
106 tags.split(';').each do |tag|
107 parts = tag.split('=')
110 key = parts[0].strip unless parts[0].nil?
111 val = parts[1].strip unless parts[1].nil?
112 if key != '' && val != ''
113 el2 = XML::Node.new('tag')