1 class Node < ActiveRecord::Base
5 include ConsistencyValidations
7 set_table_name 'current_nodes'
9 validates_presence_of :changeset_id, :timestamp
10 validates_inclusion_of :visible, :in => [ true, false ]
11 validates_numericality_of :latitude, :longitude
12 validate :validate_position
16 has_many :old_nodes, :foreign_key => :id
19 has_many :ways, :through => :way_nodes
21 has_many :node_tags, :foreign_key => :id
23 has_many :old_way_nodes
24 has_many :ways_via_history, :class_name=> "Way", :through => :old_way_nodes, :source => :way
26 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
27 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
29 # Sanity check the latitude and longitude and add an error if it's broken
31 errors.add_to_base("Node is not in the world") unless in_world?
35 # Search for nodes matching tags within bounding_box
37 # Also adheres to limitations such as within max_number_of_nodes
39 def self.search(bounding_box, tags = {})
40 min_lon, min_lat, max_lon, max_lat = *bounding_box
41 # @fixme a bit of a hack to search for only visible nodes
42 # couldn't think of another to add to tags condition
43 #conditions_hash = tags.merge({ 'visible' => 1 })
45 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
49 #conditions_hash.each do |key,value|
50 # keys << "#{key} = :#{key}"
51 # values[key.to_sym] = value
53 #conditions = keys.join(' AND ')
55 find_by_area(min_lat, min_lon, max_lat, max_lon,
56 :conditions => {:visible => true},
57 :limit => APP_CONFIG['max_number_of_nodes']+1)
60 # Read in xml as text and return it's Node object representation
61 def self.from_xml(xml, create=false)
67 doc.find('//osm/node').each do |pt|
68 return Node.from_xml_node(pt, create)
75 def self.from_xml_node(pt, create=false)
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?
84 # version must be present unless creating
85 return nil unless create or not pt['version'].nil?
86 node.version = create ? 0 : pt['version'].to_i
90 node.id = pt['id'].to_i
94 # visible if it says it is, or as the default if the attribute
96 node.visible = pt['visible'].nil? or pt['visible'] == 'true'
99 node.timestamp = Time.now
102 node.timestamp = Time.parse(pt['timestamp'])
108 pt.find('tag').each do |tag|
109 node.add_tag_key_val(tag['k'],tag['v'])
116 # the bounding box around a node
118 [ longitude, latitude, longitude, latitude ]
121 def save_with_history!
130 NodeTag.delete_all(['id = ?', self.id])
140 old_node = OldNode.from_node(self)
141 old_node.timestamp = t
142 old_node.save_with_dependencies!
144 # tell the changeset we updated one element only
145 changeset.add_changes! 1
147 # save the changeset in case of bounding box updates
152 # Should probably be renamed delete_from to come in line with update
153 def delete_with_history!(new_node, user)
155 check_consistency(self, new_node, user)
156 if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ])
157 raise OSM::APIPreconditionFailedError.new
158 elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='node' and member_id=? ", true, self.id])
159 raise OSM::APIPreconditionFailedError.new
161 self.changeset_id = new_node.changeset_id
164 # update the changeset with the deleted position
165 changeset.update_bbox!(bbox)
170 raise OSM::APIAlreadyDeletedError.new
174 def update_from(new_node, user)
175 check_consistency(self, new_node, user)
177 # update changeset with *old* position first
178 changeset.update_bbox!(bbox);
180 # FIXME logic needs to be double checked
181 self.changeset_id = new_node.changeset_id
182 self.latitude = new_node.latitude
183 self.longitude = new_node.longitude
184 self.tags = new_node.tags
187 # update changeset with *new* position
188 changeset.update_bbox!(bbox);
193 def create_with_history(user)
194 check_create_consistency(self, user)
198 # update the changeset to include the new location
199 changeset.update_bbox!(bbox)
205 doc = OSM::API.new.get_xml_doc
206 doc.root << to_xml_node()
210 def to_xml_node(user_display_name_cache = nil)
211 el1 = XML::Node.new 'node'
212 el1['id'] = self.id.to_s
213 el1['lat'] = self.lat.to_s
214 el1['lon'] = self.lon.to_s
215 el1['version'] = self.version.to_s
216 el1['changeset'] = self.changeset_id.to_s
218 user_display_name_cache = {} if user_display_name_cache.nil?
220 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
221 # use the cache if available
222 elsif self.changeset.user.data_public?
223 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
225 user_display_name_cache[self.changeset.user_id] = nil
228 if not user_display_name_cache[self.changeset.user_id].nil?
229 el1['user'] = user_display_name_cache[self.changeset.user_id]
230 el1['uid'] = self.changeset.user_id.to_s
233 self.tags.each do |k,v|
234 el2 = XML::Node.new('tag')
240 el1['visible'] = self.visible.to_s
241 el1['timestamp'] = self.timestamp.xmlschema
252 self.node_tags.each do |tag|
263 def add_tag_key_val(k,v)
264 @tags = Hash.new unless @tags
266 # duplicate tags are now forbidden, so we can't allow values
267 # in the hash to be overwritten.
268 raise OSM::APIDuplicateTagsError.new if @tags.include? k
274 # are the preconditions OK? this is mainly here to keep the duck
275 # typing interface the same between nodes, ways and relations.
276 def preconditions_ok?
281 # dummy method to make the interfaces of node, way and relation
283 def fix_placeholders!(id_map)
284 # nodes don't refer to anything, so there is nothing to do here