1 class Way < ActiveRecord::Base
4 include ConsistencyValidations
6 set_table_name 'current_ways'
10 has_many :old_ways, :foreign_key => 'id', :order => 'version'
12 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
13 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
15 has_many :way_tags, :foreign_key => 'id'
17 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
18 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
20 validates_presence_of :id, :on => :update
21 validates_presence_of :changeset_id,:version, :timestamp
22 validates_uniqueness_of :id
23 validates_inclusion_of :visible, :in => [ true, false ]
24 validates_numericality_of :changeset_id, :version, :integer_only => true
25 validates_numericality_of :id, :on => :update, :integer_only => true
26 validates_associated :changeset
28 def self.from_xml(xml, create=false)
34 doc.find('//osm/way').each do |pt|
35 return Way.from_xml_node(pt, create)
42 def self.from_xml_node(pt, create=false)
45 if !create and pt['id'] != '0'
46 way.id = pt['id'].to_i
49 way.version = pt['version']
50 way.changeset_id = pt['changeset']
53 way.timestamp = Time.now
57 way.timestamp = Time.parse(pt['timestamp'])
59 # if visible isn't present then it defaults to true
60 way.visible = (pt['visible'] or true)
63 pt.find('tag').each do |tag|
64 way.add_tag_keyval(tag['k'], tag['v'])
67 pt.find('nd').each do |nd|
68 way.add_nd_num(nd['ref'])
74 # Find a way given it's ID, and in a single SQL call also grab its nodes
77 # 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
78 def self.find_eager(id)
79 way = Way.find(id, :include => {:way_nodes => :node})
80 #If waytag had a multipart key that was real, you could do this:
81 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
84 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
86 doc = OSM::API.new.get_xml_doc
87 doc.root << to_xml_node()
91 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
92 el1 = XML::Node.new 'way'
93 el1['id'] = self.id.to_s
94 el1['visible'] = self.visible.to_s
95 el1['timestamp'] = self.timestamp.xmlschema
96 el1['version'] = self.version.to_s
97 el1['changeset'] = self.changeset_id.to_s
99 user_display_name_cache = {} if user_display_name_cache.nil?
101 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
102 # use the cache if available
103 elsif self.changeset.user.data_public?
104 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
106 user_display_name_cache[self.changeset.user_id] = nil
109 if not user_display_name_cache[self.changeset.user_id].nil?
110 el1['user'] = user_display_name_cache[self.changeset.user_id]
111 el1['uid'] = self.changeset.user_id.to_s
114 # make sure nodes are output in sequence_id order
116 self.way_nodes.each do |nd|
118 # if there is a list of visible nodes then use that to weed out deleted nodes
119 if visible_nodes[nd.node_id]
120 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
123 # otherwise, manually go to the db to check things
124 if nd.node and nd.node.visible?
125 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
130 ordered_nodes.each do |nd_id|
131 if nd_id and nd_id != '0'
132 e = XML::Node.new 'nd'
138 self.way_tags.each do |tag|
139 e = XML::Node.new 'tag'
150 self.way_nodes.each do |nd|
160 self.way_tags.each do |tag|
176 @nds = Array.new unless @nds
180 def add_tag_keyval(k, v)
181 @tags = Hash.new unless @tags
183 # duplicate tags are now forbidden, so we can't allow values
184 # in the hash to be overwritten.
185 raise OSM::APIDuplicateTagsError.new if @tags.include? k
191 # the integer coords (i.e: unscaled) bounding box of the way, assuming
192 # straight line segments.
194 lons = nodes.collect { |n| n.longitude }
195 lats = nodes.collect { |n| n.latitude }
196 [ lons.min, lats.min, lons.max, lats.max ]
199 def save_with_history!
202 # update the bounding box, but don't save it as the controller knows the
203 # lifetime of the change better. note that this has to be done both before
204 # and after the save, so that nodes from both versions are included in the
206 changeset.update_bbox!(bbox) unless nodes.empty?
214 WayTag.delete_all(['id = ?', self.id])
224 WayNode.delete_all(['id = ?', self.id])
228 nd.id = [self.id, sequence]
234 old_way = OldWay.from_way(self)
235 old_way.timestamp = t
236 old_way.save_with_dependencies!
238 # update and commit the bounding box, now that way nodes
239 # have been updated and we're in a transaction.
240 changeset.update_bbox!(bbox) unless nodes.empty?
242 # tell the changeset we updated one element only
243 changeset.add_changes! 1
249 def update_from(new_way, user)
250 check_consistency(self, new_way, user)
251 if !new_way.preconditions_ok?
252 raise OSM::APIPreconditionFailedError.new
254 self.changeset_id = new_way.changeset_id
255 self.tags = new_way.tags
256 self.nds = new_way.nds
261 def create_with_history(user)
262 check_create_consistency(self, user)
263 if !self.preconditions_ok?
264 raise OSM::APIPreconditionFailedError.new
271 def preconditions_ok?
272 return false if self.nds.empty?
273 if self.nds.length > APP_CONFIG['max_number_of_way_nodes']
274 raise OSM::APITooManyWayNodesError.new(self.nds.count, APP_CONFIG['max_number_of_way_nodes'])
277 node = Node.find(:first, :conditions => ["id = ?", n])
278 unless node and node.visible
285 def delete_with_history!(new_way, user)
286 check_consistency(self, new_way, user)
288 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
289 :conditions => [ "visible = ? AND member_type='way' and member_id=? ", true, self.id])
290 raise OSM::APIPreconditionFailedError
292 self.changeset_id = new_way.changeset_id
296 self.save_with_history!
299 raise OSM::APIAlreadyDeletedError
303 # delete a way and its nodes that aren't part of other ways, with history
305 # FIXME: merge the potlatch code to delete the relations
306 # and refactor to use delete_with_history!
307 def delete_with_relations_and_nodes_and_history(changeset_id)
308 # delete the nodes not used by other ways
309 self.unshared_node_ids.each do |node_id|
310 n = Node.find(node_id)
311 n.changeset_id = changeset_id
316 self.changeset_id = changeset_id
320 self.save_with_history!
323 # Find nodes that belong to this way only
324 def unshared_node_ids
325 node_ids = self.nodes.collect { |node| node.id }
327 unless node_ids.empty?
328 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
329 node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
335 # Temporary method to match interface to nodes
341 # if any referenced nodes are placeholder IDs (i.e: are negative) then
342 # this calling this method will fix them using the map from placeholders
344 def fix_placeholders!(id_map)
345 self.nds.map! do |node_id|
347 new_id = id_map[:node][node_id]
348 raise "invalid placeholder for #{node_id.inspect}: #{new_id.inspect}" if new_id.nil?