1 class Node < ActiveRecord::Base
3 set_table_name 'current_nodes'
5 validates_numericality_of :latitude
6 validates_numericality_of :longitude
7 # FIXME validate lat and lon within the world
9 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 = XML::Document.new
76 doc.encoding = 'UTF-8'
77 root = XML::Node.new 'osm'
78 root['version'] = API_VERSION
79 root['generator'] = 'OpenStreetMap server'
86 el1 = XML::Node.new 'node'
87 el1['id'] = self.id.to_s
88 el1['lat'] = self.latitude.to_s
89 el1['lon'] = self.longitude.to_s
90 Node.split_tags(el1, self.tags)
91 el1['visible'] = self.visible.to_s
92 el1['timestamp'] = self.timestamp.xmlschema
96 def self.split_tags(el, tags)
97 tags.split(';').each do |tag|
98 parts = tag.split('=')
101 key = parts[0].strip unless parts[0].nil?
102 val = parts[1].strip unless parts[1].nil?
103 if key != '' && val != ''
104 el2 = Node.new('tag')