1 class Way < ActiveRecord::Base
6 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
7 has_many :way_tags, :foreign_key => 'id'
9 has_many :old_ways, :foreign_key => 'id', :order => 'version'
11 set_table_name 'current_ways'
13 def self.from_xml(xml, create=false)
21 doc.find('//osm/way').each do |pt|
22 if !create and pt['id'] != '0'
23 way.id = pt['id'].to_i
27 way.timestamp = Time.now
31 way.timestamp = Time.parse(pt['timestamp'])
35 pt.find('tag').each do |tag|
36 way.add_tag_keyval(tag['k'], tag['v'])
39 pt.find('nd').each do |nd|
40 way.add_nd_num(nd['ref'])
50 # Find a way given it's ID, and in a single SQL call also grab its nodes
53 # 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
54 def self.find_eager(id)
55 way = Way.find(id, :include => {:way_nodes => :node})
56 #If waytag had a multipart key that was real, you could do this:
57 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
60 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
62 doc = OSM::API.new.get_xml_doc
63 doc.root << to_xml_node()
67 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
68 el1 = XML::Node.new 'way'
69 el1['id'] = self.id.to_s
70 el1['visible'] = self.visible.to_s
71 el1['timestamp'] = self.timestamp.xmlschema
73 user_display_name_cache = {} if user_display_name_cache.nil?
75 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
76 # use the cache if available
77 elsif self.user.data_public?
78 user_display_name_cache[self.user_id] = self.user.display_name
80 user_display_name_cache[self.user_id] = nil
83 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
85 # make sure nodes are output in sequence_id order
87 self.way_nodes.each do |nd|
89 # if there is a list of visible nodes then use that to weed out deleted nodes
90 if visible_nodes[nd.node_id]
91 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
94 # otherwise, manually go to the db to check things
95 if nd.node.visible? and nd.node.visible?
96 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
101 ordered_nodes.each do |nd_id|
102 if nd_id and nd_id != '0'
103 e = XML::Node.new 'nd'
109 self.way_tags.each do |tag|
110 e = XML::Node.new 'tag'
121 self.way_nodes.each do |nd|
131 self.way_tags.each do |tag|
147 @nds = Array.new unless @nds
151 def add_tag_keyval(k, v)
152 @tags = Hash.new unless @tags
156 def save_with_history!
164 WayTag.transaction do
167 WayTag.delete_all(['id = ?', self.id])
178 WayNode.transaction do
181 WayNode.delete_all(['id = ?', self.id])
186 nd.id = [self.id, sequence]
193 old_way = OldWay.from_way(self)
194 old_way.timestamp = t
195 old_way.save_with_dependencies!
198 def preconditions_ok?
199 return false if self.nds.empty?
201 node = Node.find(:first, :conditions => ["id = ?", n])
202 unless node and node.visible
209 # 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.
210 def delete_with_relations_and_history(user)
213 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
214 :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
215 raise OSM::APIPreconditionFailedError
217 self.user_id = user.id
221 self.save_with_history!
224 raise OSM::APIAlreadyDeletedError
228 # delete a way and it's nodes that aren't part of other ways, with history
229 # WARNING, INCOMPLETE - Read the code
230 def delete_with_relations_and_nodes_and_history(user)
231 return false # until complete, just return false
233 node_ids = self.nodes.collect {|node| node.id }
234 node_ids_not_to_delete = []
235 way_nodes = WayNode.find(:all, :conditions => "node_id in [#{node_ids.join(',')}] and id not #{self.id}")
237 node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id}
239 nodes_to_delete = node_ids - node_ids_not_to_delete
241 # update the visibility etc on the current nodes
242 update_time = Time.now()
244 Node.update(node_ids_to_delete, {:user_id => user.id, :timestamp => update_time, :visibility => false})
248 old_nodes_to_create = []
249 OldNode.find(node_ids_to_delete).each do |old_node|
250 old_nodes_to_create << {:id => old_node.id, :timestamp => update_time, :latitude => old_node.latitude, :longitude => old_node.longitude, :visible => false}
253 OldNode.create(old_nodes_to_create)
255 # FIXME - the old nodes need to have their tile updated and I have no idea how that works
256 # they also need their tags copied over, and that depends on normalising the tags out to their own table that nicks working on
258 # finally, delete the way
260 self.delete_with_relations_and_history