+ ##
+ # 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 }
+ [ lons.min, lats.min, lons.max, lats.max ]
+ end
+
+ def update_from(new_way, user)
+ check_consistency(self, new_way, user)
+ unless new_way.preconditions_ok?
+ 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
+
+ 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?
+ return false if self.nds.empty?
+ if self.nds.length > APP_CONFIG['max_number_of_way_nodes']
+ raise OSM::APITooManyWayNodesError.new(self.nds.length, APP_CONFIG['max_number_of_way_nodes'])
+ end
+ self.nds.each do |n|
+ node = Node.find(:first, :conditions => ["id = ?", n])
+ unless node and node.visible
+ raise OSM::APIPreconditionFailedError.new("Way #{self.id} requires the node with id #{n}, which either does not exist, or is 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
+ check_consistency(self, new_way, user)
+ rel = RelationMember.find(:first, :joins => :relation,
+ :conditions => [ "visible = ? AND member_type='Way' and member_id=? ", true, self.id])
+ raise OSM::APIPreconditionFailedError.new("Way #{self.id} still used by relation #{rel.relation.id}.") if rel
+
+ 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
+