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