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
14 before_action :default_format_xml
16 # Set format to xml unless client requires a specific format
17 def default_format_xml
18 request.format = "xml" unless params[:format]
24 relation = Relation.from_xml(request.raw_post, true)
26 # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
27 relation.create_with_history current_user
28 render :plain => relation.id.to_s
32 @relation = Relation.find(params[:id])
33 response.last_modified = @relation.timestamp
36 respond_to do |format|
46 logger.debug request.raw_post
48 relation = Relation.find(params[:id])
49 new_relation = Relation.from_xml(request.raw_post)
51 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
53 relation.update_from new_relation, current_user
54 render :plain => relation.version.to_s
58 relation = Relation.find(params[:id])
59 new_relation = Relation.from_xml(request.raw_post)
60 if new_relation && new_relation.id == relation.id
61 relation.delete_with_history!(new_relation, current_user)
62 render :plain => relation.version.to_s
68 # -----------------------------------------------------------------
71 # input parameters: id
73 # returns XML representation of one relation object plus all its
74 # members, plus all nodes part of member ways
75 # -----------------------------------------------------------------
77 relation = Relation.find(params[:id])
81 # first find the ids of nodes, ways and relations referenced by this
82 # relation - note that we exclude this relation just in case.
84 node_ids = relation.members.select { |m| m[0] == "Node" }.map { |m| m[1] }
85 way_ids = relation.members.select { |m| m[0] == "Way" }.map { |m| m[1] }
86 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.map { |m| m[1] }
88 # next load the relations and the ways.
90 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
91 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
93 # now additionally collect nodes referenced by ways. Note how we
94 # recursively evaluate ways but NOT relations.
96 way_node_ids = ways.collect do |way|
97 way.way_nodes.collect(&:node_id)
99 node_ids += way_node_ids.flatten
100 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
106 next unless node.visible? # should be unnecessary if data is consistent.
109 visible_nodes[node.id] = node
114 next unless way.visible? # should be unnecessary if data is consistent.
120 relations.each do |rel|
121 next unless rel.visible? # should be unnecessary if data is consistent.
127 @relations << relation
130 respond_to do |format|
140 raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
142 ids = params["relations"].split(",").collect(&:to_i)
144 raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
146 @relations = Relation.find(ids)
149 respond_to do |format|
155 def relations_for_way
156 relations_for_object("Way")
159 def relations_for_node
160 relations_for_object("Node")
163 def relations_for_relation
164 relations_for_object("Relation")
169 def relations_for_object(objtype)
170 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
174 Relation.find(relationids).each do |relation|
175 @relations << relation if relation.visible
179 respond_to do |format|