2 class RelationsController < ApiController
3 before_action :check_api_writable, :only => [:create, :update, :delete]
4 before_action :check_api_readable, :except => [:create, :update, :delete]
5 before_action :authorize, :only => [:create, :update, :delete]
9 before_action :require_public_data, :only => [:create, :update, :delete]
10 around_action :api_call_handle_error, :api_call_timeout
12 before_action :set_request_formats, :except => [:create, :update, :delete]
13 before_action :check_rate_limit, :only => [:create, :update, :delete]
16 raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
18 ids = params["relations"].split(",").collect(&:to_i)
20 raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
22 @relations = Relation.find(ids)
25 respond_to do |format|
32 @relation = Relation.find(params[:id])
33 response.last_modified = @relation.timestamp
36 respond_to do |format|
46 relation = Relation.from_xml(request.raw_post, :create => true)
48 # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
49 relation.create_with_history current_user
50 render :plain => relation.id.to_s
54 logger.debug request.raw_post
56 relation = Relation.find(params[:id])
57 new_relation = Relation.from_xml(request.raw_post)
59 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
61 relation.update_from new_relation, current_user
62 render :plain => relation.version.to_s
66 relation = Relation.find(params[:id])
67 new_relation = Relation.from_xml(request.raw_post)
68 if new_relation && new_relation.id == relation.id
69 relation.delete_with_history!(new_relation, current_user)
70 render :plain => relation.version.to_s
76 # -----------------------------------------------------------------
79 # input parameters: id
81 # returns XML representation of one relation object plus all its
82 # members, plus all nodes part of member ways
83 # -----------------------------------------------------------------
85 relation = Relation.find(params[:id])
89 # first find the ids of nodes, ways and relations referenced by this
90 # relation - note that we exclude this relation just in case.
92 node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
93 way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
94 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
96 # next load the relations and the ways.
98 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
99 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
101 # now additionally collect nodes referenced by ways. Note how we
102 # recursively evaluate ways but NOT relations.
104 way_node_ids = ways.collect do |way|
105 way.way_nodes.collect(&:node_id)
107 node_ids += way_node_ids.flatten
108 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
114 next unless node.visible? # should be unnecessary if data is consistent.
117 visible_nodes[node.id] = node
122 next unless way.visible? # should be unnecessary if data is consistent.
128 relations.each do |rel|
129 next unless rel.visible? # should be unnecessary if data is consistent.
135 @relations << relation
138 respond_to do |format|
147 def relations_for_way
148 relations_for_object("Way")
151 def relations_for_node
152 relations_for_object("Node")
155 def relations_for_relation
156 relations_for_object("Relation")
161 def relations_for_object(objtype)
162 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
166 Relation.find(relationids).each do |relation|
167 @relations << relation if relation.visible
171 respond_to do |format|