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
31 node.id = pt['id'].to_i
34 node.visible = pt['visible'] and pt['visible'] == 'true'
37 node.timestamp = Time.now
40 node.timestamp = Time.parse(pt['timestamp'])
46 pt.find('tag').each do |tag|
47 tags << [tag['k'],tag['v']]
50 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
51 tags = '' if tags.nil?
63 old_node = OldNode.from_node(self)
67 rescue Exception => ex
73 doc = XML::Document.new
74 doc.encoding = 'UTF-8'
75 root = XML::Node.new 'osm'
76 root['version'] = '0.4'
77 root['generator'] = 'OpenStreetMap server'
79 el1 = XML::Node.new 'node'
80 el1['id'] = self.id.to_s
81 el1['lat'] = self.latitude.to_s
82 el1['lon'] = self.longitude.to_s
83 Node.split_tags(el1, self.tags)
84 el1['visible'] = self.visible.to_s
85 el1['timestamp'] = self.timestamp.xmlschema
90 def self.split_tags(el, tags)
91 tags.split(';').each do |tag|
92 parts = tag.split('=')
95 key = parts[0].strip unless parts[0].nil?
96 val = parts[1].strip unless parts[1].nil?
97 if key != '' && val != ''