1 class Way < ActiveRecord::Base
4 set_table_name 'current_ways'
8 has_many :old_ways, :foreign_key => 'id', :order => 'version'
10 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
11 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
13 has_many :way_tags, :foreign_key => 'id'
15 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
16 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
18 def self.from_xml(xml, create=false)
20 p = XML::Parser.string(xml)
25 doc.find('//osm/way').each do |pt|
26 if !create and pt['id'] != '0'
27 way.id = pt['id'].to_i
31 way.timestamp = Time.now
35 way.timestamp = Time.parse(pt['timestamp'])
39 pt.find('tag').each do |tag|
40 way.add_tag_keyval(tag['k'], tag['v'])
43 pt.find('nd').each do |nd|
44 way.add_nd_num(nd['ref'])
54 # Find a way given it's ID, and in a single SQL call also grab its nodes
57 # 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
58 def self.find_eager(id)
59 way = Way.find(id, :include => {:way_nodes => :node})
60 #If waytag had a multipart key that was real, you could do this:
61 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
64 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
66 doc = OSM::API.new.get_xml_doc
67 doc.root << to_xml_node()
71 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
72 el1 = XML::Node.new 'way'
73 el1['id'] = self.id.to_s
74 el1['visible'] = self.visible.to_s
75 el1['timestamp'] = self.timestamp.xmlschema
77 user_display_name_cache = {} if user_display_name_cache.nil?
79 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
80 # use the cache if available
81 elsif self.user.data_public?
82 user_display_name_cache[self.user_id] = self.user.display_name
84 user_display_name_cache[self.user_id] = nil
87 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
89 # make sure nodes are output in sequence_id order
91 self.way_nodes.each do |nd|
93 # if there is a list of visible nodes then use that to weed out deleted nodes
94 if visible_nodes[nd.node_id]
95 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
98 # otherwise, manually go to the db to check things
99 if nd.node.visible? and nd.node.visible?
100 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
105 ordered_nodes.each do |nd_id|
106 if nd_id and nd_id != '0'
107 e = XML::Node.new 'nd'
113 self.way_tags.each do |tag|
114 e = XML::Node.new 'tag'
125 self.way_nodes.each do |nd|
135 self.way_tags.each do |tag|
151 @nds = Array.new unless @nds
155 def add_tag_keyval(k, v)
156 @tags = Hash.new unless @tags
160 def save_with_history!
168 WayTag.transaction do
171 WayTag.delete_all(['id = ?', self.id])
182 WayNode.transaction do
185 WayNode.delete_all(['id = ?', self.id])
190 nd.id = [self.id, sequence]
197 old_way = OldWay.from_way(self)
198 old_way.timestamp = t
199 old_way.save_with_dependencies!
202 def preconditions_ok?
203 return false if self.nds.empty?
205 node = Node.find(:first, :conditions => ["id = ?", n])
206 unless node and node.visible
213 # Delete the way and it's relations, but don't really delete it - set its visibility to false and update the history etc to maintain wiki-like functionality.
214 def delete_with_relations_and_history(user)
217 # this should actually delete the relations,
218 # not just throw a PreconditionFailed if it's a member of a relation!!
219 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
220 :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
221 raise OSM::APIPreconditionFailedError
224 self.user_id = user.id
228 self.save_with_history!
231 raise OSM::APIAlreadyDeletedError
235 # delete a way and it's nodes that aren't part of other ways, with history
236 def delete_with_relations_and_nodes_and_history(user)
237 # delete the nodes not used by other ways
238 self.unshared_node_ids.each do |node_id|
239 n = Node.find(node_id)
245 self.user_id = user.id
247 self.delete_with_relations_and_history(user)
250 # Find nodes that belong to this way only
251 def unshared_node_ids
252 node_ids = self.nodes.collect { |node| node.id }
254 unless node_ids.empty?
255 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
256 node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
262 # Temporary method to match interface to nodes