2   class RelationsController < ApiController
 
   5     before_action :authorize, :only => [:create, :update, :delete]
 
   9     before_action :require_public_data, :only => [:create, :update, :delete]
 
  10     before_action :check_api_writable, :only => [:create, :update, :delete]
 
  11     before_action :check_api_readable, :except => [:create, :update, :delete]
 
  12     around_action :api_call_handle_error, :api_call_timeout
 
  17       relation = Relation.from_xml(request.raw_post, true)
 
  19       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
 
  20       relation.create_with_history current_user
 
  21       render :plain => relation.id.to_s
 
  25       @relation = Relation.find(params[:id])
 
  26       response.last_modified = @relation.timestamp
 
  29         render :formats => [:xml]
 
  36       logger.debug request.raw_post
 
  38       relation = Relation.find(params[:id])
 
  39       new_relation = Relation.from_xml(request.raw_post)
 
  41       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
 
  43       relation.update_from new_relation, current_user
 
  44       render :plain => relation.version.to_s
 
  48       relation = Relation.find(params[:id])
 
  49       new_relation = Relation.from_xml(request.raw_post)
 
  50       if new_relation && new_relation.id == relation.id
 
  51         relation.delete_with_history!(new_relation, current_user)
 
  52         render :plain => relation.version.to_s
 
  58     # -----------------------------------------------------------------
 
  61     # input parameters: id
 
  63     # returns XML representation of one relation object plus all its
 
  64     # members, plus all nodes part of member ways
 
  65     # -----------------------------------------------------------------
 
  67       relation = Relation.find(params[:id])
 
  71         # first find the ids of nodes, ways and relations referenced by this
 
  72         # relation - note that we exclude this relation just in case.
 
  74         node_ids = relation.members.select { |m| m[0] == "Node" }.map { |m| m[1] }
 
  75         way_ids = relation.members.select { |m| m[0] == "Way" }.map { |m| m[1] }
 
  76         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.map { |m| m[1] }
 
  78         # next load the relations and the ways.
 
  80         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
 
  81         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
 
  83         # now additionally collect nodes referenced by ways. Note how we
 
  84         # recursively evaluate ways but NOT relations.
 
  86         way_node_ids = ways.collect do |way|
 
  87           way.way_nodes.collect(&:node_id)
 
  89         node_ids += way_node_ids.flatten
 
  90         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
 
  96           next unless node.visible? # should be unnecessary if data is consistent.
 
  99           visible_nodes[node.id] = node
 
 104           next unless way.visible? # should be unnecessary if data is consistent.
 
 110         relations.each do |rel|
 
 111           next unless rel.visible? # should be unnecessary if data is consistent.
 
 117         @relations << relation
 
 120         render :formats => [:xml]
 
 127       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
 
 129       ids = params["relations"].split(",").collect(&:to_i)
 
 131       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
 
 133       @relations = Relation.find(ids)
 
 136       render :formats => [:xml]
 
 139     def relations_for_way
 
 140       relations_for_object("Way")
 
 143     def relations_for_node
 
 144       relations_for_object("Node")
 
 147     def relations_for_relation
 
 148       relations_for_object("Relation")
 
 153     def relations_for_object(objtype)
 
 154       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
 
 158       Relation.find(relationids).each do |relation|
 
 159         @relations << relation if relation.visible
 
 163       render :formats => [:xml]