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?
65 old_node = OldNode.from_node(self)
69 rescue Exception => ex
75 doc = OSM::API.new.get_xml_doc
76 doc.root << to_xml_node()
81 el1 = XML::Node.new 'node'
82 el1['id'] = self.id.to_s
83 el1['lat'] = self.latitude.to_s
84 el1['lon'] = self.longitude.to_s
85 el1['user'] = self.user.display_name if self.user.data_public?
86 Node.split_tags(el1, self.tags)
87 el1['visible'] = self.visible.to_s
88 el1['timestamp'] = self.timestamp.xmlschema
92 def self.split_tags(el, tags)
93 tags.split(';').each do |tag|
94 parts = tag.split('=')
97 key = parts[0].strip unless parts[0].nil?
98 val = parts[1].strip unless parts[1].nil?
99 if key != '' && val != ''
100 el2 = XML::Node.new('tag')