]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/relations_controller.rb
Merge remote-tracking branch 'upstream/pull/4622'
[rails.git] / app / controllers / api / relations_controller.rb
1 module Api
2   class RelationsController < ApiController
3     require "xml/libxml"
4
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]
8
9     authorize_resource
10
11     before_action :require_public_data, :only => [:create, :update, :delete]
12     around_action :api_call_handle_error, :api_call_timeout
13
14     before_action :set_request_formats, :except => [:create, :update, :delete]
15     before_action :check_rate_limit, :only => [:create, :update, :delete]
16
17     def index
18       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
19
20       ids = params["relations"].split(",").collect(&:to_i)
21
22       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
23
24       @relations = Relation.find(ids)
25
26       # Render the result
27       respond_to do |format|
28         format.xml
29         format.json
30       end
31     end
32
33     def show
34       @relation = Relation.find(params[:id])
35       response.last_modified = @relation.timestamp
36       if @relation.visible
37         # Render the result
38         respond_to do |format|
39           format.xml
40           format.json
41         end
42       else
43         head :gone
44       end
45     end
46
47     def create
48       relation = Relation.from_xml(request.raw_post, :create => true)
49
50       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
51       relation.create_with_history current_user
52       render :plain => relation.id.to_s
53     end
54
55     def update
56       logger.debug request.raw_post
57
58       relation = Relation.find(params[:id])
59       new_relation = Relation.from_xml(request.raw_post)
60
61       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
62
63       relation.update_from new_relation, current_user
64       render :plain => relation.version.to_s
65     end
66
67     def delete
68       relation = Relation.find(params[:id])
69       new_relation = Relation.from_xml(request.raw_post)
70       if new_relation && new_relation.id == relation.id
71         relation.delete_with_history!(new_relation, current_user)
72         render :plain => relation.version.to_s
73       else
74         head :bad_request
75       end
76     end
77
78     # -----------------------------------------------------------------
79     # full
80     #
81     # input parameters: id
82     #
83     # returns XML representation of one relation object plus all its
84     # members, plus all nodes part of member ways
85     # -----------------------------------------------------------------
86     def full
87       relation = Relation.find(params[:id])
88
89       if relation.visible
90
91         # first find the ids of nodes, ways and relations referenced by this
92         # relation - note that we exclude this relation just in case.
93
94         node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
95         way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
96         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
97
98         # next load the relations and the ways.
99
100         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
101         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
102
103         # now additionally collect nodes referenced by ways. Note how we
104         # recursively evaluate ways but NOT relations.
105
106         way_node_ids = ways.collect do |way|
107           way.way_nodes.collect(&:node_id)
108         end
109         node_ids += way_node_ids.flatten
110         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
111
112         visible_nodes = {}
113
114         @nodes = []
115         nodes.each do |node|
116           next unless node.visible? # should be unnecessary if data is consistent.
117
118           @nodes << node
119           visible_nodes[node.id] = node
120         end
121
122         @ways = []
123         ways.each do |way|
124           next unless way.visible? # should be unnecessary if data is consistent.
125
126           @ways << way
127         end
128
129         @relations = []
130         relations.each do |rel|
131           next unless rel.visible? # should be unnecessary if data is consistent.
132
133           @relations << rel
134         end
135
136         # finally add self
137         @relations << relation
138
139         # Render the result
140         respond_to do |format|
141           format.xml
142           format.json
143         end
144       else
145         head :gone
146       end
147     end
148
149     def relations_for_way
150       relations_for_object("Way")
151     end
152
153     def relations_for_node
154       relations_for_object("Node")
155     end
156
157     def relations_for_relation
158       relations_for_object("Relation")
159     end
160
161     private
162
163     def relations_for_object(objtype)
164       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
165
166       @relations = []
167
168       Relation.find(relationids).each do |relation|
169         @relations << relation if relation.visible
170       end
171
172       # Render the result
173       respond_to do |format|
174         format.xml
175         format.json
176       end
177     end
178   end
179 end