]> git.openstreetmap.org Git - rails.git/blob - app/controllers/relation_controller.rb
Eager load node and way details when processing a map call.
[rails.git] / app / controllers / relation_controller.rb
1 class RelationController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :require_public_data, :only => [:create, :update, :delete]
7   before_filter :check_api_writable, :only => [:create, :update, :delete]
8   before_filter :check_api_readable, :except => [:create, :update, :delete]
9   after_filter :compress_output
10
11   def create
12     begin
13       if request.put?
14         relation = Relation.from_xml(request.raw_post, true)
15
16         # We assume that an exception has been thrown if there was an error 
17         # generating the relation
18         #if relation
19           relation.create_with_history @user
20           render :text => relation.id.to_s, :content_type => "text/plain"
21         #else
22          # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
23         #end
24       else
25         render :nothing => true, :status => :method_not_allowed
26       end
27     rescue OSM::APIError => ex
28       render ex.render_opts
29     end
30   end
31
32   def read
33     begin
34       relation = Relation.find(params[:id])
35       response.headers['Last-Modified'] = relation.timestamp.rfc822
36       if relation.visible
37         render :text => relation.to_xml.to_s, :content_type => "text/xml"
38       else
39         render :text => "", :status => :gone
40       end
41     rescue ActiveRecord::RecordNotFound
42       render :nothing => true, :status => :not_found
43     rescue
44       render :nothing => true, :status => :internal_server_error
45     end
46   end
47
48   def update
49     logger.debug request.raw_post
50     begin
51       relation = Relation.find(params[:id])
52       new_relation = Relation.from_xml(request.raw_post)
53
54       if new_relation and new_relation.id == relation.id
55         relation.update_from new_relation, @user
56         render :text => relation.version.to_s, :content_type => "text/plain"
57       else
58         render :nothing => true, :status => :bad_request
59       end
60     rescue ActiveRecord::RecordNotFound
61       render :nothing => true, :status => :not_found
62     rescue OSM::APIError => ex
63       render ex.render_opts
64     end
65   end
66
67   def delete
68     begin
69       relation = Relation.find(params[:id])
70       new_relation = Relation.from_xml(request.raw_post)
71       if new_relation and new_relation.id == relation.id
72         relation.delete_with_history!(new_relation, @user)
73         render :text => relation.version.to_s, :content_type => "text/plain"
74       else
75         render :nothing => true, :status => :bad_request
76       end
77     rescue OSM::APIError => ex
78       render ex.render_opts
79     rescue ActiveRecord::RecordNotFound
80       render :nothing => true, :status => :not_found
81     end
82   end
83
84   # -----------------------------------------------------------------
85   # full
86   # 
87   # input parameters: id
88   #
89   # returns XML representation of one relation object plus all its
90   # members, plus all nodes part of member ways
91   # -----------------------------------------------------------------
92   def full
93     begin
94       relation = Relation.find(params[:id])
95
96       if relation.visible
97
98         # first collect nodes, ways, and relations referenced by this relation.
99         
100         ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
101             "rm.member_type='Way' and rm.member_id=w.id and rm.id=#{relation.id}");
102         nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
103             "rm.member_type='Node' and rm.member_id=n.id and rm.id=#{relation.id}");
104         # note query is built to exclude self just in case.
105         relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
106             "rm.member_type='Relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
107
108         # now additionally collect nodes referenced by ways. Note how we recursively 
109         # evaluate ways but NOT relations.
110
111         node_ids = nodes.collect {|node| node.id }
112         way_node_ids = ways.collect { |way|
113            way.way_nodes.collect { |way_node| way_node.node_id }
114         }
115         way_node_ids.flatten!
116         way_node_ids.uniq
117         way_node_ids -= node_ids
118         nodes += Node.find(way_node_ids)
119     
120         # create XML.
121         doc = OSM::API.new.get_xml_doc
122         visible_nodes = {}
123         changeset_cache = {}
124         user_display_name_cache = {}
125
126         nodes.each do |node|
127           if node.visible? # should be unnecessary if data is consistent.
128             doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
129             visible_nodes[node.id] = node
130           end
131         end
132         ways.each do |way|
133           if way.visible? # should be unnecessary if data is consistent.
134             doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
135           end
136         end
137         relations.each do |rel|
138           if rel.visible? # should be unnecessary if data is consistent.
139             doc.root << rel.to_xml_node(changeset_cache, user_display_name_cache)
140           end
141         end
142         # finally add self and output
143         doc.root << relation.to_xml_node(changeset_cache, user_display_name_cache)
144         render :text => doc.to_s, :content_type => "text/xml"
145
146       else
147         render :nothing => true, :status => :gone
148       end
149
150     rescue ActiveRecord::RecordNotFound
151       render :nothing => true, :status => :not_found
152
153     rescue
154       render :nothing => true, :status => :internal_server_error
155     end
156   end
157
158   def relations
159     ids = params['relations'].split(',').collect { |w| w.to_i }
160
161     if ids.length > 0
162       doc = OSM::API.new.get_xml_doc
163
164       Relation.find(ids).each do |relation|
165         doc.root << relation.to_xml_node
166       end
167
168       render :text => doc.to_s, :content_type => "text/xml"
169     else
170       render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
171     end
172   rescue ActiveRecord::RecordNotFound
173     render :text => "Could not find one of the relations", :status => :not_found
174   end
175
176   def relations_for_way
177     relations_for_object("Way")
178   end
179   def relations_for_node
180     relations_for_object("Node")
181   end
182   def relations_for_relation
183     relations_for_object("Relation")
184   end
185
186   def relations_for_object(objtype)
187     relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
188
189     doc = OSM::API.new.get_xml_doc
190
191     Relation.find(relationids).each do |relation|
192       doc.root << relation.to_xml_node if relation.visible
193     end
194
195     render :text => doc.to_s, :content_type => "text/xml"
196   end
197 end