1 class RelationController < ApplicationController
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
14 relation = Relation.from_xml(request.raw_post, true)
16 # We assume that an exception has been thrown if there was an error
17 # generating the relation
19 relation.create_with_history @user
20 render :text => relation.id.to_s, :content_type => "text/plain"
22 # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
25 render :nothing => true, :status => :method_not_allowed
27 rescue OSM::APIError => ex
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
49 logger.debug request.raw_post
51 relation = Relation.find(params[:id])
52 new_relation = Relation.from_xml(request.raw_post)
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"
58 render :nothing => true, :status => :bad_request
60 rescue ActiveRecord::RecordNotFound
61 render :nothing => true, :status => :not_found
62 rescue OSM::APIError => ex
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"
75 render :nothing => true, :status => :bad_request
77 rescue OSM::APIError => ex
79 rescue ActiveRecord::RecordNotFound
80 render :nothing => true, :status => :not_found
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
124 user_display_name_cache = {}
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
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)
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)
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"
147 render :nothing => true, :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 :text => "You need to supply a comma separated list of ids.", :status => :bad_request
172 rescue ActiveRecord::RecordNotFound
173 render :text => "Could not find one of the relations", :status => :not_found
176 def relations_for_way
177 relations_for_object("Way")
179 def relations_for_node
180 relations_for_object("Node")
182 def relations_for_relation
183 relations_for_object("Relation")
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
189 doc = OSM::API.new.get_xml_doc
191 Relation.find(relationids).each do |relation|
192 doc.root << relation.to_xml_node if relation.visible
195 render :text => doc.to_s, :content_type => "text/xml"