1 class Node < ActiveRecord::Base
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
15 has_many :old_nodes, :foreign_key => :id
18 has_many :ways, :through => :way_nodes
20 has_many :old_way_nodes
21 has_many :ways_via_history, :class_name=> "Way", :through => :old_way_nodes, :source => :way
23 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
24 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
26 # Sanity check the latitude and longitude and add an error if it's broken
28 errors.add_to_base("Node is not in the world") unless in_world?
32 # Search for nodes matching tags within bounding_box
34 # Also adheres to limitations such as within max_number_of_nodes
36 def self.search(bounding_box, tags = {})
37 min_lon, min_lat, max_lon, max_lat = *bounding_box
38 # @fixme a bit of a hack to search for only visible nodes
39 # couldn't think of another to add to tags condition
40 #conditions_hash = tags.merge({ 'visible' => 1 })
42 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
46 #conditions_hash.each do |key,value|
47 # keys << "#{key} = :#{key}"
48 # values[key.to_sym] = value
50 #conditions = keys.join(' AND ')
52 find_by_area(min_lat, min_lon, max_lat, max_lon,
53 :conditions => 'visible = 1',
54 :limit => APP_CONFIG['max_number_of_nodes']+1)
57 # Read in xml as text and return it's Node object representation
58 def self.from_xml(xml, create=false)
60 p = XML::Parser.string(xml)
65 doc.find('//osm/node').each do |pt|
66 node.lat = pt['lat'].to_f
67 node.lon = pt['lon'].to_f
69 return nil unless node.in_world?
73 node.id = pt['id'].to_i
77 node.visible = pt['visible'] and pt['visible'] == 'true'
80 node.timestamp = Time.now
83 node.timestamp = Time.parse(pt['timestamp'])
89 pt.find('tag').each do |tag|
90 tags << [tag['k'],tag['v']]
93 node.tags = Tags.join(tags)
102 # Save this node with the appropriate OldNode object to represent it's history.
103 def save_with_history!
105 self.timestamp = Time.now
107 old_node = OldNode.from_node(self)
112 # Turn this Node in to a complete OSM XML object with <osm> wrapper
114 doc = OSM::API.new.get_xml_doc
115 doc.root << to_xml_node()
119 # Turn this Node in to an XML Node without the <osm> wrapper.
120 def to_xml_node(user_display_name_cache = nil)
121 el1 = XML::Node.new 'node'
122 el1['id'] = self.id.to_s
123 el1['lat'] = self.lat.to_s
124 el1['lon'] = self.lon.to_s
126 user_display_name_cache = {} if user_display_name_cache.nil?
128 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
129 # use the cache if available
130 elsif self.user.data_public?
131 user_display_name_cache[self.user_id] = self.user.display_name
133 user_display_name_cache[self.user_id] = nil
136 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
138 Tags.split(self.tags) do |k,v|
139 el2 = XML::Node.new('tag')
145 el1['visible'] = self.visible.to_s
146 el1['timestamp'] = self.timestamp.xmlschema
150 # Return the node's tags as a Hash of keys and their values
153 Tags.split(self.tags) do |k,v|