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 around_action :api_call_handle_error, :api_call_timeout
11 before_action :set_request_formats, :except => [:create, :update, :delete]
12 before_action :check_rate_limit, :only => [:create, :update, :delete]
15 raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
17 ids = params["relations"].split(",").collect(&:to_i)
19 raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
21 @relations = Relation.find(ids)
24 respond_to do |format|
31 @relation = Relation.find(params[:id])
32 response.last_modified = @relation.timestamp
35 respond_to do |format|
45 relation = Relation.from_xml(request.raw_post, :create => true)
47 # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
48 relation.create_with_history current_user
49 render :plain => relation.id.to_s
53 logger.debug request.raw_post
55 relation = Relation.find(params[:id])
56 new_relation = Relation.from_xml(request.raw_post)
58 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
60 relation.update_from new_relation, current_user
61 render :plain => relation.version.to_s
65 relation = Relation.find(params[:id])
66 new_relation = Relation.from_xml(request.raw_post)
67 if new_relation && new_relation.id == relation.id
68 relation.delete_with_history!(new_relation, current_user)
69 render :plain => relation.version.to_s
75 # -----------------------------------------------------------------
78 # input parameters: id
80 # returns XML representation of one relation object plus all its
81 # members, plus all nodes part of member ways
82 # -----------------------------------------------------------------
84 relation = Relation.find(params[:id])
88 # first find the ids of nodes, ways and relations referenced by this
89 # relation - note that we exclude this relation just in case.
91 node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
92 way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
93 relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
95 # next load the relations and the ways.
97 relations = Relation.where(:id => relation_ids).includes(:relation_tags)
98 ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
100 # now additionally collect nodes referenced by ways. Note how we
101 # recursively evaluate ways but NOT relations.
103 way_node_ids = ways.collect do |way|
104 way.way_nodes.collect(&:node_id)
106 node_ids += way_node_ids.flatten
107 nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
113 next unless node.visible? # should be unnecessary if data is consistent.
116 visible_nodes[node.id] = node
121 next unless way.visible? # should be unnecessary if data is consistent.
127 relations.each do |rel|
128 next unless rel.visible? # should be unnecessary if data is consistent.
134 @relations << relation
137 respond_to do |format|
146 def relations_for_way
147 relations_for_object("Way")
150 def relations_for_node
151 relations_for_object("Node")
154 def relations_for_relation
155 relations_for_object("Relation")
160 def relations_for_object(objtype)
161 relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
165 Relation.find(relationids).each do |relation|
166 @relations << relation if relation.visible
170 respond_to do |format|