1 class RelationController < ApplicationController
4 before_filter :authorize, :only => [:create, :update, :delete]
5 before_filter :check_availability, :only => [:create, :update, :delete]
7 after_filter :compress_output
11 relation = Relation.from_xml(request.raw_post, true)
14 if !relation.preconditions_ok?
15 render :nothing => true, :status => :precondition_failed
17 relation.user_id = @user.id
18 relation.save_with_history!
20 render :text => relation.id.to_s, :content_type => "text/plain"
23 render :nothing => true, :status => :bad_request
26 render :nothing => true, :status => :method_not_allowed
32 relation = Relation.find(params[:id])
35 render :text => relation.to_xml.to_s, :content_type => "text/xml"
37 render :nothing => true, :status => :gone
39 rescue ActiveRecord::RecordNotFound
40 render :nothing => true, :status => :not_found
42 render :nothing => true, :status => :internal_server_error
48 relation = Relation.find(params[:id])
51 new_relation = Relation.from_xml(request.raw_post)
53 if new_relation and new_relation.id == relation.id
54 if !new_relation.preconditions_ok?
55 render :nothing => true, :status => :precondition_failed
57 relation.user_id = @user.id
58 relation.tags = new_relation.tags
59 relation.members = new_relation.members
60 relation.visible = true
61 relation.save_with_history!
63 render :nothing => true
66 render :nothing => true, :status => :bad_request
69 render :nothing => true, :status => :gone
71 rescue ActiveRecord::RecordNotFound
72 render :nothing => true, :status => :not_found
74 render :nothing => true, :status => :internal_server_error
79 #XXX check if member somewhere!
81 relation = Relation.find(params[:id])
84 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='relation' and member_id=?", params[:id]])
85 render :nothing => true, :status => :precondition_failed
87 relation.user_id = @user.id
90 relation.visible = false
91 relation.save_with_history!
93 render :nothing => true
96 render :nothing => true, :status => :gone
98 rescue ActiveRecord::RecordNotFound
99 render :nothing => true, :status => :not_found
101 render :nothing => true, :status => :internal_server_error
105 # -----------------------------------------------------------------
108 # input parameters: id
110 # returns XML representation of one relation object plus all its
111 # members, plus all nodes part of member ways
112 # -----------------------------------------------------------------
115 relation = Relation.find(params[:id])
119 # first collect nodes, ways, and relations referenced by this relation.
121 ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
122 "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
123 nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
124 "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
125 # note query is built to exclude self just in case.
126 relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
127 "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
129 # now additionally collect nodes referenced by ways. Note how we recursively
130 # evaluate ways but NOT relations.
132 node_ids = nodes.collect {|node| node.id }
133 way_node_ids = ways.collect { |way|
134 way.way_nodes.collect { |way_node| way_node.node_id }
136 way_node_ids.flatten!
138 way_node_ids -= node_ids
139 nodes += Node.find(way_node_ids)
142 doc = OSM::API.new.get_xml_doc
143 user_display_name_cache = {}
146 if node.visible? # should be unnecessary if data is consistent.
147 doc.root << node.to_xml_node(user_display_name_cache)
151 if way.visible? # should be unnecessary if data is consistent.
152 doc.root << way.to_xml_node(user_display_name_cache)
155 relations.each do |rel|
156 if rel.visible? # should be unnecessary if data is consistent.
157 doc.root << rel.to_xml_node(user_display_name_cache)
160 # finally add self and output
161 doc.root << relation.to_xml_node(user_display_name_cache)
162 render :text => doc.to_s, :content_type => "text/xml"
166 render :nothing => true, :status => :gone
169 rescue ActiveRecord::RecordNotFound
170 render :nothing => true, :status => :not_found
173 render :nothing => true, :status => :internal_server_error
178 ids = params['relations'].split(',').collect { |w| w.to_i }
181 doc = OSM::API.new.get_xml_doc
183 Relation.find(ids).each do |relation|
184 doc.root << relation.to_xml_node
187 render :text => doc.to_s, :content_type => "text/xml"
189 render :nothing => true, :status => :bad_request
193 def relations_for_way
194 relations_for_object("way")
196 def relations_for_node
197 relations_for_object("node")
199 def relations_for_relation
200 relations_for_object("relation")
203 def relations_for_object(objtype)
204 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
206 if relationids.length > 0
207 doc = OSM::API.new.get_xml_doc
209 Relation.find(relationids).each do |relation|
210 doc.root << relation.to_xml_node
213 render :text => doc.to_s, :content_type => "text/xml"
215 render :nothing => true, :status => :not_found