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 :node_tags, :foreign_key => :id
22 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
23 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
25 # Sanity check the latitude and longitude and add an error if it's broken
27 errors.add_to_base("Node is not in the world") unless in_world?
31 # Search for nodes matching tags within bounding_box
33 # Also adheres to limitations such as within max_number_of_nodes
35 def self.search(bounding_box, tags = {})
36 min_lon, min_lat, max_lon, max_lat = *bounding_box
37 # @fixme a bit of a hack to search for only visible nodes
38 # couldn't think of another to add to tags condition
39 #conditions_hash = tags.merge({ 'visible' => 1 })
41 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
45 #conditions_hash.each do |key,value|
46 # keys << "#{key} = :#{key}"
47 # values[key.to_sym] = value
49 #conditions = keys.join(' AND ')
51 find_by_area(min_lat, min_lon, max_lat, max_lon,
52 :conditions => 'visible = 1',
53 :limit => APP_CONFIG['max_number_of_nodes']+1)
56 # Read in xml as text and return it's Node object representation
57 def self.from_xml(xml, create=false)
63 doc.find('//osm/node').each do |pt|
64 return Node.from_xml_node(pt, create)
71 def self.from_xml_node(pt, create=false)
74 node.version = pt['version']
75 node.lat = pt['lat'].to_f
76 node.lon = pt['lon'].to_f
78 return nil unless node.in_world?
82 node.id = pt['id'].to_i
86 node.visible = pt['visible'] and pt['visible'] == 'true'
89 node.timestamp = Time.now
92 node.timestamp = Time.parse(pt['timestamp'])
98 pt.find('tag').each do |tag|
99 node.add_tag_key_val(tag['k'],tag['v'])
105 def save_with_history!
114 NodeTag.delete_all(['id = ?', self.id])
124 old_node = OldNode.from_node(self)
125 old_node.timestamp = t
126 old_node.save_with_dependencies!
130 def delete_with_history(user)
132 if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = 1 AND current_way_nodes.node_id = ?", self.id ])
133 raise OSM::APIPreconditionFailedError.new
134 elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='node' and member_id=?", self.id])
135 raise OSM::APIPreconditionFailedError.new
137 self.user_id = user.id
142 raise OSM::APIAlreadyDeletedError.new
146 def update_from(new_node, user)
147 if new_node.version != version
148 raise OSM::APIVersionMismatchError.new(new_node.version, version)
151 self.user_id = user.id
152 self.latitude = new_node.latitude
153 self.longitude = new_node.longitude
154 self.tags = new_node.tags
160 doc = OSM::API.new.get_xml_doc
161 doc.root << to_xml_node()
165 def to_xml_node(user_display_name_cache = nil)
166 el1 = XML::Node.new 'node'
167 el1['id'] = self.id.to_s
168 el1['lat'] = self.lat.to_s
169 el1['lon'] = self.lon.to_s
171 user_display_name_cache = {} if user_display_name_cache.nil?
173 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
174 # use the cache if available
175 elsif self.user.data_public?
176 user_display_name_cache[self.user_id] = self.user.display_name
178 user_display_name_cache[self.user_id] = nil
181 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
183 self.tags.each do |k,v|
184 el2 = XML::Node.new('tag')
190 el1['visible'] = self.visible.to_s
191 el1['timestamp'] = self.timestamp.xmlschema
192 el1['version'] = self.version.to_s
203 self.node_tags.each do |tag|
214 def add_tag_key_val(k,v)
215 @tags = Hash.new unless @tags