]> git.openstreetmap.org Git - rails.git/blob - app/controllers/relation_controller.rb
Force selection to fail when no traces match the tag.
[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         user_display_name_cache = {}
124
125         nodes.each do |node|
126           if node.visible? # should be unnecessary if data is consistent.
127             doc.root << node.to_xml_node(user_display_name_cache)
128             visible_nodes[node.id] = node
129           end
130         end
131         ways.each do |way|
132           if way.visible? # should be unnecessary if data is consistent.
133             doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
134           end
135         end
136         relations.each do |rel|
137           if rel.visible? # should be unnecessary if data is consistent.
138             doc.root << rel.to_xml_node(user_display_name_cache)
139           end
140         end
141         # finally add self and output
142         doc.root << relation.to_xml_node(user_display_name_cache)
143         render :text => doc.to_s, :content_type => "text/xml"
144
145       else
146         render :nothing => true, :status => :gone
147       end
148
149     rescue ActiveRecord::RecordNotFound
150       render :nothing => true, :status => :not_found
151
152     rescue
153       render :nothing => true, :status => :internal_server_error
154     end
155   end
156
157   def relations
158     ids = params['relations'].split(',').collect { |w| w.to_i }
159
160     if ids.length > 0
161       doc = OSM::API.new.get_xml_doc
162
163       Relation.find(ids).each do |relation|
164         doc.root << relation.to_xml_node
165       end
166
167       render :text => doc.to_s, :content_type => "text/xml"
168     else
169       render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
170     end
171   rescue ActiveRecord::RecordNotFound
172     render :text => "Could not find one of the relations", :status => :not_found
173   end
174
175   def relations_for_way
176     relations_for_object("Way")
177   end
178   def relations_for_node
179     relations_for_object("Node")
180   end
181   def relations_for_relation
182     relations_for_object("Relation")
183   end
184
185   def relations_for_object(objtype)
186     relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
187
188     doc = OSM::API.new.get_xml_doc
189
190     Relation.find(relationids).each do |relation|
191       doc.root << relation.to_xml_node if relation.visible
192     end
193
194     render :text => doc.to_s, :content_type => "text/xml"
195   end
196 end