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]
15 before_action :check_rate_limit, :only => [:create, :update, :delete]
18 raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
20 ids = params["relations"].split(",").collect(&:to_i)
22 raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
24 @relations = Relation.find(ids)
27 respond_to do |format|
34 @relation = Relation.find(params[:id])
35 response.last_modified = @relation.timestamp
38 respond_to do |format|
50 relation = Relation.from_xml(request.raw_post, :create => true)
52 # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
53 relation.create_with_history current_user
54 render :plain => relation.id.to_s
58 logger.debug request.raw_post
60 relation = Relation.find(params[:id])
61 new_relation = Relation.from_xml(request.raw_post)
63 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
65 relation.update_from new_relation, current_user
66 render :plain => relation.version.to_s
70 relation = Relation.find(params[:id])
71 new_relation = Relation.from_xml(request.raw_post)
72 if new_relation && new_relation.id == relation.id
73 relation.delete_with_history!(new_relation, current_user)
74 render :plain => relation.version.to_s
80 # -----------------------------------------------------------------
83 # input parameters: id
85 # returns XML representation of one relation object plus all its
86 # members, plus all nodes part of member ways
87 # -----------------------------------------------------------------
89 relation = Relation.find(params[:id])
93 # first find the ids of nodes, ways and relations referenced by this
94 # relation - note that we exclude this relation just in case.
96 node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
97 way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
98 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
100 # next load the relations and the ways.
102 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
103 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
105 # now additionally collect nodes referenced by ways. Note how we
106 # recursively evaluate ways but NOT relations.
108 way_node_ids = ways.collect do |way|
109 way.way_nodes.collect(&:node_id)
111 node_ids += way_node_ids.flatten
112 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
118 next unless node.visible? # should be unnecessary if data is consistent.
121 visible_nodes[node.id] = node
126 next unless way.visible? # should be unnecessary if data is consistent.
132 relations.each do |rel|
133 next unless rel.visible? # should be unnecessary if data is consistent.
139 @relations << relation
142 respond_to do |format|
151 def relations_for_way
152 relations_for_object("Way")
155 def relations_for_node
156 relations_for_object("Node")
159 def relations_for_relation
160 relations_for_object("Relation")
165 def relations_for_object(objtype)
166 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
170 Relation.find(relationids).each do |relation|
171 @relations << relation if relation.visible
175 respond_to do |format|