1 class Node < ActiveRecord::Base
5 set_table_name 'current_nodes'
7 validates_presence_of :user_id, :timestamp
8 validates_inclusion_of :visible, :in => [ true, false ]
9 validates_numericality_of :latitude, :longitude
10 validate :validate_position
12 has_many :old_nodes, :foreign_key => :id
14 has_many :node_tags, :foreign_key => :id
17 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
18 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
21 errors.add_to_base("Node is not in the world") unless in_world?
25 # Search for nodes matching tags within bounding_box
27 # Also adheres to limitations such as within max_number_of_nodes
29 def self.search(bounding_box, tags = {})
30 min_lon, min_lat, max_lon, max_lat = *bounding_box
31 # @fixme a bit of a hack to search for only visible nodes
32 # couldn't think of another to add to tags condition
33 #conditions_hash = tags.merge({ 'visible' => 1 })
35 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
39 #conditions_hash.each do |key,value|
40 # keys << "#{key} = :#{key}"
41 # values[key.to_sym] = value
43 #conditions = keys.join(' AND ')
45 find_by_area(min_lat, min_lon, max_lat, max_lon,
46 :conditions => 'visible = 1',
47 :limit => APP_CONFIG['max_number_of_nodes']+1)
50 # Read in xml as text and return it's Node object representation
51 def self.from_xml(xml, create=false)
57 doc.find('//osm/node').each do |pt|
58 return Node.from_xml_node(pt, create)
65 def self.from_xml_node(pt, create=false)
68 node.version = pt['version']
69 node.lat = pt['lat'].to_f
70 node.lon = pt['lon'].to_f
72 return nil unless node.in_world?
76 node.id = pt['id'].to_i
80 node.visible = pt['visible'] and pt['visible'] == 'true'
83 node.timestamp = Time.now
86 node.timestamp = Time.parse(pt['timestamp'])
92 pt.find('tag').each do |tag|
93 node.add_tag_key_val(tag['k'],tag['v'])
99 def save_with_history!
108 NodeTag.delete_all(['id = ?', self.id])
118 old_node = OldNode.from_node(self)
119 old_node.timestamp = t
120 old_node.save_with_dependencies!
124 def delete_with_history(user)
126 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 ])
127 raise OSM::APIPreconditionFailedError.new
128 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])
129 raise OSM::APIPreconditionFailedError.new
131 self.user_id = user.id
136 raise OSM::APIAlreadyDeletedError.new
140 def update_from(new_node, user)
141 if new_node.version != version
142 raise OSM::APIVersionMismatchError.new(new_node.version, version)
145 self.user_id = user.id
146 self.latitude = new_node.latitude
147 self.longitude = new_node.longitude
148 self.tags = new_node.tags
154 doc = OSM::API.new.get_xml_doc
155 doc.root << to_xml_node()
159 def to_xml_node(user_display_name_cache = nil)
160 el1 = XML::Node.new 'node'
161 el1['id'] = self.id.to_s
162 el1['lat'] = self.lat.to_s
163 el1['lon'] = self.lon.to_s
165 user_display_name_cache = {} if user_display_name_cache.nil?
167 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
168 # use the cache if available
169 elsif self.user.data_public?
170 user_display_name_cache[self.user_id] = self.user.display_name
172 user_display_name_cache[self.user_id] = nil
175 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
177 self.tags.each do |k,v|
178 el2 = XML::Node.new('tag')
184 el1['visible'] = self.visible.to_s
185 el1['timestamp'] = self.timestamp.xmlschema
186 el1['version'] = self.version.to_s
197 self.node_tags.each do |tag|
208 def add_tag_key_val(k,v)
209 @tags = Hash.new unless @tags