1 class RelationController < ApplicationController
5 before_filter :authorize, :only => [:create, :update, :delete]
6 before_filter :check_write_availability, :only => [:create, :update, :delete]
7 before_filter :check_read_availability, :except => [:create, :update, :delete]
8 after_filter :compress_output
12 relation = Relation.from_xml(request.raw_post, true)
15 if !relation.preconditions_ok?
16 render :text => "", :status => :precondition_failed
19 relation.user_id = @user.id
20 relation.save_with_history!
22 render :text => relation.id.to_s, :content_type => "text/plain"
25 render :nothing => true, :status => :bad_request
28 render :nothing => true, :status => :method_not_allowed
34 relation = Relation.find(params[:id])
35 response.headers['Last-Modified'] = relation.timestamp.rfc822
37 render :text => relation.to_xml.to_s, :content_type => "text/xml"
39 render :text => "", :status => :gone
41 rescue ActiveRecord::RecordNotFound
42 render :nothing => true, :status => :not_found
44 render :nothing => true, :status => :internal_server_error
50 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 relation.update_from new_relation, user
55 render :text => relation.version.to_s, :content_type => "text/plain"
57 render :nothing => true, :status => :bad_request
59 rescue ActiveRecord::RecordNotFound
60 render :nothing => true, :status => :not_found
61 rescue OSM::APIPreconditionFailedError
62 render :text => "", :status => :precondition_failed
64 render :nothing => true, :status => :internal_server_error
69 #XXX check if member somewhere!
71 relation = Relation.find(params[:id])
72 relation.delete_with_history(@user)
73 rescue OSM::APIAlreadyDeletedError
74 render :text => "", :status => :gone
75 rescue OSM::APIPreconditionFailedError
76 render :text => "", :status => :precondition_failed
77 rescue ActiveRecord::RecordNotFound
78 render :nothing => true, :status => :not_found
80 render :nothing => true, :status => :internal_server_error
84 # -----------------------------------------------------------------
87 # input parameters: id
89 # returns XML representation of one relation object plus all its
90 # members, plus all nodes part of member ways
91 # -----------------------------------------------------------------
94 relation = Relation.find(params[:id])
98 # first collect nodes, ways, and relations referenced by this relation.
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");
108 # now additionally collect nodes referenced by ways. Note how we recursively
109 # evaluate ways but NOT relations.
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 }
115 way_node_ids.flatten!
117 way_node_ids -= node_ids
118 nodes += Node.find(way_node_ids)
121 doc = OSM::API.new.get_xml_doc
123 user_display_name_cache = {}
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
132 if way.visible? # should be unnecessary if data is consistent.
133 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
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)
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"
147 render :text => "", :status => :gone
150 rescue ActiveRecord::RecordNotFound
151 render :nothing => true, :status => :not_found
154 render :nothing => true, :status => :internal_server_error
159 ids = params['relations'].split(',').collect { |w| w.to_i }
162 doc = OSM::API.new.get_xml_doc
164 Relation.find(ids).each do |relation|
165 doc.root << relation.to_xml_node
168 render :text => doc.to_s, :content_type => "text/xml"
170 render :nothing => true, :status => :bad_request
174 def relations_for_way
175 relations_for_object("way")
177 def relations_for_node
178 relations_for_object("node")
180 def relations_for_relation
181 relations_for_object("relation")
184 def relations_for_object(objtype)
185 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
187 doc = OSM::API.new.get_xml_doc
189 Relation.find(relationids).each do |relation|
190 doc.root << relation.to_xml_node
193 render :text => doc.to_s, :content_type => "text/xml"