+ ##
+ # the integer coords (i.e: unscaled) bounding box of the way, assuming
+ # straight line segments.
+ def bbox
+ lons = nodes.collect(&:longitude)
+ lats = nodes.collect(&: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?(nds)
+ fail OSM::APIPreconditionFailedError.new("Cannot update way #{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?
+ fail 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 nds.empty?
+ if nds.length > MAX_NUMBER_OF_WAY_NODES
+ fail OSM::APITooManyWayNodesError.new(id, 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 = (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(&:id)
+ fail OSM::APIPreconditionFailedError.new("Way #{id} requires the nodes with id in (#{missing.join(',')}), which either do not exist, or are not visible.")
+ end
+ end
+
+ true
+ end
+
+ def delete_with_history!(new_way, user)
+ fail OSM::APIAlreadyDeletedError.new("way", new_way.id) unless visible
+
+ # 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)
+ fail OSM::APIPreconditionFailedError.new("Way #{id} is still used by relations #{rels.collect(&: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
+ 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)
+ nds.map! do |node_id|
+ if node_id < 0
+ new_id = id_map[:node][node_id]
+ fail OSM::APIBadUserInput.new("Placeholder node not found for reference #{node_id} in way #{id.nil? ? placeholder_id : id}") if new_id.nil?
+ new_id
+ else
+ node_id
+ end
+ end
+ end
+
+ private
+