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 logger.debug request.raw_post
53 relation = Relation.find(params[:id])
54 new_relation = Relation.from_xml(request.raw_post)
56 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
58 relation.update_from new_relation, current_user
59 render :plain => relation.version.to_s
63 relation = Relation.find(params[:id])
64 new_relation = Relation.from_xml(request.raw_post)
65 if new_relation && new_relation.id == relation.id
66 relation.delete_with_history!(new_relation, current_user)
67 render :plain => relation.version.to_s
73 # -----------------------------------------------------------------
76 # input parameters: id
78 # returns XML representation of one relation object plus all its
79 # members, plus all nodes part of member ways
80 # -----------------------------------------------------------------
82 relation = Relation.find(params[:id])
86 # first find the ids of nodes, ways and relations referenced by this
87 # relation - note that we exclude this relation just in case.
89 node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
90 way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
91 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
93 # next load the relations and the ways.
95 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
96 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
98 # now additionally collect nodes referenced by ways. Note how we
99 # recursively evaluate ways but NOT relations.
101 way_node_ids = ways.collect do |way|
102 way.way_nodes.collect(&:node_id)
104 node_ids += way_node_ids.flatten
105 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
111 next unless node.visible? # should be unnecessary if data is consistent.
114 visible_nodes[node.id] = node
119 next unless way.visible? # should be unnecessary if data is consistent.
125 relations.each do |rel|
126 next unless rel.visible? # should be unnecessary if data is consistent.
132 @relations << relation
135 respond_to do |format|
144 def relations_for_way
145 relations_for_object("Way")
148 def relations_for_node
149 relations_for_object("Node")
152 def relations_for_relation
153 relations_for_object("Relation")
158 def relations_for_object(objtype)
159 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
163 Relation.find(relationids).each do |relation|
164 @relations << relation if relation.visible
168 respond_to do |format|