1 class Node < ActiveRecord::Base
6 set_table_name 'current_nodes'
8 validates_presence_of :changeset_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 :old_way_nodes
23 has_many :ways_via_history, :class_name=> "Way", :through => :old_way_nodes, :source => :way
25 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
26 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
28 # Sanity check the latitude and longitude and add an error if it's broken
30 errors.add_to_base("Node is not in the world") unless in_world?
34 # Search for nodes matching tags within bounding_box
36 # Also adheres to limitations such as within max_number_of_nodes
38 def self.search(bounding_box, tags = {})
39 min_lon, min_lat, max_lon, max_lat = *bounding_box
40 # @fixme a bit of a hack to search for only visible nodes
41 # couldn't think of another to add to tags condition
42 #conditions_hash = tags.merge({ 'visible' => 1 })
44 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
48 #conditions_hash.each do |key,value|
49 # keys << "#{key} = :#{key}"
50 # values[key.to_sym] = value
52 #conditions = keys.join(' AND ')
54 find_by_area(min_lat, min_lon, max_lat, max_lon,
55 :conditions => 'visible = 1',
56 :limit => APP_CONFIG['max_number_of_nodes']+1)
59 # Read in xml as text and return it's Node object representation
60 def self.from_xml(xml, create=false)
66 doc.find('//osm/node').each do |pt|
67 return Node.from_xml_node(pt, create)
74 def self.from_xml_node(pt, create=false)
77 node.version = pt['version']
78 node.lat = pt['lat'].to_f
79 node.lon = pt['lon'].to_f
80 node.changeset_id = pt['changeset'].to_i
82 return nil unless node.in_world?
86 node.id = pt['id'].to_i
90 node.visible = pt['visible'] and pt['visible'] == 'true'
93 node.timestamp = Time.now
96 node.timestamp = Time.parse(pt['timestamp'])
102 pt.find('tag').each do |tag|
103 node.add_tag_key_val(tag['k'],tag['v'])
109 def save_with_history!
118 NodeTag.delete_all(['id = ?', self.id])
128 old_node = OldNode.from_node(self)
129 old_node.timestamp = t
130 old_node.save_with_dependencies!
134 def delete_with_history(user)
136 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 ])
137 raise OSM::APIPreconditionFailedError.new
138 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])
139 raise OSM::APIPreconditionFailedError.new
141 self.user_id = user.id
146 raise OSM::APIAlreadyDeletedError.new
150 def update_from(new_node, user)
151 if new_node.version != version
152 raise OSM::APIVersionMismatchError.new(new_node.version, version)
153 elsif new_node.changeset.user_id != user.id
154 raise OSM::APIUserChangesetMismatchError.new
155 elsif not new_node.changeset.open?
156 raise OSM::APIChangesetAlreadyClosedError.new
159 # FIXME logic need looked at
160 self.changeset_id = new_node.changeset_id
161 self.latitude = new_node.latitude
162 self.longitude = new_node.longitude
163 self.tags = new_node.tags
169 doc = OSM::API.new.get_xml_doc
170 doc.root << to_xml_node()
174 def to_xml_node(user_display_name_cache = nil)
175 el1 = XML::Node.new 'node'
176 el1['id'] = self.id.to_s
177 el1['lat'] = self.lat.to_s
178 el1['lon'] = self.lon.to_s
179 el1['version'] = self.version.to_s
180 el1['changeset'] = self.changeset_id.to_s
182 user_display_name_cache = {} if user_display_name_cache.nil?
184 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
185 # use the cache if available
186 elsif self.changeset.user.data_public?
187 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
189 user_display_name_cache[self.changeset.user_id] = nil
192 el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
194 self.tags.each do |k,v|
195 el2 = XML::Node.new('tag')
201 el1['visible'] = self.visible.to_s
202 el1['timestamp'] = self.timestamp.xmlschema
213 self.node_tags.each do |tag|
224 def add_tag_key_val(k,v)
225 @tags = Hash.new unless @tags