1 class Way < ActiveRecord::Base
4 include ConsistencyValidations
6 set_table_name 'current_ways'
8 validates_presence_of :changeset_id, :timestamp
9 validates_inclusion_of :visible, :in => [ true, false ]
13 has_many :old_ways, :foreign_key => 'id', :order => 'version'
15 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
16 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
18 has_many :way_tags, :foreign_key => 'id'
20 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
21 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
23 def self.from_xml(xml, create=false)
29 doc.find('//osm/way').each do |pt|
30 return Way.from_xml_node(pt, create)
37 def self.from_xml_node(pt, create=false)
40 if !create and pt['id'] != '0'
41 way.id = pt['id'].to_i
44 way.version = pt['version']
45 way.changeset_id = pt['changeset']
48 way.timestamp = Time.now
52 way.timestamp = Time.parse(pt['timestamp'])
56 pt.find('tag').each do |tag|
57 way.add_tag_keyval(tag['k'], tag['v'])
60 pt.find('nd').each do |nd|
61 way.add_nd_num(nd['ref'])
67 # Find a way given it's ID, and in a single SQL call also grab its nodes
70 # You can't pull in all the tags too unless we put a sequence_id on the way_tags table and have a multipart key
71 def self.find_eager(id)
72 way = Way.find(id, :include => {:way_nodes => :node})
73 #If waytag had a multipart key that was real, you could do this:
74 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
77 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
79 doc = OSM::API.new.get_xml_doc
80 doc.root << to_xml_node()
84 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
85 el1 = XML::Node.new 'way'
86 el1['id'] = self.id.to_s
87 el1['visible'] = self.visible.to_s
88 el1['timestamp'] = self.timestamp.xmlschema
89 el1['version'] = self.version.to_s
90 el1['changeset'] = self.changeset_id.to_s
92 user_display_name_cache = {} if user_display_name_cache.nil?
94 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
95 # use the cache if available
96 elsif self.changeset.user.data_public?
97 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
99 user_display_name_cache[self.changeset.user_id] = nil
102 el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
104 # make sure nodes are output in sequence_id order
106 self.way_nodes.each do |nd|
108 # if there is a list of visible nodes then use that to weed out deleted nodes
109 if visible_nodes[nd.node_id]
110 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
113 # otherwise, manually go to the db to check things
114 if nd.node.visible? and nd.node.visible?
115 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
120 ordered_nodes.each do |nd_id|
121 if nd_id and nd_id != '0'
122 e = XML::Node.new 'nd'
128 self.way_tags.each do |tag|
129 e = XML::Node.new 'tag'
140 self.way_nodes.each do |nd|
150 self.way_tags.each do |tag|
166 @nds = Array.new unless @nds
170 def add_tag_keyval(k, v)
171 @tags = Hash.new unless @tags
173 # duplicate tags are now forbidden, so we can't allow values
174 # in the hash to be overwritten.
175 raise OSM::APIDuplicateTagsError.new if @tags.include? k
180 def save_with_history!
189 WayTag.delete_all(['id = ?', self.id])
199 WayNode.delete_all(['id = ?', self.id])
203 nd.id = [self.id, sequence]
209 old_way = OldWay.from_way(self)
210 old_way.timestamp = t
211 old_way.save_with_dependencies!
215 def update_from(new_way, user)
216 check_consistency(self, new_way, user)
217 if !new_way.preconditions_ok?
218 raise OSM::APIPreconditionFailedError.new
220 self.changeset_id = new_way.changeset_id
221 self.tags = new_way.tags
222 self.nds = new_way.nds
227 def create_with_history(user)
228 check_create_consistency(self, user)
229 if !self.preconditions_ok?
230 raise OSM::APIPreconditionFailedError.new
237 def preconditions_ok?
238 return false if self.nds.empty?
240 node = Node.find(:first, :conditions => ["id = ?", n])
241 unless node and node.visible
248 def delete_with_history!(new_way, user)
249 check_consistency(self, new_way, user)
251 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
252 :conditions => [ "visible = 1 AND member_type='way' and member_id=? ", self.id])
253 raise OSM::APIPreconditionFailedError
255 self.changeset_id = new_way.changeset_id
259 self.save_with_history!
262 raise OSM::APIAlreadyDeletedError
266 # delete a way and it's nodes that aren't part of other ways, with history
268 # FIXME: merge the potlatch code to delete the relations
269 def delete_with_relations_and_nodes_and_history(user)
270 # delete the nodes not used by other ways
271 self.unshared_node_ids.each do |node_id|
272 n = Node.find(node_id)
278 # FIXME needs more information passed in so that the changeset can be updated
279 self.user_id = user.id
281 self.delete_with_history(user)
284 # Find nodes that belong to this way only
285 def unshared_node_ids
286 node_ids = self.nodes.collect { |node| node.id }
288 unless node_ids.empty?
289 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
290 node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
296 # Temporary method to match interface to nodes