1 class Way < ActiveRecord::Base
4 include ConsistencyValidations
7 self.table_name = "current_ways"
11 has_many :old_ways, :order => 'version'
13 has_many :way_nodes, :order => 'sequence_id'
14 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
18 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
19 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
21 validates_presence_of :id, :on => :update
22 validates_presence_of :changeset_id,:version, :timestamp
23 validates_uniqueness_of :id
24 validates_inclusion_of :visible, :in => [ true, false ]
25 validates_numericality_of :changeset_id, :version, :integer_only => true
26 validates_numericality_of :id, :on => :update, :integer_only => true
27 validates_associated :changeset
29 scope :visible, where(:visible => true)
30 scope :invisible, where(:visible => false)
32 # Read in xml as text and return it's Way object representation
33 def self.from_xml(xml, create=false)
35 p = XML::Parser.string(xml)
38 doc.find('//osm/way').each do |pt|
39 return Way.from_xml_node(pt, create)
41 raise OSM::APIBadXMLError.new("node", xml, "XML doesn't contain an osm/way element.")
42 rescue LibXML::XML::Error, ArgumentError => ex
43 raise OSM::APIBadXMLError.new("way", xml, ex.message)
47 def self.from_xml_node(pt, create=false)
50 raise OSM::APIBadXMLError.new("way", pt, "Version is required when updating") unless create or not pt['version'].nil?
51 way.version = pt['version']
52 raise OSM::APIBadXMLError.new("way", pt, "Changeset id is missing") if pt['changeset'].nil?
53 way.changeset_id = pt['changeset']
56 raise OSM::APIBadXMLError.new("way", pt, "ID is required when updating") if pt['id'].nil?
57 way.id = pt['id'].to_i
58 # .to_i will return 0 if there is no number that can be parsed.
59 # We want to make sure that there is no id with zero anyway
60 raise OSM::APIBadUserInput.new("ID of way cannot be zero when updating.") if way.id == 0
63 # We don't care about the timestamp nor the visibility as these are either
64 # set explicitly or implicit in the action. The visibility is set to true,
65 # and manually set to false before the actual delete.
71 # Add in any tags from the XML
72 pt.find('tag').each do |tag|
73 raise OSM::APIBadXMLError.new("way", pt, "tag is missing key") if tag['k'].nil?
74 raise OSM::APIBadXMLError.new("way", pt, "tag is missing value") if tag['v'].nil?
75 way.add_tag_keyval(tag['k'], tag['v'])
78 pt.find('nd').each do |nd|
79 way.add_nd_num(nd['ref'])
85 # Find a way given it's ID, and in a single SQL call also grab its nodes
88 # 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
89 def self.find_eager(id)
90 way = Way.find(id, :include => {:way_nodes => :node})
91 #If waytag had a multipart key that was real, you could do this:
92 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
95 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
97 doc = OSM::API.new.get_xml_doc
98 doc.root << to_xml_node()
102 def to_xml_node(visible_nodes = nil, changeset_cache = {}, user_display_name_cache = {})
103 el1 = XML::Node.new 'way'
104 el1['id'] = self.id.to_s
105 el1['visible'] = self.visible.to_s
106 el1['timestamp'] = self.timestamp.xmlschema
107 el1['version'] = self.version.to_s
108 el1['changeset'] = self.changeset_id.to_s
110 if changeset_cache.key?(self.changeset_id)
111 # use the cache if available
113 changeset_cache[self.changeset_id] = self.changeset.user_id
116 user_id = changeset_cache[self.changeset_id]
118 if user_display_name_cache.key?(user_id)
119 # use the cache if available
120 elsif self.changeset.user.data_public?
121 user_display_name_cache[user_id] = self.changeset.user.display_name
123 user_display_name_cache[user_id] = nil
126 if not user_display_name_cache[user_id].nil?
127 el1['user'] = user_display_name_cache[user_id]
128 el1['uid'] = user_id.to_s
131 # make sure nodes are output in sequence_id order
133 self.way_nodes.each do |nd|
135 # if there is a list of visible nodes then use that to weed out deleted nodes
136 if visible_nodes[nd.node_id]
137 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
140 # otherwise, manually go to the db to check things
141 if nd.node and nd.node.visible?
142 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
147 ordered_nodes.each do |nd_id|
148 if nd_id and nd_id != '0'
149 e = XML::Node.new 'nd'
155 self.way_tags.each do |tag|
156 e = XML::Node.new 'tag'
167 self.way_nodes.each do |nd|
177 self.way_tags.each do |tag|
193 @nds = Array.new unless @nds
197 def add_tag_keyval(k, v)
198 @tags = Hash.new unless @tags
200 # duplicate tags are now forbidden, so we can't allow values
201 # in the hash to be overwritten.
202 raise OSM::APIDuplicateTagsError.new("way", self.id, k) if @tags.include? k
208 # the integer coords (i.e: unscaled) bounding box of the way, assuming
209 # straight line segments.
211 lons = nodes.collect { |n| n.longitude }
212 lats = nodes.collect { |n| n.latitude }
213 BoundingBox.new(lons.min, lats.min, lons.max, lats.max)
216 def update_from(new_way, user)
219 check_consistency(self, new_way, user)
220 unless new_way.preconditions_ok?(self.nds)
221 raise OSM::APIPreconditionFailedError.new("Cannot update way #{self.id}: data is invalid.")
224 self.changeset_id = new_way.changeset_id
225 self.changeset = new_way.changeset
226 self.tags = new_way.tags
227 self.nds = new_way.nds
233 def create_with_history(user)
234 check_create_consistency(self, user)
235 unless self.preconditions_ok?
236 raise OSM::APIPreconditionFailedError.new("Cannot create way: data is invalid.")
243 def preconditions_ok?(old_nodes = [])
244 return false if self.nds.empty?
245 if self.nds.length > MAX_NUMBER_OF_WAY_NODES
246 raise OSM::APITooManyWayNodesError.new(self.id, self.nds.length, MAX_NUMBER_OF_WAY_NODES)
249 # check only the new nodes, for efficiency - old nodes having been checked last time and can't
250 # be deleted when they're in-use.
251 new_nds = (self.nds - old_nodes).sort.uniq
253 unless new_nds.empty?
254 db_nds = Node.where(:id => new_nds, :visible => true)
256 if db_nds.length < new_nds.length
257 missing = new_nds - db_nds.collect { |n| n.id }
258 raise OSM::APIPreconditionFailedError.new("Way #{self.id} requires the nodes with id in (#{missing.join(',')}), which either do not exist, or are not visible.")
265 def delete_with_history!(new_way, user)
267 raise OSM::APIAlreadyDeletedError.new("way", new_way.id)
270 # need to start the transaction here, so that the database can
271 # provide repeatable reads for the used-by checks. this means it
272 # shouldn't be possible to get race conditions.
275 check_consistency(self, new_way, user)
276 rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Way", :member_id => id }).order(:id)
277 raise OSM::APIPreconditionFailedError.new("Way #{self.id} is still used by relations #{rels.collect { |r| r.id }.join(",")}.") unless rels.empty?
279 self.changeset_id = new_way.changeset_id
280 self.changeset = new_way.changeset
289 # Temporary method to match interface to nodes
295 # if any referenced nodes are placeholder IDs (i.e: are negative) then
296 # this calling this method will fix them using the map from placeholders
298 def fix_placeholders!(id_map, placeholder_id = nil)
299 self.nds.map! do |node_id|
301 new_id = id_map[:node][node_id]
302 raise OSM::APIBadUserInput.new("Placeholder node not found for reference #{node_id} in way #{self.id.nil? ? placeholder_id : self.id}") if new_id.nil?
312 def save_with_history!
315 # update the bounding box, note that this has to be done both before
316 # and after the save, so that nodes from both versions are included in the
317 # bbox. we use a copy of the changeset so that it isn't reloaded
320 cs.update_bbox!(bbox) unless nodes.empty?
328 WayTag.delete_all(:way_id => self.id)
338 WayNode.delete_all(:way_id => self.id)
342 nd.id = [self.id, sequence]
348 old_way = OldWay.from_way(self)
349 old_way.timestamp = t
350 old_way.save_with_dependencies!
352 # reload the way so that the nodes array points to the correct
356 # update and commit the bounding box, now that way nodes
357 # have been updated and we're in a transaction.
358 cs.update_bbox!(bbox) unless nodes.empty?
360 # tell the changeset we updated one element only