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