2   class RelationsController < ApiController
 
   3     before_action :check_api_writable, :only => [:create, :update, :delete]
 
   4     before_action :authorize, :only => [:create, :update, :delete]
 
   8     before_action :require_public_data, :only => [:create, :update, :delete]
 
   9     before_action :set_request_formats, :except => [:create, :update, :delete]
 
  10     before_action :check_rate_limit, :only => [:create, :update, :delete]
 
  13       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
 
  15       ids = params["relations"].split(",").collect(&:to_i)
 
  17       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
 
  19       @relations = Relation.find(ids)
 
  22       respond_to do |format|
 
  29       @relation = Relation.find(params[:id])
 
  30       response.last_modified = @relation.timestamp
 
  33         respond_to do |format|
 
  43       relation = Relation.from_xml(request.raw_post, :create => true)
 
  45       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
 
  46       relation.create_with_history current_user
 
  47       render :plain => relation.id.to_s
 
  51       relation = Relation.find(params[:id])
 
  52       new_relation = Relation.from_xml(request.raw_post)
 
  54       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
 
  56       relation.update_from new_relation, current_user
 
  57       render :plain => relation.version.to_s
 
  61       relation = Relation.find(params[:id])
 
  62       new_relation = Relation.from_xml(request.raw_post)
 
  63       if new_relation && new_relation.id == relation.id
 
  64         relation.delete_with_history!(new_relation, current_user)
 
  65         render :plain => relation.version.to_s
 
  71     # -----------------------------------------------------------------
 
  74     # input parameters: id
 
  76     # returns XML representation of one relation object plus all its
 
  77     # members, plus all nodes part of member ways
 
  78     # -----------------------------------------------------------------
 
  80       relation = Relation.find(params[:id])
 
  84         # first find the ids of nodes, ways and relations referenced by this
 
  85         # relation - note that we exclude this relation just in case.
 
  87         node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
 
  88         way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
 
  89         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
 
  91         # next load the relations and the ways.
 
  93         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
 
  94         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
 
  96         # now additionally collect nodes referenced by ways. Note how we
 
  97         # recursively evaluate ways but NOT relations.
 
  99         way_node_ids = ways.collect do |way|
 
 100           way.way_nodes.collect(&:node_id)
 
 102         node_ids += way_node_ids.flatten
 
 103         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
 
 109           next unless node.visible? # should be unnecessary if data is consistent.
 
 112           visible_nodes[node.id] = node
 
 117           next unless way.visible? # should be unnecessary if data is consistent.
 
 123         relations.each do |rel|
 
 124           next unless rel.visible? # should be unnecessary if data is consistent.
 
 130         @relations << relation
 
 133         respond_to do |format|
 
 142     def relations_for_way
 
 143       relations_for_object("Way")
 
 146     def relations_for_node
 
 147       relations_for_object("Node")
 
 150     def relations_for_relation
 
 151       relations_for_object("Relation")
 
 156     def relations_for_object(objtype)
 
 157       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
 
 161       Relation.find(relationids).each do |relation|
 
 162         @relations << relation if relation.visible
 
 166       respond_to do |format|