1 # The node model represents a current existing node, that is, the latest version. Use OldNode for historical nodes.
6 set_table_name 'current_nodes'
8 validates_presence_of :user_id, :timestamp
9 validates_inclusion_of :visible, :in => [ true, false ]
10 validates_numericality_of :latitude, :longitude
11 validate :validate_position
13 has_many :old_nodes, :foreign_key => :id
17 # Sanity check the latitude and longitude and add an error if it's broken
19 errors.add_to_base("Node is not in the world") unless in_world?
22 # Is this node withing -90 > latitude > 90 and -180 > longitude > 180>
23 # * returns true/false
25 return false if self.lat < -90 or self.lat > 90
26 return false if self.lon < -180 or self.lon > 180
30 # Read in xml as text and return it's Node object representation
31 def self.from_xml(xml, create=false)
39 doc.find('//osm/node').each do |pt|
40 node.lat = pt['lat'].to_f
41 node.lon = pt['lon'].to_f
43 return nil unless node.in_world?
47 node.id = pt['id'].to_i
51 node.visible = pt['visible'] and pt['visible'] == 'true'
54 node.timestamp = Time.now
57 node.timestamp = Time.parse(pt['timestamp'])
63 pt.find('tag').each do |tag|
64 tags << [tag['k'],tag['v']]
67 node.tags = Tags.join(tags)
76 # Save this node with the appropriate OldNode object to represent it's history.
77 def save_with_history!
79 self.timestamp = Time.now
81 old_node = OldNode.from_node(self)
86 # Turn this Node in to a complete OSM XML object with <osm> wrapper
88 doc = OSM::API.new.get_xml_doc
89 doc.root << to_xml_node()
93 # Turn this Node in to an XML Node without the <osm> wrapper.
94 def to_xml_node(user_display_name_cache = nil)
95 el1 = XML::Node.new 'node'
96 el1['id'] = self.id.to_s
97 el1['lat'] = self.lat.to_s
98 el1['lon'] = self.lon.to_s
100 user_display_name_cache = {} if user_display_name_cache.nil?
102 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
103 # use the cache if available
104 elsif self.user.data_public?
105 user_display_name_cache[self.user_id] = self.user.display_name
107 user_display_name_cache[self.user_id] = nil
110 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
112 Tags.split(self.tags) do |k,v|
113 el2 = XML::Node.new('tag')
119 el1['visible'] = self.visible.to_s
120 el1['timestamp'] = self.timestamp.xmlschema
124 # Return the node's tags as a Hash of keys and their values
127 Tags.split(self.tags) do |k,v|