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'])
54 # if visible isn't present then it defaults to true
55 way.visible = (pt['visible'] or true)
58 pt.find('tag').each do |tag|
59 way.add_tag_keyval(tag['k'], tag['v'])
62 pt.find('nd').each do |nd|
63 way.add_nd_num(nd['ref'])
69 # Find a way given it's ID, and in a single SQL call also grab its nodes
72 # 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
73 def self.find_eager(id)
74 way = Way.find(id, :include => {:way_nodes => :node})
75 #If waytag had a multipart key that was real, you could do this:
76 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
79 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
81 doc = OSM::API.new.get_xml_doc
82 doc.root << to_xml_node()
86 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
87 el1 = XML::Node.new 'way'
88 el1['id'] = self.id.to_s
89 el1['visible'] = self.visible.to_s
90 el1['timestamp'] = self.timestamp.xmlschema
91 el1['version'] = self.version.to_s
92 el1['changeset'] = self.changeset_id.to_s
94 user_display_name_cache = {} if user_display_name_cache.nil?
96 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
97 # use the cache if available
98 elsif self.changeset.user.data_public?
99 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
101 user_display_name_cache[self.changeset.user_id] = nil
104 if not user_display_name_cache[self.changeset.user_id].nil?
105 el1['user'] = user_display_name_cache[self.changeset.user_id]
106 el1['uid'] = self.changeset.user_id.to_s
109 # make sure nodes are output in sequence_id order
111 self.way_nodes.each do |nd|
113 # if there is a list of visible nodes then use that to weed out deleted nodes
114 if visible_nodes[nd.node_id]
115 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
118 # otherwise, manually go to the db to check things
119 if nd.node and nd.node.visible?
120 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
125 ordered_nodes.each do |nd_id|
126 if nd_id and nd_id != '0'
127 e = XML::Node.new 'nd'
133 self.way_tags.each do |tag|
134 e = XML::Node.new 'tag'
145 self.way_nodes.each do |nd|
155 self.way_tags.each do |tag|
171 @nds = Array.new unless @nds
175 def add_tag_keyval(k, v)
176 @tags = Hash.new unless @tags
178 # duplicate tags are now forbidden, so we can't allow values
179 # in the hash to be overwritten.
180 raise OSM::APIDuplicateTagsError.new if @tags.include? k
186 # the integer coords (i.e: unscaled) bounding box of the way, assuming
187 # straight line segments.
189 lons = nodes.collect { |n| n.longitude }
190 lats = nodes.collect { |n| n.latitude }
191 [ lons.min, lats.min, lons.max, lats.max ]
194 def save_with_history!
197 # update the bounding box, but don't save it as the controller knows the
198 # lifetime of the change better. note that this has to be done both before
199 # and after the save, so that nodes from both versions are included in the
201 changeset.update_bbox!(bbox) unless nodes.empty?
209 WayTag.delete_all(['id = ?', self.id])
219 WayNode.delete_all(['id = ?', self.id])
223 nd.id = [self.id, sequence]
229 old_way = OldWay.from_way(self)
230 old_way.timestamp = t
231 old_way.save_with_dependencies!
233 # update and commit the bounding box, now that way nodes
234 # have been updated and we're in a transaction.
235 changeset.update_bbox!(bbox) unless nodes.empty?
237 # tell the changeset we updated one element only
238 changeset.add_changes! 1
244 def update_from(new_way, user)
245 check_consistency(self, new_way, user)
246 if !new_way.preconditions_ok?
247 raise OSM::APIPreconditionFailedError.new
249 self.changeset_id = new_way.changeset_id
250 self.tags = new_way.tags
251 self.nds = new_way.nds
256 def create_with_history(user)
257 check_create_consistency(self, user)
258 if !self.preconditions_ok?
259 raise OSM::APIPreconditionFailedError.new
266 def preconditions_ok?
267 return false if self.nds.empty?
268 if self.nds.length > APP_CONFIG['max_number_of_way_nodes']
269 raise OSM::APITooManyWayNodesError.new(self.nds.count, APP_CONFIG['max_number_of_way_nodes'])
272 node = Node.find(:first, :conditions => ["id = ?", n])
273 unless node and node.visible
280 def delete_with_history!(new_way, user)
281 check_consistency(self, new_way, user)
283 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
284 :conditions => [ "visible = ? AND member_type='way' and member_id=? ", true, self.id])
285 raise OSM::APIPreconditionFailedError
287 self.changeset_id = new_way.changeset_id
291 self.save_with_history!
294 raise OSM::APIAlreadyDeletedError
298 # delete a way and its nodes that aren't part of other ways, with history
300 # FIXME: merge the potlatch code to delete the relations
301 # and refactor to use delete_with_history!
302 def delete_with_relations_and_nodes_and_history(changeset_id)
303 # delete the nodes not used by other ways
304 self.unshared_node_ids.each do |node_id|
305 n = Node.find(node_id)
306 n.changeset_id = changeset_id
311 self.changeset_id = changeset_id
315 self.save_with_history!
318 # Find nodes that belong to this way only
319 def unshared_node_ids
320 node_ids = self.nodes.collect { |node| node.id }
322 unless node_ids.empty?
323 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
324 node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
330 # Temporary method to match interface to nodes
336 # if any referenced nodes are placeholder IDs (i.e: are negative) then
337 # this calling this method will fix them using the map from placeholders
339 def fix_placeholders!(id_map)
340 self.nds.map! do |node_id|
342 new_id = id_map[:node][node_id]
343 raise "invalid placeholder for #{node_id.inspect}: #{new_id.inspect}" if new_id.nil?