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
14 def self.from_xml(xml, create=false)
21 doc.find('//osm/node').each do |pt|
24 node.latitude = pt['lat'].to_f
25 node.longitude = pt['lon'].to_f
27 if node.latitude > 90 or node.latitude < -90 or node.longitude > 180 or node.longitude < -180
33 node.id = pt['id'].to_i
37 node.visible = pt['visible'] and pt['visible'] == 'true'
40 node.timestamp = Time.now
43 node.timestamp = Time.parse(pt['timestamp'])
49 pt.find('tag').each do |tag|
50 tags << [tag['k'],tag['v']]
53 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
54 tags = '' if tags.nil?
66 old_node = OldNode.from_node(self)
70 rescue Exception => ex
76 doc = XML::Document.new
77 doc.encoding = 'UTF-8'
78 root = XML::Node.new 'osm'
79 root['version'] = API_VERSION
80 root['generator'] = 'OpenStreetMap server'
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
91 Node.split_tags(el1, self.tags)
92 el1['visible'] = self.visible.to_s
93 el1['timestamp'] = self.timestamp.xmlschema
97 def self.split_tags(el, tags)
98 tags.split(';').each do |tag|
99 parts = tag.split('=')
102 key = parts[0].strip unless parts[0].nil?
103 val = parts[1].strip unless parts[1].nil?
104 if key != '' && val != ''
105 el2 = XML::Node.new('tag')