+ ##
+ # the integer coords (i.e: unscaled) bounding box of the way, assuming
+ # straight line segments.
+ def bbox
+ lons = nodes.collect { |n| n.longitude }
+ lats = nodes.collect { |n| n.latitude }
+ BoundingBox.new(lons.min, lats.min, lons.max, lats.max)
+ end
+
+ def update_from(new_way, user)
+ Way.transaction do
+ self.lock!
+ check_consistency(self, new_way, user)
+ unless new_way.preconditions_ok?(self.nds)
+ raise OSM::APIPreconditionFailedError.new("Cannot update way #{self.id}: data is invalid.")
+ end
+
+ self.changeset_id = new_way.changeset_id
+ self.changeset = new_way.changeset
+ self.tags = new_way.tags
+ self.nds = new_way.nds
+ self.visible = true
+ save_with_history!
+ end
+ end
+
+ def create_with_history(user)
+ check_create_consistency(self, user)
+ unless self.preconditions_ok?
+ raise OSM::APIPreconditionFailedError.new("Cannot create way: data is invalid.")
+ end
+ self.version = 0
+ self.visible = true
+ save_with_history!
+ end
+
+ def preconditions_ok?(old_nodes = [])
+ return false if self.nds.empty?
+ if self.nds.length > MAX_NUMBER_OF_WAY_NODES
+ raise OSM::APITooManyWayNodesError.new(self.id, self.nds.length, MAX_NUMBER_OF_WAY_NODES)
+ end
+
+ # check only the new nodes, for efficiency - old nodes having been checked last time and can't
+ # be deleted when they're in-use.
+ new_nds = (self.nds - old_nodes).sort.uniq
+
+ unless new_nds.empty?
+ db_nds = Node.where(:id => new_nds, :visible => true)
+
+ if db_nds.length < new_nds.length
+ missing = new_nds - db_nds.collect { |n| n.id }
+ raise OSM::APIPreconditionFailedError.new("Way #{self.id} requires the nodes with id in (#{missing.join(',')}), which either do not exist, or are not visible.")
+ end
+ end
+
+ return true
+ end
+
+ def delete_with_history!(new_way, user)
+ unless self.visible
+ raise OSM::APIAlreadyDeletedError.new("way", new_way.id)
+ end
+
+ # need to start the transaction here, so that the database can
+ # provide repeatable reads for the used-by checks. this means it
+ # shouldn't be possible to get race conditions.
+ Way.transaction do
+ self.lock!
+ check_consistency(self, new_way, user)
+ rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Way", :member_id => id }).order(:id)
+ raise OSM::APIPreconditionFailedError.new("Way #{self.id} is still used by relations #{rels.collect { |r| r.id }.join(",")}.") unless rels.empty?
+
+ self.changeset_id = new_way.changeset_id
+ self.changeset = new_way.changeset
+
+ self.tags = []
+ self.nds = []
+ self.visible = false
+ save_with_history!
+ end
+ end
+
+ # Temporary method to match interface to nodes
+ def tags_as_hash
+ return self.tags
+ end
+
+ ##
+ # if any referenced nodes are placeholder IDs (i.e: are negative) then
+ # this calling this method will fix them using the map from placeholders
+ # to IDs +id_map+.
+ def fix_placeholders!(id_map, placeholder_id = nil)
+ self.nds.map! do |node_id|
+ if node_id < 0
+ new_id = id_map[:node][node_id]
+ 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?
+ new_id
+ else
+ node_id
+ end
+ end
+ end
+
+ private
+