2   class RelationsController < ApiController
 
   5     before_action :check_api_writable, :only => [:create, :update, :delete]
 
   6     before_action :check_api_readable, :except => [:create, :update, :delete]
 
   7     before_action :authorize, :only => [:create, :update, :delete]
 
  11     before_action :require_public_data, :only => [:create, :update, :delete]
 
  12     around_action :api_call_handle_error, :api_call_timeout
 
  14     before_action :set_request_formats, :except => [:create, :update, :delete]
 
  17       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
 
  19       ids = params["relations"].split(",").collect(&:to_i)
 
  21       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
 
  23       @relations = Relation.find(ids)
 
  26       respond_to do |format|
 
  33       @relation = Relation.find(params[:id])
 
  34       response.last_modified = @relation.timestamp
 
  37         respond_to do |format|
 
  49       relation = Relation.from_xml(request.raw_post, :create => true)
 
  51       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
 
  52       relation.create_with_history current_user
 
  53       render :plain => relation.id.to_s
 
  57       logger.debug request.raw_post
 
  59       relation = Relation.find(params[:id])
 
  60       new_relation = Relation.from_xml(request.raw_post)
 
  62       raise OSM::APIBadUserInput, "The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})" unless new_relation && new_relation.id == relation.id
 
  64       relation.update_from new_relation, current_user
 
  65       render :plain => relation.version.to_s
 
  69       relation = Relation.find(params[:id])
 
  70       new_relation = Relation.from_xml(request.raw_post)
 
  71       if new_relation && new_relation.id == relation.id
 
  72         relation.delete_with_history!(new_relation, current_user)
 
  73         render :plain => relation.version.to_s
 
  79     # -----------------------------------------------------------------
 
  82     # input parameters: id
 
  84     # returns XML representation of one relation object plus all its
 
  85     # members, plus all nodes part of member ways
 
  86     # -----------------------------------------------------------------
 
  88       relation = Relation.find(params[:id])
 
  92         # first find the ids of nodes, ways and relations referenced by this
 
  93         # relation - note that we exclude this relation just in case.
 
  95         node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
 
  96         way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
 
  97         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
 
  99         # next load the relations and the ways.
 
 101         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
 
 102         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
 
 104         # now additionally collect nodes referenced by ways. Note how we
 
 105         # recursively evaluate ways but NOT relations.
 
 107         way_node_ids = ways.collect do |way|
 
 108           way.way_nodes.collect(&:node_id)
 
 110         node_ids += way_node_ids.flatten
 
 111         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
 
 117           next unless node.visible? # should be unnecessary if data is consistent.
 
 120           visible_nodes[node.id] = node
 
 125           next unless way.visible? # should be unnecessary if data is consistent.
 
 131         relations.each do |rel|
 
 132           next unless rel.visible? # should be unnecessary if data is consistent.
 
 138         @relations << relation
 
 141         respond_to do |format|
 
 150     def relations_for_way
 
 151       relations_for_object("Way")
 
 154     def relations_for_node
 
 155       relations_for_object("Node")
 
 158     def relations_for_relation
 
 159       relations_for_object("Relation")
 
 164     def relations_for_object(objtype)
 
 165       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
 
 169       Relation.find(relationids).each do |relation|
 
 170         @relations << relation if relation.visible
 
 174       respond_to do |format|